Real World Example

Filter and Sort on a Product Listing

On Myntra- or Amazon-style listing pages, the most common regression test is: apply a brand filter, sort by price, and check that every product matches the filter and the prices are in order.

driver.get("https://shopease.example.com/search?q=shoes");
WebElement nike = driver.findElement(By.id("brand-nike"));
if (!nike.isSelected()) nike.click();
new Select(driver.findElement(By.id("sort"))).selectByVisibleText("Price: Low to High");
wait.until(ExpectedConditions.urlContains("brand=nike"));

List<WebElement> cards = driver.findElements(By.cssSelector(".product-card"));
List<String> brands = cards.stream().map(c -> c.findElement(By.cssSelector(".brand")).getText()).toList();
List<Integer> prices = cards.stream()
        .map(c -> Integer.parseInt(c.findElement(By.cssSelector(".price")).getText().replaceAll("[^0-9]", "")))
        .toList();
System.out.println("All Nike: " + brands.stream().allMatch(b -> b.equals("Nike")));
System.out.println("Sorted: " + prices.equals(prices.stream().sorted().toList()));

Example

Output:

Run ListingFilterTest.main()

Example output

All Nike: true
Sorted: true

Takeaway

Selenium reads the page; plain Java collections do the checking. The stronger your Java collections skills, the shorter these tests become.