Careeroza
Real World Example
Order History at an E-commerce Site
On Amazon- or Flipkart-style "My Orders" pages, the order rows are generated and have no stable ids. The QA team locates rows by order number text and reads the status with axes:
public String statusOf(String orderId) {
String xpath = String.format("//td[text()='%s']/following-sibling::td[1]", orderId);
return driver.findElement(By.xpath(xpath)).getText();
}
public void cancel(String orderId) {
String xpath = String.format("//td[text()='%s']/parent::tr//button[contains(.,'Cancel')]", orderId);
driver.findElement(By.xpath(xpath)).click();
}
System.out.println("ORD-1001: " + statusOf("ORD-1001"));
System.out.println("ORD-1002: " + statusOf("ORD-1002"));Example
Output:
Run OrderHistoryTest.main()Example output
ORD-1001: Delivered ORD-1002: Shipped
Takeaway
Short, relative locators that use meaningful text and attributes survive redesigns. Teams review locators in code review the same way they review code.