IRetryAnalyzer to Retry / Re-Run Failed Test in TestNG

Why failed test cases Re-Run in Selenium again?

  • While working with internal applications, you can see that your test is unnecessarily unsuccessful at different points and once you run the same test again, it passes without any changes.
  • Sometimes case fail due to the minor issue’s so to cross verify exception exist on not exist in Selenium script that time we re-run selenium script.


There are several reasons for the failure of the test:

  • Due to the network problem.
  • Reasons for Application Downtime.
  • Due to load issue and etc.
  • But if the script is failing due to XPath and some legitimate reason, then you have to maintain your script to work again.


How to re-run failed test case in Selenium?

  • It seems very difficult to run the script automatically again but it is not. We need to write a separate Java class which will take care of the failure if you run the test.
  • We will use an IRetryAnalyzer interface which is part of TestNG and we need to override the method of retrying.


Step 1: Create a class with rerun logic.


Step 2: Use annotation in your test program.

  • If Test case fails then using retryAnalyzer annotation use within the @Test then with the help of Retry.class re-run selenium script.


Example:  IRetryAnalyzer to Retry / Re-Run Failed Test in TestNG.

  • with the help of assertEquals verify pass / fail result of an existing test case.


Note: testGenY test case failure due to rerun. With the help of Assertions, we can decide the status of existing class.

Share something valuable to others

Leave a Reply

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