Method
1 Public Static voidSnapshot (takesscreenshot drivername, String filename)2 {3 //This method would take the screen shot, require-parameters, one is driver name, and another is file name4 5String Currentpath = System.getproperty ("User.dir");//get Current Work folder6 System.out.println (currentpath);7File Scrfile =Drivername.getscreenshotas (outputtype.file);8 //Now you can do whatever need to do with it, for example copy somewhere9 Try {TenSystem.out.println ("Save snapshot Path is:" +currentpath+ "/" +filename); OneFileutils.copyfile (Scrfile,NewFile (currentpath+ "\ \" +filename)); A}Catch(IOException e) { - //TODO auto-generated Catch block -System.out.println ("Can ' t Save screenshot"); the e.printstacktrace (); - } - finally - { + -System.out.println ("Screen shot finished"); + } A}
1. Use selenium to open Baidu;
2. Enter Selenium keyword
3. Search,
The specific code is as follows:
1 ImportJava.io.File;2 Importjava.io.IOException;3 4 Importorg.apache.commons.io.FileUtils;5 Importorg.openqa.selenium.By;6 ImportOrg.openqa.selenium.OutputType;7 ImportOrg.openqa.selenium.TakesScreenshot;8 ImportOrg.openqa.selenium.WebDriver;9 Importorg.openqa.selenium.WebElement;Ten ImportOrg.openqa.selenium.firefox.FirefoxDriver; One ImportOrg.testng.annotations.AfterClass; A ImportOrg.testng.annotations.BeforeClass; - Importorg.testng.annotations.Test; - the Public classTestsavescreen { - - PrivateWebdriver driver; - @BeforeClass + Public voidBeforeclass () { -Driver =Newfirefoxdriver (); +Driver.get ("https://www.baidu.com/"); ASnapshot (Takesscreenshot) driver, "Open_baidu.png"); at } - @Test - Public voidTest () { -Webelement searchinput= driver.findelement (by.id ("kw"))); -Searchinput.sendkeys ("Selenium"); inWebelement searchbutton= driver.findelement (by.id ("su")); - Searchbutton.click (); toSnapshot (Takesscreenshot) driver, "Search_output.png"); + } - the @AfterClass * Public voidAfterclass () { $ driver.quit ();Panax Notoginseng } - the Public Static voidSnapshot (takesscreenshot drivername, String filename) + { A //This method would take the screen shot, require-parameters, one is driver name, and another is file name the +String Currentpath = System.getproperty ("User.dir");//get Current Work folder - System.out.println (currentpath); $File Scrfile =Drivername.getscreenshotas (outputtype.file); $ //Now you can do whatever need to do with it, for example copy somewhere - Try { -System.out.println ("Save snapshot Path is:" +currentpath+ "/" +filename); theFileutils.copyfile (Scrfile,NewFile (currentpath+ "\ \" +filename)); -}Catch(IOException e) {Wuyi //TODO auto-generated Catch block theSystem.out.println ("Can ' t Save screenshot"); - e.printstacktrace (); Wu } - finally About { $ -System.out.println ("Screen shot finished"); - } - } A}
The effect is as follows:
Selenium webdriver--Implementation function