Arrays and Lists

List Is What Selenium Returns

driver.findElements(...) returns a List<WebElement>. So you must be comfortable with List, ArrayList, size(), get(i) and the for-each loop.

Example

Compare the product names shown (actual) with the expected list:

import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<String> expected = List.of("Laptop", "Mouse", "Keyboard");
        List<String> actual = new ArrayList<>();
        actual.add("Laptop");
        actual.add("Mouse");
        actual.add("Keyboard");

        System.out.println("Count: " + actual.size());
        System.out.println("First: " + actual.get(0));
        System.out.println("Has Mouse: " + actual.contains("Mouse"));
        System.out.println("Lists equal: " + expected.equals(actual));

        actual.remove("Mouse");
        List<String> missing = new ArrayList<>(expected);
        missing.removeAll(actual);
        System.out.println("Missing after removal: " + missing);
    }
}

Output

Count: 3
First: Laptop
Has Mouse: true
Lists equal: true
Missing after removal: [Mouse]

Sorting Check

Example

Check that "Price: Low to High" really sorted the prices:

import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> shown = List.of(299, 499, 450, 999);
        List<Integer> sorted = new ArrayList<>(shown);
        Collections.sort(sorted);
        System.out.println("Shown : " + shown);
        System.out.println("Sorted: " + sorted);
        System.out.println(shown.equals(sorted) ? "PASS: sorted" : "FAIL: not sorted");
    }
}

Output

Shown : [299, 499, 450, 999]
Sorted: [299, 450, 499, 999]
FAIL: not sorted