Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
76 views
in Technique[技术] by (71.8m points)

java - Selenium to repeat a specific test step

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

question from:https://stackoverflow.com/questions/66047408/selenium-to-repeat-a-specific-test-step

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!")
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...