断言2个字符串是按字母顺序排列的

时间:2018-05-03 13:17:07

标签: java selenium testing automated-tests

我正在写作。一个测试来比较投注表中的排行榜条目,首先我必须比较结果选择或玩家(正在工作)然后我必须比较每个玩家点(这是有效的),但如果这两个属性是相同的我必须断言桌面上更高的玩家按字母顺序排列更高。我已经创建了变量username_player和previous_user来做到这一点,但是无法弄清楚如何做到这一点,我试图把它放在else if部分(我认为是正确的)。这似乎不是一个断言选项吗?

public void test_player_leaderboard_entry() {



    int size = playerRows.size();
    Integer previous_total = 0;
    Integer previous_points = 0;
    String previous_user = null;


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

        //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)) {

            Assert.assertTrue(previous_points > point_player);
            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)) {

            Assert.assertTrue(previous_user.compareTo(username_player) < 0);



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

}

2 个答案:

答案 0 :(得分:0)

您可以在字符串上使用compareTo方法,按字典顺序对它们进行比较。所以你可以做类似

的事情

Assert.assertTrue(previous_user.compareTo(username_player) < 0)

编辑: 也许你可以这样尝试,但我不完全确定这是你想要的:

        if(previous_total.equals(total_of_won_and_looking_good)) {

            Assert.assertTrue(previous_points > point_player);
            System.out.println("Picks are the same, points are higher ");

        } else if (previous_points.equals(point_player)) {

            Assert.assertTrue(previous_user.compareTo(username_player) < 0);

        }

答案 1 :(得分:0)

您可以使用compareTo方法。

尝试使用previous_user.compareTo(username_player) <0

的断言