Thursday, 7 February 2019

Retry failed test case in testng Selenium

How can we execute failed test cases in Selenium using TestNG


There are multiple ways to execute Failed test cases in Selenium using TestNG:
  • 'testng-failed.xml'
  • IRetryAnalyzer

testng-failed.xml


If any test case is failed, testng-failed.xml is generated in the reports output directory(Target directory). We can simply run above xml to rerun the failed test cases.

Location of  testng-failed.xml




Running testng-failed.xml
In eclipse - Simply right click on xml file and run as testng


IRetryAnalyzer


TestNG has an Interface to implement to be able to have a chance to retry a failed test that is IRetryAnalyzer.

If you want TestNG to automatically retry a test whenever it fails. You can use a retry analyzer.
To retry a failed test you have bind retry analyzer to each test which you want to invoke on test failure.

How can we bind retry analyzer at Test Level

You can use retryAnalyzer attribute and assign it to implemented class of IRetryAnalyzer interface at test level. As shown below:

@Test(retryAnalyzer = RetryAnalyzerImpl.class)

After binding it to the test level, TestNG automatically invokes the retry analyzer to determine if TestNG can retry a test case again in case of failure for a specified number of times.

Complete implementation of IRetryAnalyzer class is shown below:



Sample Test class implementation is shown below:


Setup RetryAnalyzer at Class/Suite Level


We can setRetryAnalyzer in setup method - @BeforeClass, @BeforeSuite instead of @Test as shown in RetryTest.java

Sample Test class implementation is shown below:


No comments:

Post a Comment