Input your search keywords and press Enter.

How to Retry Failed Test Cases in Selenium using TestNG?

Retry failed test cases banner

We have discussed lots more features of TestNG so far. Today we are going to discuss one of the most important features of the TestNG which helps to retry failed test cases. You must be happy as this technique will automatically trigger the failed test cases only, so you do not need to bother about sorting failed test cases and running it manually. At first, let’s discuss some of the reasons for the failure of the test case.

Why do test cases fail in Automation run?

If the reason of failure is not an authentic failure reason, then the following reasons could be the one:

  • Network downtimes
  • The browser didn’t open
  • Web server didn’t respond
  • Network is slow
  • The page is taking time to load
  • Elements are not visible
  • Web page loaded but UI messed up

These are some of the reasons for test case failure when failure is not about a mismatch in expected and actual results. It’s worthless if you manually trigger tests when failed due to above reasons. We have observed that above test failure reasons generally occur at a transient time, hence, if we try to execute test again then there are positive chances that the test will get passed or if failed then the reason would the authentic one. Therefore, to reduce the number of test failure by triggering the retry of failed test cases is one of the goals to implement this technique in our Selenium project.

Retry failed test cases banner

How to Retry Failed Test Cases?

Now the question comes how can we retry failed test cases?

We have two solutions to this question and TestNG better addresses the solution. But, before we discuss the techniques, I’d like to recommend you our tutorial on TestNG Listener. Click below to read the tutorial.

Technique# 1: By running testng-failed.xml file

When any test gets failed then TestNG create a separate XML file in which it lists all the failed tests. A folder gets added to our Selenium project after the run and the folder name is, test-output. When you expand this folder, you find a file called testng-failed.xml. Here is the folder structure:

testng-failed

You just run this XML file like you run your testng.xml and you will see all the failed test cases start running. Isn’t it interesting? 😊

Technique# 2: By implementing IRetryAnalyzer

What is IRetryAnalyzer?

IRetryAnalyzer is an interface in which there is definition to trigger failed test cases found. We can further expand that method which triggers failed test cases through the implementation of the IRetryAnalyzer interface.

Here is the sample implementation:

Steps to implement the technique to Retry failed test cases through IRetryAnalyzer

Step# 1: Implement IRetryAnalyzer

First, implement IRetryAnalyzer and override the method written inside it. Following is the below code:

In the above code, we just define initial retry count and maximum retry count. Further, retry of the failed test cases only occur when retryCount is always less than the maRetryCount.

Step# 2: Implement IAnnotationTransformer

IAnnotationTransformer is another interface which set the value to the previous class where IRetryAnalyzer was implemented. Here is the code:

Step 3: Design your tests in TestNG class file

Here is the sample test automation program.

In this program, we use Assert command to verify the presence of some text in the title of the web page. It will fail on mismatch.

Step# 4: Now add these listeners to testng.xml file

Earlier we have seen how we added listener file in testng.xml file. We can follow similar steps here too. Following is the statement which adds Retry listeners to the testng.xml file.

Console Output

Retry Failed Test cases Console Output

In the above console output, we see Retry of failed test cases got triggered.

That’s all about the techniques to retry failed test cases in Selenium using TestNG’s IRetryAnalyzer. You can post your queries in a comment below and don’t forget to join our Facebook group.

Join Inviul fb group

Avinash Mishra is the author of Inviul blog. He is a software engineer and blogger by choice. He loves to write blogs, and apart from blogging, he is interested in documentary film making, listening to music, traveling around the world and philanthropic activities.

Leave a Reply

Your email address will not be published. Required fields are marked *