【问题标题】:How to sort and compare the expected and actual list of WebElements如何对 WebElements 的预期和实际列表进行排序和比较
【发布时间】:2020-09-17 13:29:56
【问题描述】:

我有一个存储在列表中的预期 WebElement 和实际 WebElement 列表。我想对两个列表进行排序并将预期与实际的 WebElements 进行比较。

下面是我写的代码。

List<WebElement> ExpList = List.of(LookingLbl,GenderLbl,MandatoryList,NameLbl,MobLbl );
        System.out.println("Expected list size is : " +ExpList.size());
        System.out.println("Expected list of Web Elements below : ");
        for(WebElement expElem : ExpList) {
            System.out.println(expElem.getText());
        }

List<WebElement> ActList = driver.findElements(By.xpath("//h3[contains(text(),'')]"));
        System.out.println("WebElement list size is :" +ActList.size());
        System.out.println("Actual list of Web Elements below : ");
        for(WebElement elem : ActList) {
            if(elem.getText().contains("*")) {
                System.out.println(elem.getText());
            }           
        }

现在我希望对 ExpList 和 ActList 进行排序和比较。如何对实际和预期的网络元素进行排序和比较?

【问题讨论】:

    标签: java selenium selenium-webdriver arraylist webdriver


    【解决方案1】:

    您可以使用Collections 对列表进行排序:

    Collections.sort(ExpList);
    Collections.sort(ActList);
    

    然后使用JUnit断言断言它们相等:

    Assert.assertEquals(ExpList, ActList);
    

    【讨论】:

    • 不,这不起作用..Collections.sort(ExpList)。这没有向我显示 sort as ExpList 中的任何选项。如果我强行使用此 Collections.sort(ExpList);它会在“排序”上给出错误,如下所示。将参数“ExpList”转换为 List
    猜你喜欢
    • 2022-01-06
    • 2014-09-30
    • 2013-02-13
    • 1970-01-01
    • 2018-09-10
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多