是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
当前回答
硒化/ Java
以下是Selenide项目是如何做到这一点的,这比任何其他方式都要简单:
import static com.codeborne.selenide.Selenide.screenshot;
screenshot("my_file_name");
JUnit:
@Rule
public ScreenShooter makeScreenshotOnFailure =
ScreenShooter.failedTests().succeededTests();
美国小妞TestNG:
import com.codeborne.selenide.testng.ScreenShooter;
@Listeners({ ScreenShooter.class})
其他回答
我使用下面的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");
}
}
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测试的。
Java
我解决了这个问题。你可以扩展RemoteWebDriver,给它所有的接口,它的代理驱动程序实现:
WebDriver augmentedDriver = new Augmenter().augment(driver);
((TakesScreenshot)augmentedDriver).getScreenshotAs(...); // It works this way
机器人框架
下面是一个使用Robot Framework和Selenium2Library的解决方案:
*** Settings ***
Library Selenium2Library
*** Test Cases ***
Example
Open Browser http://localhost:8080/index.html firefox
Capture Page Screenshot
这将在工作空间中保存一个截图。也可以为关键字Capture Page截图提供一个文件名来改变这种行为。
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);
}