是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
当前回答
Python
每个WebDriver都有一个. save_截图(filename)方法。所以对于Firefox,它可以这样使用:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.google.com/')
browser.save_screenshot('screenie.png')
令人困惑的是,.get_screenshot_as_file(filename)方法也存在,它做同样的事情。
还有一些方法:.get_screenshot_as_base64()(用于嵌入HTML)和.get_screenshot_as_png()(用于检索二进制数据)。
注意,WebElements有一个.截图()方法,它的工作原理类似,但只捕获所选元素。
其他回答
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');
}
...
}
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"));
塞勒尼斯
captureEntirePageScreenshot | /path/to/filename.png | background=#ccffdd
C# (Ranorex API)
public static void ClickButton()
{
try
{
// code
}
catch (Exception e)
{
TestReport.Setup(ReportLevel.Debug, "myReport.rxlog", true);
Report.Screenshot();
throw (e);
}
}
Python
每个WebDriver都有一个. save_截图(filename)方法。所以对于Firefox,它可以这样使用:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.google.com/')
browser.save_screenshot('screenie.png')
令人困惑的是,.get_screenshot_as_file(filename)方法也存在,它做同样的事情。
还有一些方法:.get_screenshot_as_base64()(用于嵌入HTML)和.get_screenshot_as_png()(用于检索二进制数据)。
注意,WebElements有一个.截图()方法,它的工作原理类似,但只捕获所选元素。