如果由于某种Throwable类型而导致测试失败,我想重试Selenium测试。为此,我使用了IRetryAnalyzer。这可以正常工作,要在运行时添加它,我可以使用IAnnotationTransformer。
但是,@ BeforeMethod不会在重试时调用,它说“跳过配置”。我已经添加了(alwaysRun = true)。有没有办法也可以在重试时调用beforeMethod?
Retry.java
public class Retry implements IRetryAnalyzer {
private final Map<String, Integer> testReRunMap = new HashMap<>();
private static int MAX_RETRY = 5;
@Override
public boolean retry(ITestResult iTestResult) {
System.out.println("Checking if test should be retried");
boolean shouldRetry = false;
//Compute shouldRetry
if (shouldRetry) {
return true;
} else {
return false;
}
}
}
AnnotationTransformer:
public class AnnotationTransformerListener implements IAnnotationTransformer {
@Override
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor,
Method testMethod) {
annotation.setRetryAnalyzer(Retry.class);
}
}
BeforeMethod:
@BeforeMethod(alwaysRun = true)
public void beforeMethod() throws Exception {
//perform operation to be done before every test
}