0
0
Selenium Javatesting~8 mins

Getting text and attributes in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Getting text and attributes
Folder Structure
src/
└── test/
    └── java/
        └── com/
            └── example/
                ├── pages/
                │   └── HomePage.java
                ├── tests/
                │   └── HomePageTest.java
                ├── utils/
                │   └── WebDriverFactory.java
                └── config/
                    └── ConfigReader.java

This structure separates page objects, tests, utilities, and configuration files clearly.

Test Framework Layers
  • Driver Layer: Manages WebDriver setup and teardown (e.g., WebDriverFactory).
  • Page Objects: Classes representing web pages, encapsulating element locators and methods to get text or attributes (e.g., HomePage.java).
  • Tests: Test classes that use page objects to perform actions and assertions (e.g., HomePageTest.java).
  • Utilities: Helper classes for common functions like waits or reading config.
  • Configuration: Files and classes managing environment settings, URLs, credentials.
Configuration Patterns
  • Use a config.properties file to store environment URLs, browser types, and credentials.
  • Load configuration in ConfigReader.java to provide easy access to properties.
  • Allow switching browsers by passing parameters or environment variables to WebDriverFactory.
  • Keep sensitive data out of source code by using environment variables or encrypted files.
Test Reporting and CI/CD Integration
  • Use TestNG or JUnit built-in reports for test execution results.
  • Integrate with Allure or ExtentReports for detailed HTML reports showing passed/failed tests and screenshots.
  • Configure CI/CD pipelines (e.g., Jenkins, GitHub Actions) to run tests automatically on code changes.
  • Publish reports as build artifacts or send notifications on failures.
Framework Design Principles
  • Page Object Model: Encapsulate element locators and methods like getText() and getAttribute() inside page classes.
  • Explicit Waits: Use waits before getting text or attributes to ensure elements are ready.
  • Reusable Methods: Create generic methods in page objects for getting text or attributes to avoid duplication.
  • Clear Naming: Name methods clearly, e.g., getHeaderText() or getButtonAttribute(String attr).
  • Separation of Concerns: Keep test logic in test classes, UI interaction in page objects, and config in separate files.
Self-Check Question

Where in this framework structure would you add a new method to get the "alt" attribute of an image on the Home page?

Key Result
Use Page Object Model to encapsulate element text and attribute retrieval with explicit waits and clear separation of concerns.