简体   繁体   English

比较两个字符串 ArrayList 中的 Java Selenium 中的值

[英]Compare two ArrayList of String for values inside it in Java Selenium

I have two ArrayList of String which I want to compare.我有两个要比较的字符串 ArrayList。 Below is the code:下面是代码:

ArrayList<String> myData = objExcelData.GetRows("Dashboard", 1);
        System.out.println(myData);

        List<WebElement> uiList = driver
                .findElements(By.xpath("//div[@class='machineblockdiv cursorpointer machinblockheight']//h4"));

        ArrayList<String> lineText = new ArrayList<String>();

        for (int i = 0; i < uiList.size(); i++) {
            lineText.add(uiList.get(i).getText());

        }
        System.out.println(lineText);

This code gives me two lists ie myData and lineText.这段代码给了我两个列表,即 myData 和 lineText。 The myData values are coming from an excel sheet and are getting stored in an arraylist of String. myData 值来自 excel 表,并存储在字符串的 arraylist 中。 The lineText is coming from the web portal and is the text of web elements shown on portal. lineText 来自 web 门户,是门户上显示的 web 元素的文本。 I want to compare the test data i am passing through excel and data shown on the portal.我想比较我通过 excel 的测试数据和门户网站上显示的数据。 Both should match.两者应该匹配。 If not then my test fails.如果不是,那么我的测试失败。

For example: My 'myData' array consists of {Airway, Finsuc, milestone} and my 'lineText' arraylist consists of {milestone, Jet}.例如:我的 'myData' 数组由 {Airway, Finsuc, landmark} 组成,我的 'lineText' arraylist 由 {milestone, Jet} 组成。 So this test should fail as all the values are not same in both Array lists.所以这个测试应该失败,因为两个数组列表中的所有值都不相同。

After comparing these I want to put an assertion in my TestNg code as well.在比较这些之后,我想在我的 TestNg 代码中添加一个断言。 Please help.请帮忙。

package selenium;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CompareListsExample {

    public static void main (String[] args) {
        List<String> src = Arrays.asList("FOO", "BAR", "Berlin");
        List<String> trg = Arrays.asList("FOO", "BEAR");
        
        // compare List<String> with List<String>
        System.out.println(src.equals(trg));
        
        // compare String with String inside of the List<String>
        compareListsOfStrings(src, trg);
    }
    
    
    public static void compareListsOfStrings(List<String> sourceList, List<String> targetList) {
        List<String> skippedFromSource = new ArrayList<String>();
        List<String> skippedFromTarget = new ArrayList<String>();
        int maxIndex = 0;
        if (sourceList.size() > targetList.size()) {
            maxIndex = targetList.size();
            for (int i = maxIndex; i < sourceList.size(); i++) {
                skippedFromSource.add(sourceList.get(i));
            }
        }
        else {
            maxIndex = sourceList.size();
            for (int i = maxIndex; i < targetList.size(); i++) {
                skippedFromTarget.add(targetList.get(i));
            }
        }
        for (int i = 0; i < maxIndex; i++) {
            if (!sourceList.get(i).equals(targetList.get(i))) {
                System.out.println("'" + sourceList.get(i) + "' not equals '" + targetList.get(i) + "'");
            }
        }
        if (!skippedFromSource.isEmpty()) {
            System.out.println("Skipped items from source due to different lists size:" + System.lineSeparator() + String.join(System.lineSeparator(), skippedFromSource));
        }
        if (!skippedFromTarget.isEmpty()) {
            System.out.println("Skipped items from target due to different lists size:" + System.lineSeparator() + String.join(System.lineSeparator(), skippedFromTarget));
        }
    }
    
}

Output: Output:

false
'BAR' not equals 'BEAR'
Skipped items from source due to different lists size:
Berlin

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

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