0
0
Selenium Javatesting~8 mins

Getting and setting attributes in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Getting and setting attributes
Folder Structure
src/
└── test/
    └── java/
        └── com/
            └── example/
                ├── pages/
                │   └── LoginPage.java
                ├── tests/
                │   └── LoginTest.java
                ├── utils/
                │   └── WebDriverFactory.java
                └── config/
                    └── ConfigReader.java
Test Framework Layers
  • Driver Layer: Manages WebDriver setup and teardown (e.g., WebDriverFactory.java).
  • Page Objects: Classes representing web pages with methods to get/set attributes (e.g., LoginPage.java).
  • Tests: Test classes that use page objects to perform actions and assertions (e.g., LoginTest.java).
  • Utilities: Helper classes for common tasks like waits, logging, or reading configs.
  • Configuration: Files and classes to manage environment settings, browser types, and credentials.
Configuration Patterns
  • Use 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 via command line or config file (e.g., Chrome, Firefox).
  • Keep sensitive data like passwords encrypted or use environment variables.
Test Reporting and CI/CD Integration
  • Use TestNG for running tests and generating HTML reports.
  • Integrate with CI tools like Jenkins or GitHub Actions to run tests on code changes.
  • Configure reports to show pass/fail status and screenshots on failure.
  • Use logs to capture attribute values retrieved or set during tests for debugging.
Best Practices
  • Use Page Object Model: Encapsulate attribute get/set methods inside page classes.
  • Explicit Waits: Wait for elements to be present before getting or setting attributes.
  • Do Not Set Attributes Directly Unless Necessary: Prefer user actions (sendKeys, clicks) over JS attribute changes unless testing JS behavior.
  • Keep Tests Independent: Avoid relying on attribute changes from previous tests.
  • Use Clear Naming: Name methods like getInputValue() or setInputAttribute() for clarity.
Self Check

Where in this folder structure would you add a new method to get the "placeholder" attribute of a search input field?

Key Result
Organize Selenium Java tests with Page Objects encapsulating attribute access, supported by config and reporting layers.