大家好我再次尝试自动测试网络应用程序,所以我需要知道如何在测试期间拍摄多个屏幕截图
那是我的代码
@Test
public void TestJavaS1() {
WebDriver driver;
System.setProperty("webdriver.gecko.driver", "C:\\selenium\\geckodriver.exe");
driver = new FirefoxDriver();
Screenshot.captureScreenShot(driver);
driver.get("http://hotmail.com");
Take.captureScreenShot(driver);
答案 0 :(得分:0)
有多种方法可以做到这一点。
创建一个单独的类文件作为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类文件已准备好以及两种不同的方法。 你需要调用这个方法来调用你想要的地方。
您可以直接将此方法调用到任何类,如下所示。
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.");
}
答案 1 :(得分:0)
每个页面的多个屏幕截图,只需创建一个常用方法,该方法可截取屏幕截图并在您的代码中调用该方法,只要您想截取屏幕即可。正如@Jainish所说。
另一种选择是如果你想在一段时间间隔后截取屏幕截图,例如应该每5秒捕获一次屏幕截图。您可以在java
-
将其放在您的代码中
Runnable takeScreenshot = new Runnable()
{
public void run() {
try {
captureScreenShot();
} catch (IOException e) {
e.printStackTrace();
}
}
};
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(takeScreenshot, 0, 3, TimeUnit.SECONDS);
方法
public void captureScreenShot() throws IOException
{
Date d =new Date();
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:\\My_Folder\\"+d.toString().replace(":", "_")+".png"));
}