需要编写一个截图类ScreenShot
package com.daanhealth.tnb.util;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
public class ScreenShot {
public WebDriver driver;
public ScreenShot(WebDriver driver){
this.driver=driver;
}
private void takeScreenshot(String screenPath){
try {
File srcFile=((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile, new File(screenPath));
} catch (Exception e) {
// TODO: handle exception
System.out.println("Screen shot error: " + screenPath);
}
}
public void takeScreenshot(){
String screenName=new DateFormat().formatDateToString(System.currentTimeMillis())+".jpg";
File dir=new File("test-output/snapshot");
if(!dir.exists()){
dir.mkdirs();
}
String screenPath=dir.getAbsolutePath()+File.separator+screenName;
takeScreenshot(screenPath);
}
}