Time for action – saving images to bytes - Selenium

Imagine that you want to do some in-depth analysis of the UI by taking screenshots. This is something that has been done a number of times in different projects. For example, you take a screenshot, then make changes, and then take more screenshots along the way.

  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://book.theautomatedtester.co.uk); Bytes screenbytes = ((Screenshot)driver).getScreenshotAs(bytes);
  3. If you set a breakpoint on the previous line, you will be able to see what the string looks like.

What just happened?

Now that we have seen what it takes to take a screenshot of the page from the browser, we have managed to take a screenshot and then push the result into a bytes variable. We can then perform histogram type checks against the bytes and anything else that we want.

We can also push the bytes into a stream to save it to file or we can have a look at taking screenshots straight to files.

Saving screenshots to files

Saving screenshots to file is probably the most common way to save a file. This approach will save the file to disk straight away. When we save the screenshot as a file, we are returned a file object.

We can then use it straight away to do anything like getPath() or do what we need.


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

Selenium Topics