import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.interactions.Actions;
public static void takeScreeshot(String screenPath, WebDriver chrome){
try {
//指定了OutputType.FILE做为参数传递给getScreenshotAs()方法,其含义是将截取的屏幕以文件形式返回。
File scrFile = ((TakesScreenshot) chrome)
.getScreenshotAs(OutputType.FILE); // 关键代码,执行屏幕截图,默认会把截图保存到temp目录
FileUtils.copyFile(scrFile, new File(screenPath)); //利用FileUtils工具类的copyFile()方法保存getScreenshotAs()返回的文件对象。
} catch (IOException e) {
System.out.println("Screen shot error: " + screenPath);
System.out.println("该错误可以查看截图:"+screenPath);
} catch (Exception e) {
// TODO: handle exception
}
}
public static void takeScreenshot(WebDriver chrome,String imgName) {
String screenName=imgName+DateUtils.MILLIS_PER_DAY+".jpg";
String fileString= "D:\\selenium\\SchoolpalERP_QTP\\image";
if (!(new File(fileString).isDirectory())) { // 判断是否存在该目录
new File(fileString).mkdir(); // 如果不存在则新建一个目录
}
File dir = new File(fileString);
if (!dir.exists())
dir.mkdirs();
String screenPath = dir.getAbsolutePath() + "\\" + screenName;
takeScreeshot(screenPath, chrome);
}
|