Validate the text validation and
attributes values.
Validate the static text, dynamic text and pattern matching dynamic content
Test Case Flow:
- Launch Chrome via WebDriver.
- Navigate to your validation page
- Validate the static text.
- Validate the dynamic text using exact match
- Wait until the status text changes to “Completed”
- Validate the dynamic text using pattern matching
- Validate the dynamic text for non-empty / changed text
- Close the browser.
Program:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
public class TextValidationExample {
public static void main(String[] args) throws InterruptedException {
// 1. Point to your ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
// 2. Launch Chrome
WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
try {
// 3. Navigate to your validation page
driver.get("https://example.com/validation");
// === STATIC TEXT VALIDATION ===
WebElement header = driver.findElement(By.cssSelector("h1.page-title"));
String actualHeader = header.getText().trim();
System.out.println("Header text: " + actualHeader);
if (!actualHeader.equals("Welcome to Our Store")) {
throw new AssertionError("Static text validation failed: expected 'Welcome to Our Store'");
}
// === DYNAMIC TEXT VALIDATION: exact match ===
// 4. Trigger the dynamic update
driver.findElement(By.id("startProcess")).click();
// 5. Wait until the status text changes to "Completed"
wait.until(ExpectedConditions.textToBe(By.id("orderStatus"), "Completed"));
String status = driver.findElement(By.id("orderStatus")).getText();
System.out.println("Order status: " + status);
// === DYNAMIC TEXT VALIDATION: pattern matching ===
WebElement confirmationMsg = driver.findElement(By.id("confirmationMsg"));
String confirmation = confirmationMsg.getText();
System.out.println("Confirmation message: " + confirmation);
if (!confirmation.matches("^Order #\\d{5,10} placed$")) {
throw new AssertionError("Pattern validation failed: " + confirmation);
}
// === DYNAMIC TEXT VALIDATION: non-empty / changed text ===
WebElement counter = driver.findElement(By.id("itemCount"));
String beforeCount = counter.getText();
driver.findElement(By.id("updateCounter")).click();
wait.until(drv -> !counter.getText().equals(beforeCount));
String afterCount = counter.getText();
System.out.println("Counter changed from " + beforeCount + " to " + afterCount);
if (afterCount.isEmpty() || afterCount.equals(beforeCount)) {
throw new AssertionError("Counter did not update correctly");
}
} finally {
// 6. Close the browser
driver.quit();
}
}
}
Validate the attribute values
Test Case Flow:
- Launch Chrome via WebDriver.
- Navigate to your validation page
- Locate the element using an XPath locator
- Check basic state of webelement
- Verify the expected attribute values
- Close the browser.
Program:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class GetAttributeExample {
public static void main(String[] args) {
// 1. Point to your ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
// 2. Launch Chrome
WebDriver driver = new ChromeDriver();
try {
// 3. Navigate to the page containing the element
driver.get("https://example.com/profile");
// 4. Locate the element using an XPath locator
// e.g., an input field with a placeholder attribute
WebElement searchInput = driver.findElement(
By.xpath("//input[@id='searchBox']"));
// 5. Check basic state
System.out.println("Displayed? " + searchInput.isDisplayed());
System.out.println("Enabled? " + searchInput.isEnabled());
// 6. Get attribute values
String typeAttr = searchInput.getAttribute("type");
String placeholderAttr= searchInput.getAttribute("placeholder");
String maxLengthAttr = searchInput.getAttribute("maxlength");
System.out.println("type = " + typeAttr);
System.out.println("placeholder= " + placeholderAttr);
System.out.println("maxlength = " + maxLengthAttr);
// 7. Example: verify an expected attribute
String expectedPlaceholder = "Search...";
if (placeholderAttr != null && placeholderAttr.equals(expectedPlaceholder)) {
System.out.println("Placeholder matches expected value.");
} else {
System.out.println("Placeholder does not match. Expected: \""
+ expectedPlaceholder + "\"");
}
} finally {
// 8. Close the browser (end the session)
driver.quit();
}
}
}