是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
当前回答
Java
public void captureScreenShot(String obj) throws IOException {
File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotFile, new File("Screenshots\\" + obj + "" + GetTimeStampValue() + ".png"));
}
public String GetTimeStampValue()throws IOException{
Calendar cal = Calendar.getInstance();
Date time = cal.getTime();
String timestamp = time.toString();
System.out.println(timestamp);
String systime = timestamp.replace(":", "-");
System.out.println(systime);
return systime;
}
使用这两种方法,您还可以拍摄带有日期和时间的屏幕截图。
其他回答
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);
}
PHP
public function takescreenshot($event)
{
$errorFolder = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . "ErrorScreenshot";
if(!file_exists($errorFolder)){
mkdir($errorFolder);
}
if (4 === $event->getResult()) {
$driver = $this->getSession()->getDriver();
$screenshot = $driver->getWebDriverSession()->screenshot();
file_put_contents($errorFolder . DIRECTORY_SEPARATOR . 'Error_' . time() . '.png', base64_decode($screenshot));
}
}
JAVA
嗨,你可以在selenium中截图,这里是下面给出的代码,你可以在任何selenium项目中创建截图,我们也可以在aws中为失败的场景生成截图。
public boolean takeScreenshot(final String name) {
String screenshotDirectory = System.getenv("WORKING_DIRECTORY");
File screenshot =
((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
return screenshot.renameTo(new File(screenshotDirectory,
String.format("%s.png", name)));
}
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对象,然后你可以截屏。