0
0
Selenium Javatesting~8 mins

XPath with attributes in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - XPath with attributes
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, config files
│   └── testdata.properties
├── drivers/                             # WebDriver executables
│   └── chromedriver.exe
├── pom.xml                              # Maven project file
└── testng.xml                          # TestNG suite configuration
    
Test Framework Layers
  • Driver Layer: Manages WebDriver setup and teardown (e.g., ChromeDriver).
  • Page Objects: Classes representing web pages. Use XPath with attributes to locate elements, e.g., By.xpath("//input[@id='username']").
  • Test Layer: Contains test classes using TestNG to run tests and assertions.
  • Utilities: Helper classes for waits, logging, and common actions.
  • Configuration: Properties files and TestNG XML for environment and browser settings.
Configuration Patterns

Use testng.xml to define test suites and parameters like browser type.

Use testdata.properties to store URLs, credentials, and environment-specific data.

Example snippet from testdata.properties:

url=https://example.com/login
username=testuser
password=secret
    

Load properties in tests or page objects to keep data separate from code.

Test Reporting and CI/CD Integration
  • Use TestNG built-in reports for pass/fail results.
  • Integrate with Jenkins or GitHub Actions to run tests on code commits.
  • Generate HTML reports with plugins like maven-surefire-report-plugin.
  • Use screenshots on failure saved in a reports folder for debugging.
Best Practices for XPath with Attributes in Selenium Java
  • Use unique attributes like id or name in XPath for stable locators.
  • Prefer absolute attribute matching: //input[@id='username'] over partial or complex XPath.
  • Keep XPath expressions simple and readable to ease maintenance.
  • Use Page Object Model to centralize locators and avoid duplication.
  • Use explicit waits to ensure elements located by XPath are present before interaction.
Self Check

Where in this folder structure would you add a new Page Object class for a "Registration" page that uses XPath with attributes?

Key Result
Organize Selenium Java tests using Page Objects with XPath locators based on attributes for clear, maintainable automation.