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

(注:不含硒遥控器)


当前回答

Ruby(黄瓜)

After do |scenario| 
    if(scenario.failed?)
        puts "after step is executed"
    end
    time = Time.now.strftime('%a_%e_%Y_%l_%m_%p_%M')

    file_path = File.expand_path(File.dirname(__FILE__) + '/../../../../../mlife_screens_shot')+'/'+time +'.png'

    page.driver.browser.save_screenshot file_path
end

Given /^snapshot$/ do
    time = Time.now.strftime('%a_%e_%Y_%l_%m_%p_%M')

    file_path = File.expand_path(File.dirname(__FILE__) + '/../../../../../mlife_screens_shot')+'/'+time +'.png'
    page.driver.browser.save_screenshot file_path
end

其他回答

是的,可以使用Selenium WebDriver对网页进行快照。

WebDriver API提供的getScreenshotAs()方法为我们做了这项工作。

语法:getScreenshotAs(OutputType<X> target)

返回类型:X

参数:target—查看OutputType提供的选项

适用性:不特定于任何DOM元素

例子:

    TakesScreenshot screenshot = (TakesScreenshot) driver;
    File file = screenshot.getScreenshotAs(OutputType.FILE);

有关详细信息,请参阅下面的工作代码片段。

    public class TakeScreenShotDemo {

        public static void main(String[] args) {
            WebDriver driver = new FirefoxDriver();
            driver.manage().window().maximize();
            driver.get(“http: //www.google.com”);

            TakesScreenshot screenshot = (TakesScreenshot) driver;

            File file = screenshot.getScreenshotAs(OutputType.FILE);

            // Creating a destination file
            File destination = new File(“newFilePath(e.g.: C: \\Folder\\ Desktop\\ snapshot.png)”);
            try {
                FileUtils.copyFile(file, destination);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

访问Snapshot using WebDriver获取更多详细信息。

塞勒尼斯

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

你可以使用Webdriverclass对象创建一个webdriver支持的selenium对象,然后你可以截屏。

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

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