-1

I have two ArrayList of String which I want to compare. 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 i.e myData and lineText. The myData values are coming from an excel sheet and are getting stored in an arraylist of String. The lineText is coming from the web portal and is the text of web elements shown on portal. I want to compare the test data i am passing through excel and data shown on the portal. 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}. 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. Please help.

1

1 Answer 1

0
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:

false
'BAR' not equals 'BEAR'
Skipped items from source due to different lists size:
Berlin
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.