[英]How to automatically take full screen shot of a webpage include fixed element using java selenium webdriver ashot firefox
[英]Take a multiple screen shot with selenium and java
大家好,我正在尝试对 Web 应用程序进行自动化测试,所以我需要知道如何在测试期间拍摄多个屏幕截图
那是我的代码
@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);
有多种方法可以做到这一点。
创建一个单独的类文件作为 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();
或者
另一种方式如下。
创建一个类文件,如下所示。
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.");
}
每个页面都有多个屏幕截图,只需创建一个通用的方法来截取屏幕截图,并在您想要截取屏幕截图的任何地方在代码中调用该方法。 正如@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"));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.