The Issue

One salient feature of test automation is the ability to trace the origin of a failed test. If the root cause of the failed test is known beforehand, the user should be able to effectively debug the issue without spending on time trying to figure out the origin of the issue. If the user does not know the reason of failure, they will have to take time to debug the issue, which can be an exponentially increasing time-sink.

One way to track failure in automated tests is by including assertion/validation statements. But often, the reason for a failed scenario is not apparent and a visual is often necessary to illustrate the reason for a failed test.

The Answer

By capturing screenshots at the time of failure, a function that is provided by Selenium Webdriver, we have the potential to quickly discover the root of an error.

The Code

The “.save_screenshot” method saves the screenshot in the current directory where the test is running. “3qihomepage.png” is the name of the image captured. In the above example, the screenshot is captured when the text extracted from the page source does not match with the ‘Hello’ text.

The Result

We can use this code and run the automated test script which will produce the following results:

  • Browser opens
  • Browser navigates to the “3Qi Labs website”
  • Screenshot of the current browser window is captured

The Takeaway

It is easy to take screenshots in Selenium with the help of a few simple lines of code. Screenshots can be used for more effecient interpretation, reporting purposes, and debugging – which will ultimately save a lot of time and yield significant benefits when running automated tests.