Time for action – saving a screenshot to file - Selenium

In this section we will have a look at how we can go about saving a file to disk. This is the most common thing that people do when saving screenshots. One thing to note is that if you are using RemoteWebDriver, this will save the file on the same machine as the Selenium Server.

  1. Open up Intellij and create a new Java test class.

  2. We will now add a new line for taking a screenshot:

    driver.get(http://www.wisdomjobs.com); File savedImage = ((Screenshot)driver).getScreenshotAs(file);
  3. If you set a breakpoint on the previous line, you will be able to see what the string looks like.

What just happened?

We have just seen what is probably going to be the most common way to save screenshots when we take them. When we take the screenshot, the image is saved to disk and we are returned the file object that has access to that image.

If you would like to move the file when it is created, you can use the following code snippet:

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Selenium Topics