2

I am using RetryAnalyzer in selenium code, wherein if a test case fails in first execution, retryAnalyzer would again execute it. In my case, if first time execution fails and second time execution passes, I want to show the results of only second execution in Extent Reports. But I am getting both the test case results in the report.

Below is my code.

ExtentTest test;
int maxRetryCount = 1;
@AfterMethod
protected void afterMethod(ITestResult result) {
boolean firstTry = true;
if(maxRetryCount>0){
    if (result.getStatus() == ITestResult.FAILURE && firstTry == true) { 
        firstTry = false;
    } else if (result.getStatus() == ITestResult.SKIP) {
        test.log(LogStatus.SKIP, "Test skipped " + result.getThrowable());
    } else if (result.getStatus() == ITestResult.FAILURE && firstTry == false) {
        test.log(LogStatus.ERROR, test.addScreenCapture(imgPath));
        test.log(LogStatus.FAIL, result.getThrowable());
    } else {
        test.log(LogStatus.PASS, "Test passed");
    }
}else{
    if (result.getStatus() == ITestResult.FAILURE) { 
        test.log(LogStatus.ERROR, test.addScreenCapture(imgPath));
        test.log(LogStatus.FAIL, result.getThrowable());
    } else if (result.getStatus() == ITestResult.SKIP) {
        test.log(LogStatus.SKIP, "Test skipped " + result.getThrowable());
    } else {
        test.log(LogStatus.PASS, "Test passed");
    }
}
}

in this case, if the test case fails in first execution, it shows the status of that test case as "unknown", and suppose the execution passes in retry(second time execution), the pass percentage is shown as 50%, not 100%, because it counts the unknown's percentage as well.

What changes should i do in below loop condition, so that result of this test case are not shown in the report.

if (result.getStatus() == ITestResult.FAILURE && firstTry == true) { }

Please suggest.

Craig
  • 7,471
  • 4
  • 29
  • 46
Nidhi Midha
  • 33
  • 2
  • 5

1 Answers1

0

You can remove the current test node if the test status is Skipped. Something like this will work...

@BeforeMethod(alwaysRun=true)
public void beforeMethod(){
   //be sure to create node in the BeforeMethod
   HtmlReporter.createNode("CurrentTest");
}


@AfterMethod(alwaysRun = true)
public void afterMethod(ITestResult result) {
 if (result.getStatus() == ITestResult.SKIP) {
    HtmlReporter.removeCurrentNode();           
}

The code to remove the current node would look like:

public static synchronized void removeCurrentNode() {
        _report.removeTest(getNode());
    }
 public static synchronized ExtentTest getNode() {
    return extentTestMap.get("node_" + Thread.currentThread().getId());
}
HRVHackers
  • 2,793
  • 4
  • 36
  • 38