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 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 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 fail 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 existing test case.


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

How to Retry Failed Tests in TestNG Automation Framework and How To Run Failed Test Cases Using TestNG In Selenium WebDriver