Reputation: 1825
I have a test method like this:
@Test
public void generateReports(String clientname, String username) {
loginPage().login(clientname, username);
loginPage().chooseReportManagement();
reportPage().createReport();
}
My goal is to generate 100 reports. My solution right now is to loop the step createReport()
100 times, like this:
@Test
public void generateReports(String clientname, String username) {
loginPage().login(clientname, username);
loginPage().chooseReportManagement();
for (int i = 0; i < 100 ; i++) {
reportPage().createReport();
}
}
It does the task. But I would like to know if there is any other way to achieve this. Because in this way, when something wrong happens when creating a report, the test will be terminated. I want something like, the test should carry on if creating a report is failed, until the loop ends.
I use Selenium and TestNG.
Thanks
Upvotes: 2
Views: 1115
Reputation: 544
RetryAnalyzer.class
public class RetryAnalyzer implements IRetryAnalyzer {
int counter = 0;
@Override
public boolean retry(ITestResult result) {
// check if the test method had RetryCountIfFailed annotation
RetryCountIfFailed annotation = result.getMethod().getConstructorOrMethod().getMethod()
.getAnnotation(RetryCountIfFailed.class);
// based on the value of annotation see if test needs to be rerun
if((annotation != null) && (counter < annotation.value()))
{
counter++;
return true;
}
return false;
}
}
RetryCountIfFailed.class
@Retention(RetentionPolicy.RUNTIME)
public @interface RetryCountIfFailed {
// Specify how many times you want to
// retry the test if failed.
// Default value of retry count is 0
int value() default 0;
}
Test.class
@Test
@RetryCountIfFailed(100)
public void generateReports(String clientname, String username) {
loginPage().login(clientname, username);
loginPage().chooseReportManagement();
reportPage().createReport();
}
You can refer to this link if this answer doesn't satisfy enough: TestNG retryAnalyzer only works when defined in methods @Test, does not work in class' @Test
Upvotes: 0
Reputation: 8444
Use try/catch:
@Test
public void generateReports(String clientname, String username) {
loginPage().login(clientname, username);
loginPage().chooseReportManagement();
for (int i = 0; i < 100 ; i++) {
try {
reportPage().createReport();
} catch (Exception e) {
System.out.println("Report creation failed!")
}
}
}
Upvotes: 1
Reputation: 1868
If you are using TestNG you can just add invocation count besides using for loop
@Test(invocationCount = 100)
Upvotes: 0