Selenium_webdriver screenshot and file operation

Source: Internet
Author: User

In the process of automated testing, the key node screenshot is a very useful trick, can intuitively see the whole process and results.

This article to a financial BBS daily check-in to take the copper coins as an example, in the following 8 key nodes to screen: Pre-logon, login, personal information page number of coins, check-in sub-section, today check-in posts, replies key words, replies, personal Information page, the number of coins.

After many tests, the whole process can be completed in about 20s.

The screenshot section code is as follows:

 Public Static intt =1; Public StaticStringGetDateTime() {SimpleDateFormat df =NewSimpleDateFormat ("Yyyymmdd_hhmmss");returnDf.format (NewDate ()); } Public Static void screenshot(Webdriver Dr, String dir) {File screenshot = ((takesscreenshot) DR). Getscreenshotas (Outputtype.file);Try{Fileutils.copyfile (screenshot,NewFile (Dir+getdatetime () +"_"+t+". jpg"));        ++t; }Catch(IOException e)        {E.printstacktrace (); }    }

Screenshots of the picture saved in the Dir path, over time, will generate more and more files, so at the beginning of the judgment of the number of files, too many words deleted.

        new File(dir);        File[] fs = file.listFiles();        if80){            for(File f: fs){                if(f.getName().contains("jpg"))                    f.delete();            }        }

The complete code is as follows:

 PackageCom.company;ImportJava.io.IOException;ImportJava.text.SimpleDateFormat;ImportJava.util.concurrent.TimeUnit;ImportJava.util.Calendar;Importjava.util.regex.*;ImportJava.util.Date;ImportOrg.apache.commons.io.FileUtils;Importorg.openqa.selenium.*;ImportOrg.openqa.selenium.firefox.FirefoxDriver;ImportJava.io.File; Public  class autologinreplyscreenshot {     Public Static intt =1; Public StaticStringGetDateTime() {SimpleDateFormat df =NewSimpleDateFormat ("Yyyymmdd_hhmmss");returnDf.format (NewDate ()); } Public Static void screenshot(Webdriver Dr, String dir) {File screenshot = ((takesscreenshot) DR). Getscreenshotas (Outputtype.file);Try{Fileutils.copyfile (screenshot,NewFile (Dir+getdatetime () +"_"+t+". jpg"));        ++t; }Catch(IOException e)        {E.printstacktrace (); }    } Public Static void Main(String args[])throwsException {System.out.println (Autologinreplyscreenshot.getdatetime ()); Webdriver Driver =NewFirefoxdriver (); String loginurl ="https://www.wacai.com/user/user.action?url=http%3A%2F%2Fbbs.wacai.com%2Fportal.php"; String Basicurl ="http://bbs.wacai.com/portal.php"; String dir ="/users/sophie/ideaprojects/bbsautologinreplyscreenshot/screenshot/"; Driver.manage (). Timeouts (). Implicitlywait ( -, timeunit.seconds);        Driver.get (loginurl); Driver.manage (). window (). Maximize ();//delete All Pics If there is more thanFile File =NewFile (dir); file[] fs = File.listfiles ();if(Fs.length >= the){ for(File F:fs) {if(F.getname (). Contains ("JPG")) F.delete (); }        }/** * 1st Screen shot:login page * /Autologinreplyscreenshot.screenshot (Driver, dir); Driver.findelement (By.id ("Account"). Clear (); Driver.findelement (By.id ("Account"). SendKeys ("******"); Driver.findelement (By.id ("PWD"). Clear (); Driver.findelement (By.id ("PWD"). SendKeys ("******"); Driver.findelement (By.id ("LOGIN-BTN"). Click (); Driver.manage (). window (). Maximize ();/** *2nd Screen shot:after login * /Autologinreplyscreenshot.screenshot (Driver, dir); String a = Driver.findelement (By.cssselector (". Name"). getattribute ("href");        Driver.get (a); String pi = driver.findelement (By.xpath ("//div[@class = ' userhead container ']/div[@class = ' tabs w_tab ']/nav/ul/li[5]/a '). getattribute ("href"); Driver.get (PI);/** * 3rd screen shot:personal Info * *Autologinreplyscreenshot.screenshot (Driver, dir); Driver.get (Basicurl);//get the BBS portal page, find the specific forum hrefA = Driver.findelement (By.linktext ("polite and courteous"). getattribute ("href"); Driver.get (a);/** * 4th Screen shot:check-in Forum * *Autologinreplyscreenshot.screenshot (Driver, dir);//thread titleCalendar C = calendar.getinstance ();intm = C.get (calendar.month) +1;intD = C.get (Calendar.day_of_month); String DD =""+d;if(D <Ten) DD ="0"+DD; String Threadtitle ="polite and courteous"+m+"."+dd+"Sign up for the coins every day.";//find today ' s check-in threadA = Driver.findelement (By.partiallinktext (Threadtitle)). getattribute ("href"); Driver.get (a);/** * 5th screen shot:check-in thread */Autologinreplyscreenshot.screenshot (Driver, dir);//using Regrex To find the content we use to replyPattern p = pattern.compile ("The content of the reply must be .+</font> not this content will recover the coins award");        Matcher r = P.matcher (Driver.getpagesource (). toString ()); StringBuffer key =NewStringBuffer (); while(R.find ())        {Key.append (R.group ()); }//using XPath Locate the TEXTAREA and submit buttonDriver.findelement (By.xpath ("//textarea[@id = ' fastpostmessage ']"). SendKeys (Key.substring (Key.indexof (">")+1, Key.indexof ("</")-1)); Driver.manage (). window (). Maximize ();/** * 6th screen shot:key words */Autologinreplyscreenshot.screenshot (Driver, dir); Driver.findelement (By.xpath ("//button[@id = ' fastpostsubmit ']"). Click ();/** * 7th screen shot:after reply */Autologinreplyscreenshot.screenshot (Driver, dir);//go to personal info pageDriver.get (PI);/** * 8th screen Shot:check The coin amount * /Autologinreplyscreenshot.screenshot (Driver, dir);        Driver.close ();    System.out.println (Autologinreplyscreenshot.getdatetime ()); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Selenium_webdriver screenshot and file operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.