Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Tech Solutions
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

Selenium Automation Testing

  • Introduction to Automation Testing
  •  Introduction to Selenium
  • Advantages and Disadvantages of Selenium WebDriver
  •  Locators in Selenium
  • How to launch the browsers ? 
  • Browser Navigation Commands in Selenium
  •  Handling the textbox and button using selenium
  • Handling the radio button and checkbox using selenium
  •  Handling the drop downs and multi-select dropdown
  • Validate the text validation and attributes values. 
  • Handling Alert Popup
  • Handling iFrames
  • Handling Multiple Browsers or Tabs
View Categories
  • Home
  • Tutorials
  • Selenium Automation
  • Selenium Automation Testing
  • Validate the text validation and attributes values. 

Validate the text validation and attributes values. 

Validate the text validation and

attributes values. 

Validate the static text, dynamic text and pattern matching dynamic content 

Test Case Flow: 

  1. Launch Chrome via WebDriver.
  2. Navigate to your validation page
  3. Validate the static text. 
  4. Validate the dynamic text using exact match
  5. Wait until the status text changes to “Completed”
  6. Validate the dynamic text using pattern matching
  7. Validate the dynamic text for non-empty / changed text
  8. 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: 

  1. Launch Chrome via WebDriver.
  2. Navigate to your validation page
  3. Locate the element using an XPath locator
  4. Check basic state of webelement
  5. Verify the expected attribute values
  6. 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();

        }

    }

}

Selenium Automation Testing
What are your Feelings
Share This Article :
  • Facebook
  • X
  • LinkedIn
 Handling the drop downs and multi-select dropdownHandling Alert Popup
© 2018 – 2025 Vinoth Tech Solutions Ltd (UK), Reg. No: 16489105