简体   繁体   中英

How to avoid Retry adding in Test annotation?

I came across an answer on how to use Retry in Selenium web driver test. Now I have implemented Retry class under utilities and inside the test annotation given as follows @Test(retryAnalyzer = Retry.class) . This is working fine now, but I would like to change this way, can I use the Retry in any other way instead of giving in test annotation as @Test(retryAnalyzer = Retry.class) ? Also I need to comment the @Test(enabled = true) , Can someone please advise?

utilities/Retry class

public class Retry implements IRetryAnalyzer {

     private int retryCount = 0;
     private int maxRetryCount = 2;
     
     public boolean retry(ITestResult result) {
         if(retryCount < maxRetryCount) {
             retryCount ++ ;
             return true;
         }
         return false;
     }
}

tests/RepaymentCalculatorTest

public class RepaymentCalculatorTest extends BaseTest {
    
    Retry retryTest = new Retry();
    //@Test(enabled = true)
    @Test(retryAnalyzer = Retry.class)
    public void loanRepaymentCalculator() throws InterruptedException {

     // rest of the UI test code added here ....
}

i am using bit different way, no need to add class in @Test annotation

Lets have custom Listener for it

public class RetryFailedTestCases implements IRetryAnalyzer, IAnnotationTransformer{

public void transform(ITestAnnotation testannotation, Class testClass, Constructor testConstructor,
        Method testMethod) {
    testannotation.setRetryAnalyzer(RetryFailedTestCases.class);
}

int counter = 0;
int retryLimit = 1; //as per need

@Override
public boolean retry(ITestResult result) {
    
    if(counter < retryLimit)
    {
        counter++;
        return true;
    }
    
    return false;
   }

 }

Now, add this in testng.xml file

<listeners>
  <listener class-name="package.RetryFailedTestCases"/>
</listeners>

this is simple and helpful when we run as suite.. alternative we can mentioned listener class on top of each class instead of testng.xml file

i am not sure why explicitly mentioned enabled = true? even if not @Test execute right? if you dont want to execute then mention with false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM