0
0
Selenium Javatesting~8 mins

findElement by className in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - findElement by className
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).
  • Page Objects: Classes representing web pages with locators and methods. Use findElement(By.className("className")) here.
  • Tests: Test classes using test frameworks like TestNG or JUnit to run test cases.
  • Utilities: Helper classes for common functions like waits, logging, or data handling.
  • Configuration: Reads environment settings, browser types, and credentials.
Configuration Patterns

Use property files (e.g., config.properties) to store environment URLs, browser types, and credentials.

Load these properties in ConfigReader.java and pass them to WebDriver setup.

Example snippet in WebDriverFactory.java:

String browser = ConfigReader.getProperty("browser");
switch (browser.toLowerCase()) {
  case "chrome":
    // setup ChromeDriver
    break;
  case "firefox":
    // setup FirefoxDriver
    break;
  default:
    throw new IllegalArgumentException("Unsupported browser: " + browser);
}
    
Test Reporting and CI/CD Integration
  • Use TestNG built-in reports or integrate with Allure for detailed HTML reports.
  • Configure CI/CD pipelines (e.g., Jenkins, GitHub Actions) to run tests on code commits.
  • Publish reports as build artifacts and notify teams on failures.
Best Practices
  1. Use Page Object Model: Keep locators and page actions in page classes to separate test logic from UI details.
  2. Use explicit waits: Avoid flaky tests by waiting for elements to be visible or clickable before interacting.
  3. Use descriptive class names: When using By.className, ensure the class uniquely identifies the element.
  4. Avoid brittle locators: Prefer stable class names and avoid dynamic or auto-generated classes.
  5. Centralize configuration: Manage environment and browser settings in one place for easy changes.
Self Check

Where in this folder structure would you add a new page object class for a "Dashboard" page that uses findElement(By.className("dashboard-header")) to locate the header element?

Key Result
Organize Selenium Java tests using Page Object Model with clear folder layers and centralized config for maintainability.