0
0
Selenium Javatesting~8 mins

findElement by name in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - findElement by name
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 locators and methods (e.g., LoginPage.java uses findElement(By.name()) to locate elements by name).
  • 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: Handles environment settings, browser types, and credentials (e.g., ConfigReader.java).
Configuration Patterns
  • Use property files (e.g., config.properties) to store URLs, browser types, and credentials.
  • Load configurations in ConfigReader.java to provide values to tests and driver setup.
  • Allow switching browsers via config (e.g., Chrome, Firefox) in WebDriverFactory.java.
  • Use environment variables or profiles for different test environments (dev, staging, prod).
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 help debug locator issues like findElement(By.name()) failures.
Best Practices
  • Use By.name() locator only when the name attribute is unique and stable.
  • Encapsulate findElement(By.name()) calls inside page object methods for reuse and clarity.
  • Use explicit waits before locating elements to avoid timing issues.
  • Keep locators in page objects, not in test classes, to separate concerns.
  • Use meaningful method names in page objects that describe the action or element.
Self Check

Where in this folder structure would you add a new page object class for a page that contains elements located by findElement(By.name())?

Key Result
Use Page Object Model with By.name() locators encapsulated in page classes under src/test/java/com/example/pages.