是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
当前回答
C#
public static void TakeScreenshot(IWebDriver driver, String filename)
{
// Take a screenshot and save it to filename
Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();
screenshot.SaveAsFile(filename, ImageFormat.Png);
}
其他回答
更新2022
要在Selenium中截取屏幕截图,我们使用一个名为TakesScreenshot的接口,它使Selenium WebDriver能够捕捉屏幕截图并以不同的方式存储它。它有一个getScreenshotAs()方法,用于捕获屏幕截图并将其存储在指定的位置。
//Convert webdriver to TakeScreenshot
File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
在上面的代码中,它将WebDriver对象(驱动程序)转换为TakeScreenshot。并调用getScreenshotAs()方法通过提供参数*OutputType.FILE来创建图像文件。
我们可以使用File对象将图像复制到我们想要的位置,如下所示,使用FileUtils类。
FileUtils.copyFile(screenshotFile , new File("C:\\temp\\screenshot.png));
捕获整个页面
Selenium WebDriver没有提供捕获整个页面截图的固有功能。为了捕获整个页面的截图,我们必须使用一个名为shot的第三方库。它提供了截取特定WebElement的截图以及整页截图的功能。
捕获屏幕大小的图像
Screenshot screenshot = new Ashot().takeScreenshot(driver);
捕获整个页面的截图
Screenshot s=new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
ImageIO.write(s.getImage(),"PNG",new File("<< file path>>"));
在上面的代码中,1000是以毫秒为单位的滚动时间。换句话说,这意味着该程序将滚动每1000毫秒来截取屏幕截图。
捕获一个元素
在Selenium中有两种方法来捕获web元素的屏幕截图。
取全屏图像,然后根据网页元素的尺寸裁剪图像。 在web元素上使用getScreenshotAs()方法。(这只在selenium版本4.X中可用)
我使用下面的c#代码来获取整个页面或仅一个浏览器截图
public void screenShot(string tcName)
{
try
{
string dateTime = string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}", DateTime.Now);
string screenShotName = @"D:\Selenium\Project\VAM\VAM\bin" + "\\" + tcName + dateTime + ".png";
ITakesScreenshot screen = driverScript.driver as ITakesScreenshot;
Screenshot screenshot = screen.GetScreenshot();
screenshot.SaveAsFile(screenShotName, System.Drawing.Imaging.ImageFormat.Png);
if (driverScript.last == 1)
this.writeResult("Sheet1", "Fail see Exception", "Status", driverScript.resultRowID);
}
catch (Exception ex)
{
driverScript.writeLog.writeLogToFile(ex.ToString(), "inside screenShot");
}
}
public void fullPageScreenShot(string tcName)
{
try
{
string dateTime = string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}", DateTime.Now);
string screenShotName = @"D:\Selenium\Project\VAM\VAM\bin" + "\\" + tcName + dateTime + ".png";
Rectangle bounds = Screen.GetBounds(Point.Empty);
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
}
bitmap.Save(screenShotName, System.Drawing.Imaging.ImageFormat.Png);
}
if (driverScript.last == 1)
this.writeResult("Sheet1", "Pass", "Status", driverScript.resultRowID);
}
catch (Exception ex)
{
driverScript.writeLog.writeLogToFile(ex.ToString(), "inside fullPageScreenShot");
}
}
Ruby
require 'rubygems'
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :ie
driver.get "https://www.google.com"
driver.save_screenshot("./screen.png")
更多的文件类型和选项可用,您可以在文件takes_screenshot.rb中看到它们。
是的,可以使用Selenium WebDriver对网页进行快照。
WebDriver API提供的getScreenshotAs()方法为我们做了这项工作。
语法:getScreenshotAs(OutputType<X> target)
返回类型:X
参数:target—查看OutputType提供的选项
适用性:不特定于任何DOM元素
例子:
TakesScreenshot screenshot = (TakesScreenshot) driver;
File file = screenshot.getScreenshotAs(OutputType.FILE);
有关详细信息,请参阅下面的工作代码片段。
public class TakeScreenShotDemo {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get(“http: //www.google.com”);
TakesScreenshot screenshot = (TakesScreenshot) driver;
File file = screenshot.getScreenshotAs(OutputType.FILE);
// Creating a destination file
File destination = new File(“newFilePath(e.g.: C: \\Folder\\ Desktop\\ snapshot.png)”);
try {
FileUtils.copyFile(file, destination);
} catch (IOException e) {
e.printStackTrace();
}
}
}
访问Snapshot using WebDriver获取更多详细信息。
Java
使用RemoteWebDriver,在增加了节点的截图功能后,我将像这样存储截图:
void takeScreenShotMethod(){
try{
Thread.sleep(10000);
long id = Thread.currentThread().getId();
BufferedImage image = new Robot().createScreenCapture(new Rectangle(
Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "jpg", new File("./target/surefire-reports/"
+ id + "/screenshot.jpg"));
}
catch( Exception e ) {
e.printStackTrace();
}
}
你可以在任何需要的地方使用这种方法。然后,我假设您可以在surefire-reports/html/custom.css中定制maven-surefire-report-plugin的样式表,以便您的报告包含到每个测试的正确截图的链接。