简体   繁体   English

比较列表WebElement与字符串数组

[英]Compare List WebElement with String Array

How can I compare both list is equal Verify data is from Excel sheet. 如何比较两个列表是否相等验证数据来自Excel工作表。 I need to validate both list are same and there is no additional element or missing element from the list. 我需要验证两个列表是否相同,并且列表中没有其他元素或缺少元素。 I don't require to sorting the list. 我不需要对列表进行排序。 And Print output CAGID Excel data = CAGID web list 并打印输出CAGID Excel数据= CAGID Web列表

    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

If you don't bother about the order, then collect all text from WebElement to List<String> 如果您不关心订单,则将所有文本从WebElement收集到List<String>

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

Then now just compare by using equals() 然后现在只需使用equals()进行比较

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

Note This approach is case sensitive 注意此方法区分大小写

Not really elegant but I thinks it's works 不太优雅,但我认为这是可行的

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM