How To Capture Screenshot of Failed Test Cases Using Selenium WebDriver

How To Capture Screenshot of Failed Test Cases Using Selenium WebDriver:

Earlier I have posted a detailed post on how to capture a screenshot using Selenium WebDriver. If you have missed it, you could check the detailed post on how to capture screenshot using Selenium WebDriver.

If a script fails, we need to know where was the error in script. Solution for this is to capture a screenshot of webpage when the test case fails. We could easily identify where exactly the script got failed by seeing the screenshot.

To achieve this, we could place the entire code in try-catch block. Which means placing the test steps in try block and screen capture statement in catch block. If a test step fails in the try block then it goes to the catch block and capture a screenshot of the web page.

Below mentioned script shows how to capture a screenshot of failed test cases using Selenium WebDriver.

Another way of capturing screenshot of failed test cases using Selenium WebDriver is to use ITestResult Interface.

ITestResult interface provides the test case execution status and test case name

We do place the screen capture steps in a separate method using AfterMethod TestNG Annotation. If we use TestNG AfterMethod annotation then this particular method (AfterMethod) executes after every test execution.

Below mentioned script shows how to capture a screenshot using ITestResult Interface.

If you are not regular reader of my blog then I highly recommend you to signup for the free email newsletter using the below link.

Rajkumar SM is a founder of SoftwareTestingMaterial. He is a certified Software Test Engineer by profession and blogger & youtuber by choice. He has an extensive experience in the field of Software Testing. He writes here about Software Testing which includes both Manual and Automation Testing. He loves to be with his wife and cute little kid 'Freedom'.

6 thoughts on “How To Capture Screenshot of Failed Test Cases Using Selenium WebDriver”

  1. Hi raj,,

    when you are using ITestResult interface we need to implement that interface right…with out implementing interface can we directly use in screenshot method

    Thanks

    Reply

Leave a Comment

Share via
Copy link
Powered by Social Snap