Time for action – capturing images as base64 strings - Selenium

Imagine that you want to take a screenshot on Selenium Grid. When you take the screenshot, you will not want it to be saved to the hard drive of the Selenium Grid node.

You will want it to be moved back to where your tests are, especially if you are using it with a Continuous Integration Box.

  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); String screenshotBase64 = ((Screenshot)driver).getScreenshotAs(base64);
  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 managed to take a screenshot and have it returned as a base64 string. This will allow us to take a screenshot on a remote machine and then transport the resultant image back to where the test is being run from.

Saving the screenshot to bytes

Now that we have had a look at capturing screenshots to base64 strings, let's have a look at capturing them as bytes. Having them as bytes means that we can transform them into a number of different things as we see fit.

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

Selenium Topics