The getText method is a WebDriver operation that retrieves the text content of a web element so an automated acceptance test can verify what the UI displays. section 10.2.13 section 10.2.12
A typical verification flow is:
- Locate the relevant element or collection with an ID, CSS selector, or XPath. section 10.2.12
- Call getText() on each WebElement and collect the returned strings. section 10.2.12
- Assert that the retrieved text matches the acceptance criterion, such as containing “Singapore.” section 10.2.12
Checking featured destinations
After locating the featured destination titles with the CSS selector #featured .destination-title, loop through the WebElements, add each getText() result to a list, and assert that the list contains “Singapore.” section 10.2.12
List<WebElement> destinations #1
= driver.findElements(By.cssSelector("#featured .destination-title")); #1
List<String> destinationTitles = new ArrayList<String>(); #2
for(WebElement destinationElement : destinations) { #3
destinationTitles.add(destinationElement.getText()); #3
} #3
assertThat(destinationTitles).contains("Singapore"); #4
View the complete Java example that locates destination titles, extracts their text with getText(), and verifies the expected destination. section 10.2.12
