0
0
Selenium Javatesting~8 mins

XPath with text() in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - XPath with text()
Folder Structure
selenium-java-project/
├── src/
│   ├── main/
│   │   └── java/
│   │       └── com/example/pages/       # Page Object classes
│   │           └── LoginPage.java
│   └── test/
│       └── java/
│           └── com/example/tests/       # Test classes
│               └── LoginTest.java
├── src/test/resources/                   # Test data and config files
│   └── testdata.properties
├── utils/                               # Utility classes (e.g., XPath helpers)
│   └── XPathUtils.java
├── drivers/                             # WebDriver executables
│   └── chromedriver.exe
├── testng.xml                          # TestNG suite configuration
└── pom.xml                             # Maven project file
Test Framework Layers
  • Driver Layer: Manages WebDriver setup and teardown (e.g., ChromeDriver). Located in utility classes or base test classes.
  • Page Objects: Java classes representing web pages. Use XPath with text() to locate elements by visible text. Example: By.xpath("//button[text()='Login']").
  • Tests: TestNG test classes that use page objects to perform actions and assertions.
  • Utilities: Helper classes for common XPath expressions or dynamic XPath builders using text().
  • Configuration: Properties files and XML files to manage environment URLs, browser types, and credentials.
Configuration Patterns
  • Use testng.xml to define test suites and parameters like browser type.
  • Store environment URLs and credentials in src/test/resources/testdata.properties for easy updates.
  • Load properties in utility classes to configure WebDriver and test data dynamically.
  • Use Maven profiles or system properties to switch environments (e.g., dev, staging, prod) without code changes.
Test Reporting and CI/CD Integration
  • Use TestNG built-in reports for test execution results (pass/fail).
  • Integrate with Allure or ExtentReports for detailed HTML reports showing which XPath locators passed or failed.
  • Configure CI/CD pipelines (e.g., Jenkins, GitHub Actions) to run tests automatically on code commits.
  • Publish reports as build artifacts and send notifications on failures.
Best Practices for XPath with text()
  • Prefer text() in XPath to locate elements by their visible text for readability and maintainability.
  • Use exact text matches when possible: //tagname[text()='Exact Text'].
  • For partial matches, use contains(text(),'partial') but avoid overuse to reduce flakiness.
  • Keep XPath expressions simple and avoid overly long or complex chains.
  • Store XPath locators as constants in page object classes for easy updates.
Self Check

Where in this folder structure would you add a new XPath locator using text() for a "Submit" button on the registration page?

Key Result
Use Page Object Model with XPath text() locators stored in page classes for clear, maintainable Selenium Java tests.