0
0
Selenium Javatesting~8 mins

XPath functions (contains, starts-with) in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - XPath functions (contains, starts-with)
Folder Structure
selenium-java-project/
├── src/
│   └── test/
│       └── java/
│           ├── pages/
│           │   └── LoginPage.java
│           ├── tests/
│           │   └── LoginTest.java
│           ├── utils/
│           │   └── XPathHelper.java
│           └── config/
│               └── ConfigManager.java
├── testng.xml
└── pom.xml

This structure organizes code by purpose: pages hold page objects, tests hold test cases, utils hold helper classes like XPath utilities, and config manages environment settings.

Test Framework Layers
  • Driver Layer: Manages WebDriver setup and teardown for browser control.
  • Page Objects: Classes representing web pages. Use XPath functions contains() and starts-with() in locators to find elements flexibly.
  • Tests: Test classes using TestNG to run scenarios and assert outcomes.
  • Utilities: Helper classes like XPathHelper to build dynamic XPath expressions using functions.
  • Configuration: Manages environment variables, browser types, and credentials.
Configuration Patterns
  • Environment Properties: Use config.properties file to store URLs, usernames, passwords.
  • Browser Selection: Pass browser type as a parameter or system property to run tests on different browsers.
  • Credentials: Store securely and load at runtime, avoid hardcoding in code.
  • XPath Helpers: Utility methods to create XPath with contains() or starts-with() for reusable, readable locators.
Test Reporting and CI/CD Integration
  • TestNG Reports: Built-in HTML reports showing passed/failed tests with stack traces.
  • Allure Reporting: Integrate Allure for detailed, user-friendly reports with screenshots.
  • CI/CD Integration: Use Jenkins or GitHub Actions to run tests on code push, generate reports automatically.
  • Failure Screenshots: Capture screenshots on assertion failures to help debugging XPath locator issues.
Framework Design Best Practices
  1. Use XPath functions for flexible locators: contains() helps find elements with partial text or attribute matches; starts-with() helps with predictable prefixes.
  2. Keep XPath expressions readable: Use helper methods to build complex XPath instead of long strings in page objects.
  3. Separate locators from test logic: Define XPath locators in page objects, not in test classes.
  4. Use explicit waits: Wait for elements located by XPath functions to be visible or clickable to avoid flaky tests.
  5. Parameterize XPath locators: Allow dynamic values in XPath to reuse locators for different test data.
Self Check

Where in this folder structure would you add a new XPath locator using contains() to find a button with partial text "Submit"?

Key Result
Organize Selenium Java tests with clear layers and use XPath functions in page objects for flexible element locating.