Capture Screenshot: How Selenium WebDriver Takes Screenshot?

Category: Selenium WebDriver 31 0

Once upon a time, I was a manual functional tester. So, I had to keep proof of all the testing activities in the form of screenshots. Functional testing was quite simple to do with ease, but, taking screenshots of each step and saving them in doc format was cumbersome activity. I really hate to capture screenshot; the main reason was the system was quite slow in VDI and we need to prepare QA doc.

Later, I moved to Automation testing with Selenium WebDriver. It really fascinated me when I came across that Selenium could capture screenshot of automation testing activities. I took a deep breath and whispered thank you Selenium for saving me. 🙂

Further with the advancement in Automation framework the purpose to capture screenshot varies, so let’s discuss them below:

Don’t Miss: How to handle date picker Calendar in Selenium WebDriver? 

Purpose to capture screenshot in Selenium WebDriver

  • Taking a screenshot of failed test cases or steps
  • To capture screenshot of the entire testing processes
  • Capturing the screenshot of the important checkpoints
  • To capture screenshot when the server has an awkward response
  • It could be done when there are assertion failure and application issues
  • When timeout exceptions occur then Selenium could capture screenshot
  • Selenium capture screenshot when WebDriver is unable to find the web elements

Taking screenshot has the major advantage as in the form of failure analysis. It could be bug analysis, downtime analysis, etcetera.

Concepts needed to implement capture screenshot command with WebDriver

Initially, the automation engineer tries to memorize the commands and later they get confused. Therefore, I would suggest you go through the basic concept so that you don’t find any difficulties while implementation.

Java Type Casting and File creation is the basic concept behind the screenshot capture command of Selenium. Just glued to these two concepts then the command will be automatically stored in your mind.

capture screenshot in Selenium
There is no connection of this image with the content 🙂

Takes Screenshot command to capture screenshot in Selenium

Here is the command with their explanation to capture screenshot in WebDriver.

Step 1: Type cast WebDriver interface

This command basically performs typecasting of WebDriver interface to TakesScreenshot interface.

Step 2: Declaration of the source file

We declare source file by calling the getScreenshotAs method and we put File type as its argument. This method resides within the TakesScreenshot interface.

Step 3: Declaration of the destination file

Now we should define the local destination folder to copy the screenshot from a source file.

Step 4: Copy source file to destination file (folder)

Above command is basically copying the source file to the local destination folder.

If you develop the concept like this, then you don’t have to mug up everything. Let’s implement capture screenshot scenario in a simple test case.

Implementation of TakesScreenshot to capture screenshot in Selenium WebDriver

Scenario: This test case is to verify the proper loading of http://www.inviul.com homepage.

Sample code

Explanation

WebDriver simple opens the homepage and wait for the elements to load. Once homepage is loaded then it takes the screenshot and stores to the destination folder.

I believe this article will help you when you need to implement screenshot capture command in your automation test cases. If you have any queries, then don’t hesitate to share in our Facebook group. If you have not joined yet, then click on below image to join now.

Join Inviul fb group

Related Articles

Add Comment