是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
当前回答
Java
对于较新版本的Java, FileUtils函数不起作用。在这里,下面的功能可以完美地复制屏幕截图。
import java.io.File;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.io.FileHandler;
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
File destfile = new File(destination folder /filename.extension);
FileHandler.copy(scrfile, destfile);
其他回答
Java
String yourfilepath = "E:\\username\\Selenium_Workspace\\foldername";
// Take a snapshort
File snapshort_file = ((TakesScreenshot) mWebDriver)
.getScreenshotAs(OutputType.FILE);
// Copy the file into folder
FileUtils.copyFile(snapshort_file, new File(yourfilepath));
C#
using System;
using OpenQA.Selenium.PhantomJS;
using System.Drawing.Imaging;
namespace example.com
{
class Program
{
public static PhantomJSDriver driver;
public static void Main(string[] args)
{
driver = new PhantomJSDriver();
driver.Manage().Window.Size = new System.Drawing.Size(1280, 1024);
driver.Navigate().GoToUrl("http://www.example.com/");
driver.GetScreenshot().SaveAsFile("screenshot.png", ImageFormat.Png);
driver.Quit();
}
}
}
它需要NuGet包:
PhantomJS 2.0.0 硒。支持2.48.2 硒。WebDriver 2.48.2
它是用。net Framework v4.5.2测试的。
你可以使用Webdriverclass对象创建一个webdriver支持的selenium对象,然后你可以截屏。
PowerShell
Set-Location PATH:\to\selenium
Add-Type -Path "Selenium.WebDriverBackedSelenium.dll"
Add-Type -Path "ThoughtWorks.Selenium.Core.dll"
Add-Type -Path "WebDriver.dll"
Add-Type -Path "WebDriver.Support.dll"
$driver = New-Object OpenQA.Selenium.PhantomJS.PhantomJSDriver
$driver.Navigate().GoToUrl("https://www.google.co.uk/")
# Take a screenshot and save it to filename
$filename = Join-Path (Get-Location).Path "01_GoogleLandingPage.png"
$screenshot = $driver.GetScreenshot()
$screenshot.SaveAsFile($filename, [System.Drawing.Imaging.ImageFormat]::Png)
其他司机……
$driver = New-Object OpenQA.Selenium.Chrome.ChromeDriver
$driver = New-Object OpenQA.Selenium.Firefox.FirefoxDriver
$driver = New-Object OpenQA.Selenium.IE.InternetExplorerDriver
$driver = New-Object OpenQA.Selenium.Opera.OperaDriver
Java
我想我应该给出完整的解决方案,因为有两种不同的方法获得截图。一个来自本地浏览器,另一个来自远程浏览器。我甚至将图像嵌入到HTML报告中:
@After()
public void selenium_after_step(Scenario scenario) throws IOException, JSONException {
if (scenario.isFailed()){
scenario.write("Current URL = " + driver.getCurrentUrl() + "\n");
try{
driver.manage().window().maximize(); // Maximize window to get full screen for chrome
}
catch (org.openqa.selenium.WebDriverException e){
System.out.println(e.getMessage());
}
try {
if(isAlertPresent()){
Alert alert = getAlertIfPresent();
alert.accept();
}
byte[] screenshot;
if(false /*Remote Driver flow*/) { // Get a screenshot from the remote driver
Augmenter augmenter = new Augmenter();
TakesScreenshot ts = (TakesScreenshot) augmenter.augment(driver);
screenshot = ts.getScreenshotAs(OutputType.BYTES);
}
else { // Get a screenshot from the local driver
// Local webdriver user flow
screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
}
scenario.embed(screenshot, "image/png"); // Embed the image in reports
}
catch (WebDriverException wde) {
System.err.println(wde.getMessage());
}
catch (ClassCastException cce) {
cce.printStackTrace();
}
}
//seleniumCleanup();
}