After a general Web application error occurs, an exception is thrown. This time can cut a picture down, of course, is very good.
The selenium comes with its own features.
// Get file File scrfile= ((takesscreenshot) driver). Getscreenshotas (Outputtype.file); // move a picture to a specified location New File (Newfilepath));
Of course, before you need to determine whether the new path is legitimate. The whole code is posted below.
PackageCommon;ImportJava.io.File;Importjava.io.IOException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;Importorg.apache.commons.io.FileUtils;ImportOrg.openqa.selenium.OutputType;ImportOrg.openqa.selenium.TakesScreenshot;ImportOrg.openqa.selenium.WebDriver; Public classscreenshot { PublicWebdriver Driver; Publicscreenshot (Webdriver _driver) {driver=_driver; } Private voidtakescreenshot (String screenpath) {Try { //Get FileFile scrfile=((takesscreenshot) driver). Getscreenshotas (Outputtype.file); //move a picture to a specified locationFileutils.movefile (Scrfile,NewFile (Screenpath)); } Catch(IOException e) {e.printstacktrace (); } } Public voidTakescreenshot () {SimpleDateFormat DateFormat=NewSimpleDateFormat ("Yyyy-mm-dd (HH_MM_SS) SSS"); String FileName=dateformat.format (NewDate ()); //Create a new file nameString screenname=filename+ ". jpg"; //Create a folderFile dir =NewFile ("Test-output/snapshot"); if(!dir.exists ()) {Dir.mkdirs (); } //gets the absolute path of the new file nameString screenpath= Dir.getabsolutepath () + "/" +screenname; // This. Takescreenshot (Screenpath); }}
Call
New Screenshot (Driver);
Selenium2 Getting Started assertion failure Automatic (iv)