0
0
Selenium Javatesting~8 mins

XPath axes (parent, following-sibling, preceding) in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - XPath axes (parent, following-sibling, preceding)
Folder Structure of Selenium Java Test Framework
  selenium-java-framework/
  ├── src/
  │   ├── main/
  │   │   └── java/
  │   │       └── com/example/pages/       # Page Object classes
  │   │           ├── LoginPage.java
  │   │           └── DashboardPage.java
  │   └── test/
  │       └── java/
  │           └── com/example/tests/       # Test classes
  │               ├── LoginTest.java
  │               └── DashboardTest.java
  ├── resources/
  │   └── testdata/                        # Test data files (CSV, JSON)
  ├── config/
  │   └── config.properties                # Environment and browser configs
  ├── utils/
  │   ├── WebDriverFactory.java            # WebDriver setup and utilities
  │   └── WaitUtils.java                    # Explicit wait helpers
  ├── testng.xml                           # TestNG suite configuration
  └── pom.xml                             # Maven build and dependencies
  
Test Framework Layers
  • Driver Layer: WebDriver setup and teardown handled by WebDriverFactory.java. Manages browser instances.
  • Page Object Layer: Classes representing web pages (e.g., LoginPage.java). Use XPath axes like parent::, following-sibling::, preceding:: to locate elements relative to others.
  • Test Layer: Test classes (e.g., LoginTest.java) contain test methods using TestNG. They call page objects to perform actions and assertions.
  • Utilities Layer: Helper classes for waits, logging, and WebDriver management.
  • Configuration Layer: Properties files and TestNG XML to manage environments, browsers, and test suites.
Configuration Patterns

Use config.properties to store environment URLs, browser types, and credentials. Example:

  baseUrl=https://example.com
  browser=chrome
  username=testuser
  password=testpass
  

Load these properties in WebDriverFactory.java to initialize the correct browser and navigate to the right URL.

Use TestNG XML (testng.xml) to define test suites and parameterize tests for different browsers or environments.

Test Reporting and CI/CD Integration
  • Use TestNG built-in reports for test execution results (pass/fail, skipped).
  • Integrate with Maven Surefire plugin to generate HTML and XML reports.
  • Use Jenkins or GitHub Actions to run tests automatically on code commits.
  • Publish reports in CI/CD pipelines for visibility.
Framework Design Principles
  1. Use Page Object Model: Encapsulate element locators and actions in page classes. Use XPath axes to write robust locators relative to known elements.
  2. Explicit Waits: Use explicit waits (e.g., WebDriverWait) to handle dynamic page elements instead of implicit waits.
  3. Configurable Tests: Use properties files and TestNG parameters to run tests on different browsers and environments without code changes.
  4. Readable XPath: Use XPath axes like parent::, following-sibling::, and preceding:: to create clear and maintainable locators that reflect page structure.
  5. Separate Test Data: Keep test data outside code in files like CSV or JSON for easy updates and data-driven testing.
Self Check

Where in this folder structure would you add a new page object class for a "UserProfile" page that requires XPath axes locators?

Key Result
Organize Selenium Java tests using Page Object Model with clear layers, config files, and XPath axes for robust element location.