有多种方法可以做到这一点。
创建一个与ScreenCapture不同的类文件,并在此类文件内创建两个方法。
一种方法用于何时成功运行特定的测试用例,另一种方法用于在执行测试脚本期间测试用例失败的情况。
我给你提供了一个类文件。现在
package com.dummy;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
public class ScreenCapture {
public static void passScreenCapture() throws IOException
{
Date d = new Date();
System.out.println(d.toString());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); // Your each screenshot will be taken as this format "Year-Month-Date-Hours-Minutes-Seconds"
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\RND\\"+sdf.format(d)+".png")); //your screenshot path and convert date string to SimpleDateFormat because windows can't capture screenshot with(:)
}
public static void failScreenCapture() throws IOException
{
Date d = new Date();
System.out.println(d.toString());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HHmmss");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("D:\\RND\\"+sdf.format(d)+".png"));
}
}
您的抓屏类文件是用两种不同的方法一起准备。 你需要调用这个方法,你想调用的地方。
你可以直接调用这个方法到任何类如下。
ScreenCapture.passScreenCapture(); //classname.methodname
ScreenCapture.failScreenCapture();
OR
的另一种方法如下。
创建一个类文件,如下所示。
package com.dummy;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.testng.annotations.Test;
public class ScreenShots {
public void captureScreen() throws IOException
{
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("D:\\RND\\Modulename.png"));
}
}
调用此方法的任何类,你可以调用这个方法,这样
public void captureScreen() throws Exception
{
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("D:\\RND\\Modulepage.png"));
System.out.println("Module Page Screen is taken successfully.");
}
那怎么服用单截图知道吗? – acikojevic
任何想法@acikojevic? –
你只是在寻找如何截图? http://stackoverflow.com/questions/3422262/take-a-screenshot-with-selenium-webdriver – mrfreester