How To Run Failed Test Cases Using TestNG In Selenium WebDriver

Run Failed Test Cases Using TestNG in Selenium WebDriver:

At times, test cases may fail while running automated test scripts.  The reason may be anything (say, Network issue, System issue or browser issue) but as an automation tester, you need to execute the test scripts again. Here is a solution to run failed test cases using TestNG in Selenium.

Don’t you want to take a screenshot on failure and attach that in your report?

Yeah, as a tester we need to capture the failed steps. This post guides you on “How To Capture Screenshot and Insert it in Extent Reports

We could execute the failed test cases in two ways.

Case 1: Execute failed test cases using TestNG in Selenium – By using “testng-failed.xml”

Steps To follow:

  1. After the first run of an automated test run. Right click on Project – Click on Refresh
  2. A folder will be generated named “test-output” folder. Inside “test-output” folder, you could find “testng-failed.xml”
  3. Run “testng-failed.xml” to execute the failed test cases again.

Run Failed Test Cases Using TestNG

Case 2: Execute failed test cases using TestNG in Selenium – By Implementing TestNG IRetryAnalyzer.

Create a class to implement IRetryAnalyzer. Here I am creating a class (say, RetryFailedTestCases) and implementing IRetryAnalyzer.

RetryFailedTestCases implements IRetryAnalyzer:

Let’s create another class ‘RetryListenerClass’ by Implementing ‘IAnnotationTransaformer’ interface. transform method is called for every test during test run. A simple implementation of this ‘IAnnotationTransformer’ interface can help us set the ‘setRetryAnalyzer’ for ‘ITestAnnotation’. Add the above class name (RetryFailedTestCases.class) in the below program. This interface does its work in run time by adding annotation to the test methods.

RetryListenerClass implements IAnnotationTransformer:

Let us see the example by executing simple tests below. Here I took two test cases say Test1 and Test2.

Testcase 1:

Testcase 2:

As per the lines of code in Test2, it will fail. So it (Test2) will be executed 2 times (we took the maxRetryCnt as 2) in Retry Class. First lets include below mentioned Listener to testng.xml file. Below mentioned syntax is to add Listener for RetryListnereClass

Final testng.xml file should looks like below:

Execute the testng.xml. Here is the output which I got. You could see in the below mentioned result that the Test 2 is executed three times as we have mentioned ‘maxRetryCnt = 2’. Even though we have just 2 tests, we could find total test runs are 4 in the result.

This way we could run failed test cases using TestNG in Selenium.

Complete TestNG Tutorial With Videos

How To Run Failed Test Cases

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

1 thought on “How To Run Failed Test Cases Using TestNG In Selenium WebDriver”

  1. thanks very helpfull

Leave a Comment

Share via
Copy link
Powered by Social Snap