Take Screenshots

In cases where you would want to automatically take screenshots, be it to file a bug when an assert condition fails, or just to document how the website looks on different screens, you can take screenshots from within the tests.

You can save these screenshots to the machine that runs your automated tests. If you are on a CI/CD setup, make sure you transfer these screenshots before you wind down the machine.

Here is how you take a screenshot and save it to your machine:

// Import the relevant packages

// ... your test code

// Take a screenshot and save it to /localtion/to/screenshot.png
driver = (RemoteWebDriver) new Augmenter().augment(driver);
File srcFile = (File) ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile, new File("/location/to/screenshot.png"));

// ... your test code
var fs = require('fs');

webdriver.WebDriver.prototype.saveScreenshot = function(filename) {
  return driver.takeScreenshot().then(function(data) {
      fs.writeFile(filename, data.replace(/^data:image\/png;base64,/,''), 'base64', function(err) {
          if(err) throw err;
      });
  })
};

driver.saveScreenshot('snapshot1.png')
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing.Imaging;

using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Firefox;

namespace SeleniumTest
{
  public class ScreenShotRemoteWebDriver : RemoteWebDriver, ITakesScreenshot
  {
    public ScreenShotRemoteWebDriver(Uri uri, DesiredCapabilities dc)
      : base(uri, dc)
    {
    }

    public Screenshot GetScreenshot()
    {
      Response screenshotResponse = this.Execute(DriverCommand.Screenshot, null);
      string base64 = screenshotResponse.Value.ToString();
      return new Screenshot(base64);
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      ScreenShotRemoteWebDriver driver;
      DesiredCapabilities capability = new DesiredCapabilities();
      capability.SetCapability("browser", "Firefox");
      capability.SetCapability("browser_version", "34.0");
      capability.SetCapability("os", "windows");
      capability.SetCapability("os_version", "7");
      capability.SetCapability("name", "Bstack-[CSharp] Screenshot Test");
      capability.SetCapability("build", "screenshot test");
      capability.SetCapability("browserstack.debug", "true");
      capability.SetCapability("browserstack.user", "YOUR_USERNAME");// add username
      capability.SetCapability("browserstack.key", "YOUR_ACCESS_KEY"); //add automate-key


      driver = new ScreenShotRemoteWebDriver(
        new Uri("https://hub-cloud.browserstack.com/wd/hub/"), capability
      );
      driver.Navigate().GoToUrl("https://www.google.com/ncr");
      Console.WriteLine(driver.Title);

      IWebElement query = driver.FindElement(By.Name("q"));
      query.SendKeys("Browserstack");
      query.Submit();
      Console.WriteLine(driver.Title);
      Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
      ss.SaveAsFile("<file-path>//ss.png", System.Drawing.Imaging.ImageFormat.Png);

      driver.Quit();
    }
  }
}
file_put_contents('screenshot.png', $web_driver->takeScreenshot());
driver.save_screenshot('screenshots.png')
driver.save_screenshot("screenshots.png")
open(FH,'>','screenshot.png');
binmode FH;
my $png_base64 = $driver->screenshot();
print FH MIME::Base64::decode_base64($png_base64);
close FH;

We're sorry to hear that. Please share your feedback so we can do better






Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback