如何跳过一定数量的循环函数

时间:2018-05-09 10:59:17

标签: java selenium-webdriver

我目前正在排行榜上进行测试,每20个条目就有一个"显示更多"用户必须按下以显示接下来的20个条目的按钮。我创造了一个"尝试抓住"方法,但它似乎只按下按钮一次,然后我将它添加到test_leaderboard_entry方法的循环,但它花了很长时间后,所有"显示更多"按钮已被按下。如何在测试运行时将其更改为更快,或者我可以更改show_more方法以实际按下出现的所有按钮吗?

public void show_more() throws Exception {
    try {
        if (show_more_button.isDisplayed()) {
            show_more_button.click();
            System.out.println("Selecting show more button");
        }
    } catch (Exception e) {
        System.out.println("Show more button not present");
    }
}

public void test_player_leaderboard_entry() throws Exception {
    int size = playerRows.size();
    Integer previous_total = 0;
    Integer previous_points = 0;
    String previous_user = null;

    for (int i = 0; i < size; i++) {
        show_more();

        //Position
        String position_first_player = Drivers.getDriver().findElement(By.cssSelector("[data-qa-position-value='" + i + "']")).getText();
        //Points
        String points_player = Drivers.getDriver().findElement(By.cssSelector("[data-qa-points-value='" + i + "']")).getText();
        //Username
        String username_player = Drivers.getDriver().findElement(By.cssSelector("[data-qa-player-value='" + i + "']")).getText();
        //Row Number
        Integer row = i + 1;
        Integer point_player = Integer.parseInt(points_player);
        Integer total_of_won_and_looking_good = 0;

        //PICKS
        for (int pick_number = 1; pick_number < 5; pick_number++) {
            String pick_status = Drivers.getDriver().findElement(By.xpath("//*[@id='root']/div/main/section[2]/section/div/ol/a[" + row + "]/li/div[3]/div[" + pick_number + "]/div")).getAttribute("data-qa-pick-state");
            //System.out.println(pick_status);
            if (Integer.parseInt(pick_status) == 2 || Integer.parseInt(pick_status) == 1) {
                total_of_won_and_looking_good = total_of_won_and_looking_good + 1;
            }
        }
        if (previous_total.equals(total_of_won_and_looking_good) && (previous_points > point_player)) {
            //Assert.assertTrue;
            System.out.println("Picks are the same, points are higher ");
        } else if (previous_total.equals(total_of_won_and_looking_good) && previous_points.equals(point_player)) {
            String prev_user = previous_user.toLowerCase();
            String use_player = username_player.toLowerCase();
            Assert.assertTrue(prev_user.compareTo(use_player) < 0);
            System.out.println("Picks are the same, points are the same, alpha order is correct ");
        }
        previous_total = total_of_won_and_looking_good;
        previous_points = point_player;
        previous_user = username_player;

        System.out.println("On row number " + row + " we find " + username_player + " in position " + position_first_player + " with " + total_of_won_and_looking_good + " correct picks and " + points_player + " points!");
    }
}

1 个答案:

答案 0 :(得分:2)

你可以通过以下方式跳过循环,

if ( ((i+1) % 20) == 0)
   show_more();

它将在20的倍数时单击并跳过其他时间。

另外,注意到点击show more按钮后不会更新尺寸值。在前20次迭代后它不会继续。

您可以更改下面给出的代码,以更新计数或大小值,

if ( ((i+1) % 20) == 0){
   show_more();
   size = playerRows.size();
}

通过替换show_more方法调用,将上面的内部插入for循环。它可能适合你。