SELENIUM BASICS:


SELENIUM WEBDRIVER:


TESTNG:


REPORTS:


BUILD & CONTINUOUS INTEGRATION TOOLS:


CROSS BROWSER TESTING:


MOBILE TESTING:


AUTOMATION FRAMEWORKS:


API Testing:


INTERVIEW QUESTIONS:


SAMPLE RESUME:

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.

Sharing is caring.

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

Like This Post?

We have a lot more where that came from?

We only send really good stuff occasionally, promise.

Rajkumar

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

  1. in the first code box, you wrote “cactch” instead of catch in the try catch section.

    • Good catch LDionsio. Thanks. Have modified that.

  2. Hi Rajkumar
    thanks a lot ur explanation is very useful for interview purpose

    • Thanks for your kind words Shamsher Khan

  3. 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

    • We can use. I have mentioned two methods in the article.

Leave a Comment

API Testing eBook

DOWNLOAD FOR FREE