For better Reporting Capture Screenshot in Selenium Webdriver

Hello welcome to Selenium tutorials, in this post we will see how to capture Screenshot in Selenium Webdriver

In automation, it is mandatory to take the screenshot for verification so that can prove also that your test case has covered certain functionality or not.Screenshots help you a lot when your test case will fail so that you can identify what went wrong in your script or in your application.

Recently I used one of the reports called Extent Report which helps to attach the screenshot to the report which actually make my reporting better than traditional TestNG reporting.

Screenshot in Selenium Webdriver

I also implemented screenshot on failure in my framework which makes my test more robust so I would recommend you to implement the same in your framework too.

 

 

I have created a youtube video on same. 

 

How to capture Screenshot in Selenium webdriver

For taking  screenshots Selenium has provided TakesScreenShot interface in this interface you can use getScreenshotAs method which will capture the entire screenshot in form of file then using FileUtils we can copy screenshots from one location to another location

Scenario – Open Google  and take screenshot

Let’s implement the same

How to capture Screenshot in Selenium Webdriver

 

 

 

 

If you use above code then it will take screenshot and will store in C:/selenium and screenshot name will be error.png

 

Now consider a scenario where you have to take multiple screenshots then above code will be repetitive so for this we will create a small method which captures screenshots and will call this method from our script.

Example-

 

 

 

So this method we can call from our test case and call multiple time.

Copy the full code and try to execute in Eclipse

package com.bog.htmldemo;

Complete program for Screenshot in Selenium Webdriver

 

 

So in above program you can see I called captureScreenshot method three times like this you can create reusable actions in your script and you can use the same.