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

(注:不含硒遥控器)


当前回答

/**
 * Take a screenshot and move to the given folder location.
 *
 * @param driver
 * @param folderLocation
 * @return screenShotFilePath
 */
public static String captureScreenshot(WebDriver driver, String folderLocation) {

    // Variable to store screenshot's file path.
    String screenShotFilePath = null;

    // Generate unique id for screen shot name.
    String uniqueId = UUID.randomUUID().toString().substring(31);

    if (driver != null) {

        // Generate screenshot as a file
        File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

        // New screenshot file path with having file name
        screenShotFilePath = folderLocation + File.separator + uniqueId + ".png";

        // Move file to the destination location.
        FileUtils.moveFile(scrFile, new File(screenShotFilePath));
    }

    return screenShotFilePath;
}

其他回答

我使用下面的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#

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;
    }
}

Python

def test_url(self):
    self.driver.get("https://www.google.com/")
    self.driver.save_screenshot("test.jpg")

它将在脚本保存的同一目录下保存一个截图。

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

我解决了这个问题。你可以扩展RemoteWebDriver,给它所有的接口,它的代理驱动程序实现:

WebDriver augmentedDriver = new Augmenter().augment(driver);
((TakesScreenshot)augmentedDriver).getScreenshotAs(...); // It works this way