Quality is delighting customers
Hello,
I am new to this site. I am posting my first query.
How to re-run failed test cases in Selenium WebDriver?
Tags:
Thanks for your reply.
Using TestNG framework and little changes to the XML file we can rerun all failed scripts without much effort.
We can use TestNG class and specify the failed_test.xml path to the TestNG to run the failed test cases. For more details, View below link:
Hello,
Normally in automation after executing scripts/tests, we will check for the results and if the test fails because of above reasons we will re-run then again.
Instead of that we can ask Selenium/testNG to execute the failed test cases again for X (we can define) number of times and check for the updated results.
To achieve this we need to implement TestNG IRetryAnalyzer.
you will find detailed coding here
hi
once check this site www.karthikelearn.com, to get more idea about selenium webdriver
Hi most of the test automation services implemented the retry logic using TestNG IRetryAnalyzer. Below is code that will automatically rerun the failed scripts using the testng report.
Create Retry Class:
public class Retry implements IRetryAnalyzer {
private int retryCount = 0;
private int maxRetryCount = 1;
// Below method returns 'true' if the test method has to be retried else 'false'
//and it takes the 'Result' as parameter of the test method that just ran
public boolean retry(ITestResult result) {
if (retryCount < maxRetryCount) {
System.out.println("Retrying test " + result.getName() + " with status "
+ getResultStatusName(result.getStatus()) + " for the " + (retryCount+1) + " time(s).");
retryCount++;
return true;
}
return false;
}
public String getResultStatusName(int status) {
String resultName = null;
if(status==1)
resultName = "SUCCESS";
if(status==2)
resultName = "FAILURE";
if(status==3)
resultName = "SKIP";
return resultName;
}
}
Class RetryListener
package com.pack.test;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.IAnnotationTransformer;
import org.testng.IRetryAnalyzer;
import org.testng.annotations.ITestAnnotation;
public class RetryListener implements IAnnotationTransformer {
@Override
public void transform(ITestAnnotation testannotation, Class testClass,
Constructor testConstructor, Method testMethod) {
IRetryAnalyzer retry = testannotation.getRetryAnalyzer();
if (retry == null) {
testannotation.setRetryAnalyzer(Retry.class);
}
}
}
Add the listener in testng.xml file
<listeners>
<listener class-name="com.pack.test.RetryListener"/>
</listeners>
Hope this will help you in implementing the retry logic
© 2022 Created by Quality Testing.
Powered by