Careeroza
Verifying Sort Order
Read, Copy, Sort, Compare
To test "Price: Low to High", read all prices from the page into a list, make a sorted copy, and compare. If they are equal, the page sorted correctly.
new Select(driver.findElement(By.id("sort"))).selectByValue("price-asc");
wait.until(ExpectedConditions.urlContains("sort=price-asc"));
List<Integer> shown = driver.findElements(By.cssSelector(".product-card .price")).stream()
.map(e -> Integer.parseInt(e.getText().replaceAll("[^0-9]", "")))
.toList();The Check
Example
The comparison part runs here with prices as the page would show them:
import java.util.*;
public class Main {
static List<Integer> parse(List<String> texts) {
List<Integer> out = new ArrayList<>();
for (String t : texts) out.add(Integer.parseInt(t.replaceAll("[^0-9]", "")));
return out;
}
static void checkAscending(String name, List<String> texts) {
List<Integer> shown = parse(texts);
List<Integer> expected = new ArrayList<>(shown);
Collections.sort(expected);
System.out.println(name + ": " + (shown.equals(expected) ? "PASS" : "FAIL expected " + expected + " but was " + shown));
}
public static void main(String[] args) {
checkAscending("Shoes", List.of("₹499", "₹1,299", "₹1,999", "₹2,450"));
checkAscending("Bags", List.of("₹699", "₹599", "₹1,099"));
List<String> names = List.of("Adidas Duramo", "Nike Revolution", "puma Velocity");
List<String> sortedNames = new ArrayList<>(names);
sortedNames.sort(String.CASE_INSENSITIVE_ORDER);
System.out.println("Names A-Z: " + (names.equals(sortedNames) ? "PASS" : "FAIL"));
}
}Output
Shoes: PASS Bags: FAIL expected [599, 699, 1099] but was [699, 599, 1099] Names A-Z: PASS
Tip: Sort by price as numbers, not strings: as text, "₹999" comes after "₹1,299".