2

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

Ragnarsson
  • 1,715
  • 7
  • 41
  • 74

3 Answers3

1

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!")
        }
    }
}
Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
0

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

TechGeek49
  • 476
  • 1
  • 7
  • 24
0

If you are using TestNG you can just add invocation count besides using for loop

@Test(invocationCount = 100)
Norayr Sargsyan
  • 1,737
  • 1
  • 12
  • 26
  • Thanks, I know this, But it doesn't, I just need to repeat `createReport`. If we use `invocationCount`, it will loop also the action login and choose report management – Ragnarsson Feb 04 '21 at 14:34
  • you can remove the login part from this method and add it in BeforeMethod – Norayr Sargsyan Feb 04 '21 at 14:36