【问题标题】:Compare List WebElement with String Array将列表 WebElement 与字符串数组进行比较
【发布时间】:2019-02-22 16:07:40
【问题描述】:

如何比较两个列表是否相等验证数据是否来自 Excel 工作表。 我需要验证两个列表是否相同,并且列表中没有其他元素或缺少元素。我不需要对列表进行排序。 并打印输出 CAGID Excel 数据 = CAGID web list

    String[] Verify1 = Verify.split(",");
        for(String actualView1 : Verify1) {
            System.out.println("string" + actualView1);
        }

        List<WebElement> options = driver.findElements(By.xpath(SUMMARYFIELDS));
        for (WebElement ele : options) {
            System.out.println(ele.getText());
        }

Output String 

string CAGID
string GFPID
string IRU
string Control Unit
string Obligor Classification
string Obligor Limit Rating
string Obligor Risk Rating
string Commentary
string Credit Officer
string Risk Manager
string RCA Date
string RCA Extension Date

Output ele.getText()

CAGID
GFPID
IRU
Control Unit
Obligor Classification
Obligor Limit Rating
Obligor Risk Rating
Commentary
Credit Officer
Risk Manager
RCA Date
RCA Extension Date

【问题讨论】:

    标签: java arrays string selenium selenium-webdriver


    【解决方案1】:

    如果你不关心订单,然后收集从WebElementList&lt;String&gt;的所有文本

    List<String> text = options.stream().map(WebElement::getText).collect(Collectors.toList());
    

    那么现在只需使用equals()进行比较

    System.out.println(text.equals(Arrays.asList(verify1));  // use naming convention for variables 
    

    注意这种方法区分大小写

    【讨论】:

    • 是的,这项工作,但我如何打印输出 CAGID Excel 数据 = CAGID 文本。
    • 你想为每个字符串@ARD做这个吗?
    • 那么你必须迭代它,为什么要这样做@ARD?
    • 我需要这样做,因为这将帮助我验证哪个项目在测试中失败,很容易找到列表或数据中缺少的元素
    • 您确实应该使用 JUnit 或 TestNG 断言。他们会为你做所有这些工作,当它失败时,它会告诉你什么不相等,例如。 How to assert that lists are equal with testng?
    【解决方案2】:

    不是很优雅,但我认为它有效

            String[] Verify1 = Verify.split(",");
            List<WebElement> options = driver.findElements(By.xpath(SUMMARYFIELDS));
            boolean isOK = true;    
    
            for (WebElement ele : options) {
                int i = Arrays.asList(Verify1).indexOf("string "+ele.getText())
                if(i==-1){
                     isOK=false;
                }
                else{
                     System.out.println(Verify1[i]" Excel data = "+ ele.getText()+" web list");
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-22
      • 1970-01-01
      • 2016-10-17
      • 1970-01-01
      • 2021-10-17
      相关资源
      最近更新 更多