Using listener Re run failed test cases in selenium automatically

In this article, we are going to use new Interfaces today which will allow you to Re run failed test cases in selenium if the test fails.

I have used this multiple times in my scripts and its helps me a lot. Hope this will be helpful for you as well.

Re run failed test cases in selenium

Why Re run failed test cases in selenium if it is failing?

While working with internal applications, you may notice that your test is failing unnecessarily at different point and once you re-run the same test it passes without any changes

There are multiple reasons why the test fails.

  • Due to the network issue.
  • Due to application downtime.
  • Due to loading issue and etc.

But if the script is failing due to xpath and some valid reason then you have to maintain for re work on your scripts.

This is one of the interview questions as well so make sure you are able to implement below scenario.

I have already updated 100 Selenium Interview question so if you are looking for job change then have a look.

100 Selenium Webdriver interview questions.


What or how we Re run failed test cases in selenium in case of Failure?

It sounds very tough to re-run scripts automatically but it is not. We need to write separate JAVA class that will take care of running your test if failure.

We will be using one IRetryAnalyer interface that is part of TestNG and we need to override retry method.

Read official documentation about IRetryAnalyzer

Create separate Class which will implement IRetryAnalyer interface



Now we are done almost only we need to specify this in the test case.


In above statement, we are giving instruction to our test case that if the test case fails then it will call Retry class that we created above.


Program to Re run failed test cases in selenium



When you run above program you will notice that our test will run 4 times.

 The first time it will run normally and three times more due to Retry class that we have written.


Hope this article will help. Please comment below if any issue or doubt in this article.

Have a nice day 🙂





  1. Sandeep
  2. Jagadeesh M