Capturing screenshots with Selenium WebDriver

Selenium WebDriver provides the TakesScreenshot interface to capture a screenshot of a web page. This helps in test runs, showing exactly what happened when an exception or error has occurred during execution, and so on. We can also capture screenshots during verification of element state, values displayed in elements, or state after an action is completed.

Capturing screenshots also helps in the verification of layouts, field alignments, and so on, where we compare screenshots taken during test execution with baseline images.

In this recipe, we will use the TakesScreenshot interface to capture a screenshot of the web page under test.

How to do it...

Let's create a test that will open our test application and take a screenshot of the page in PNG (Portable Network Graphics) format, as shown in the following code example:

@Test
public void testTakesScreenshot() throws Exception {
  File scrFile = ((TakesScreenshot) driver)
      .getScreenshotAs(OutputType.FILE);
  FileUtils.copyFile(scrFile, new File("target/main_page.png"));
}

How it works...

The TakesScreenshot interface provides the getScreenshotAs() method to capture a screenshot of the page displayed in the driver instance. In the following example, we specified OutputType.FILE as an argument to the getScreenshotAs() method, so that it will return the captured screenshot in a file:

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

We can save the file object returned by the getScreenshotAs() method using the copyFile() method of the FileUtils class from the org.apache.commons.io.FileUtils class.

Note

TakesScreenshot relies on the browser API to capture the screenshots. The HtmlUnit driver does not support the TakesScreenshot interface.

There's more...

The OutputType class provides multiple ways to output the screenshot data using the getScreenshotAs() method. In the previous example, we saw a screenshot captured in a file. Screenshots can also be captured in a Base64 string format or in raw bytes. In the following example, a screenshot is captured as Base64 string:

String base64 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BASE64);

Capturing screenshots with RemoteWebDriver/Selenium Grid

While running tests with RemoteWebDriver or Selenium Grid, it is not possible to take the screenshots, as the TakesScreenshot interface is not implemented in RemoteWebDriver.

However, we can use the Augmenter class, which adds the TakesScreenshot interface to the remote driver instance, as shown in the following code example:

driver = new Augmenter().augment(driver);
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\tmp\screenshot.png"));

The Augmenter class enhances the RemoteWebDriver by adding various interfaces to it, including the TakesScreenshot interface:

driver = new Augmenter().augment(driver);

Later, we can use the TakesScreenshot interface from RemoteWebDriver to capture the screenshot.

See also

  • The Capturing screenshots of elements in the Selenium WebDriver recipe in Chapter 9, Extending Selenium
  • The Comparing images in Selenium recipe in Chapter 9, Extending Selenium
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.3.167