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();
}
}
}
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的样式表,以便您的报告包含每个测试的正确屏幕截图的链接?
16
PHP(PHPUnit)
使用PHPUnit_Selenium扩展版本1.2.7:
class MyTestClass extends PHPUnit_Extensions_Selenium2TestCase {
...
public function screenshot($filepath) {
$filedata = $this->currentScreenshot();
file_put_contents($filepath, $filedata);
}
public function testSomething() {
$this->screenshot('/path/to/screenshot.png');
}
...
}
10
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)
After do |scenario|
if(scenario.failed?)
puts "after step is executed"
end
time = Time.now.strftime('%a_%e_%Y_%l_%m_%p_%M')
file_path = File.expand_path(File.dirname(__FILE__) + '/../../../../../mlife_screens_shot')+'/'+time +'.png'
page.driver.browser.save_screenshot file_path
end
Given /^snapshot$/ do
time = Time.now.strftime('%a_%e_%Y_%l_%m_%p_%M')
file_path = File.expand_path(File.dirname(__FILE__) + '/../../../../../mlife_screens_shot')+'/'+time +'.png'
page.driver.browser.save_screenshot file_path
end
6
Java(机器人框架)
我用这种方法进行屏幕截图 .
void takeScreenShotMethod(){
try{
Thread.sleep(10000)
BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "jpg", new File("./target/surefire-reports/screenshot.jpg"));
}
catch(Exception e){
e.printStackTrace();
}
}
您可以在需要的地方使用此方法 .
5
C#
public Bitmap TakeScreenshot(By by) {
// 1. Make screenshot of all screen
var screenshotDriver = _selenium as ITakesScreenshot;
Screenshot screenshot = screenshotDriver.GetScreenshot();
var bmpScreen = new Bitmap(new MemoryStream(screenshot.AsByteArray));
// 2. Get screenshot of specific element
IWebElement element = FindElement(by);
var cropArea = new Rectangle(element.Location, element.Size);
return bmpScreen.Clone(cropArea, bmpScreen.PixelFormat);
}
4
C#
您可以使用以下代码段/功能来获取selenium的屏幕截图:
public void TakeScreenshot(IWebDriver driver, string path = @"output")
{
var cantakescreenshot = (driver as ITakesScreenshot) != null;
if (!cantakescreenshot)
return;
var filename = string.Empty + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond;
filename = path + @"\" + filename + ".png";
var ss = ((ITakesScreenshot)driver).GetScreenshot();
var screenshot = ss.AsBase64EncodedString;
byte[] screenshotAsByteArray = ss.AsByteArray;
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
ss.SaveAsFile(filename, ImageFormat.Png);
}
import org.openqa.selenium.OutputType as OutputType
import org.apache.commons.io.FileUtils as FileUtils
import java.io.File as File
import org.openqa.selenium.firefox.FirefoxDriver as FirefoxDriver
self.driver = FirefoxDriver()
tempfile = self.driver.getScreenshotAs(OutputType.FILE)
FileUtils.copyFile(tempfile, File("C:\\screenshot.png"))
4
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);
}
public class Screenshot{
final static String ESCAPE_PROPERTY = "org.uncommons.reportng.escape-output";
public static String imgname = null;
/*
* Method to Capture Screenshot for the failures in Selenium with TestName and Timestamp appended.
*/
public static void getSnapShot(WebDriver wb, String testcaseName) throws Exception {
try {
String imgpath=System.getProperty("user.dir").concat("\\Screenshot\\"+testcaseName);
File f=new File(imgpath);
if(!f.exists()) {
f.mkdir();
}
Date d=new Date();
SimpleDateFormat sd=new SimpleDateFormat("dd_MM_yy_HH_mm_ss_a");
String timestamp=sd.format(d);
imgname=imgpath+"\\"+timestamp+".png";
//Snapshot code
TakesScreenshot snpobj=((TakesScreenshot)wb);
File srcfile=snpobj.getScreenshotAs(OutputType.FILE);
File destFile=new File(imgname);
FileUtils.copyFile(srcfile, destFile);
}
catch(Exception e) {
e.getMessage();
}
}
2
Java
对的,这是可能的 . 以下示例使用Java:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
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("c:\\tmp\\screenshot.png"));
import java.io.File;
import java.net.URL;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class Testing {
public void myTest() throws Exception {
WebDriver driver = new RemoteWebDriver(
new URL("http://localhost:4444/wd/hub"),
DesiredCapabilities.firefox());
driver.get("http://www.google.com");
// RemoteWebDriver does not implement the TakesScreenshot class
// if the driver does have the Capabilities to take a screenshot
// then Augmenter will add the TakesScreenshot methods to the instance
WebDriver augmentedDriver = new Augmenter().augment(driver);
File screenshot = ((TakesScreenshot)augmentedDriver).
getScreenshotAs(OutputType.FILE);
}
}
1
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));
30 回答
Python
它会将截屏保存在保存脚本的同一目录中 .
Ruby
C#
需要NuGetPackages:
PhantomJS 2.0.0
Selenium.Support 2.48.2
Selenium.WebDriver 2.48.2
使用.NETFramework v4.5.2进行测试
Python
您可以使用python web驱动程序从Windows捕获图像 . 使用下面的代码需要捕获屏幕截图
Java
使用这两种方法,您也可以拍摄日期和时间的屏幕截图 .
C#
Java
我解决了这个问题 . 您可以扩充
RemoteWebDriver
以为其代理驱动程序实现的所有接口提供:Ruby
更多文件类型和选项可用,您可以在takes_screenshot.rb中看到它们
Python
每个WebDriver都有一个
.save_screenshot(filename)
方法 . 所以对于Firefox,它可以像这样使用:令人困惑的是,
.get_screenshot_as_file(filename)
方法也存在同样的事情 .还有以下方法:
.get_screenshot_as_base64()
(用于嵌入html)和.get_screenshot_as_png()
(用于检索二进制数据) .请注意,WebElements具有类似的
.screenshot()
方法,但只捕获所选元素 .Java
使用RemoteWebDriver,在使用屏幕截图功能扩充Node之后,我会将屏幕截图存储起来:
您可以在需要的地方使用此方法 . 然后,我假设您可以在surefire-reports / html / custom.css中自定义maven-surefire-report-plugin的样式表,以便您的报告包含每个测试的正确屏幕截图的链接?
PHP(PHPUnit)
使用PHPUnit_Selenium扩展版本1.2.7:
PowerShell
其他司机......
红宝石(黄瓜)
Java(机器人框架)
我用这种方法进行屏幕截图 .
您可以在需要的地方使用此方法 .
C#
C#
您可以使用以下代码段/功能来获取selenium的屏幕截图:
Java
Java
这里似乎缺少 - 在Java中截取 specific element 的屏幕截图:
Jython
C#
C#(Ranorex API)
JavaScript(Selenium-Webdriver)
Selenese
JAVA
捕获Selenium中的故障的屏幕截图的方法,其中附加了TestName和Timestamp .
Java
对的,这是可能的 . 以下示例使用Java:
Java
我无法得到已接受的工作答案,但根据the current WebDriver documentation,以下在OS X 10.9上使用Java 7对我来说很好:
Java
希望这能解决你的问题
PHP
Java
以为我会提供完整的解决方案,因为有两种不同的方式来获取屏幕截图 . 一个来自本地浏览器,一个来自远程浏览器 . 我甚至将图像嵌入到html报告中
您可以试试AShot API . 这是相同的github链接 .
https://github.com/yandex-qatools/ashot
这里的一些测试......
https://github.com/yandex-qatools/ashot/tree/master/src/test/java/ru/yandex/qatools/elementscompare/tests