是否可以使用Selenium WebDriver进行截图?

(注:不含硒遥控器)


当前回答

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');
    }
    ...
}

其他回答

你可以尝试一下shot API。它在GitHub上。

测试示例。

塞勒尼斯

captureEntirePageScreenshot | /path/to/filename.png | background=#ccffdd

c#代码

IWebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((ITakesScreenshot)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"));

C#

public void TakeScreenshot()
{
    try
    {            
        Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
        ss.SaveAsFile(@"D:\Screenshots\SeleniumTestingScreenshot.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
        throw;
    }
}

我使用下面的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");
        }
    }