0
0
Selenium Javatesting~8 mins

findElement by tagName in Selenium Java - Framework Patterns

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

This structure organizes code by purpose: pages hold page objects, tests hold test cases, utils contain helpers like WebDriver setup, and config manages environment settings.

Test Framework Layers
  • Driver Layer: Manages browser setup and teardown (e.g., WebDriverFactory.java).
  • Page Objects: Classes representing web pages, encapsulating element locators and actions. For example, HomePage.java uses findElement(By.tagName()) to locate elements by tag name.
  • Test Layer: Contains test classes like HomePageTest.java that use page objects to perform tests and assertions.
  • Utilities: Helper classes for common functions like waits, logging, or data handling.
  • Configuration: Reads environment variables, browser types, and credentials to run tests flexibly.
Configuration Patterns

Use a config.properties file or environment variables to manage settings like:

  • Browser type (Chrome, Firefox, etc.)
  • Base URL of the application
  • Timeout durations
  • User credentials (stored securely)

Example snippet from ConfigReader.java:

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ConfigReader {
    private static Properties props = new Properties();

    static {
        try (InputStream input = new FileInputStream("src/test/resources/config.properties")) {
            props.load(input);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String get(String key) {
        return props.getProperty(key);
    }
}
Test Reporting and CI/CD Integration

Use TestNG or JUnit built-in reports for test results.

  • Generate HTML reports showing passed/failed tests.
  • Integrate with CI/CD tools like Jenkins or GitHub Actions to run tests automatically on code changes.
  • Use logs and screenshots on failure for easier debugging.
Best Practices
  • Use Page Object Model: Keep locators and actions in page classes to separate test logic from UI details.
  • Prefer explicit waits: Wait for elements found by findElement(By.tagName()) to be visible or clickable to avoid flaky tests.
  • Use meaningful tag names: When using By.tagName, ensure the tag uniquely identifies the element or combine with other locators.
  • Keep config flexible: Allow switching browsers and environments without code changes.
  • Write clean, readable tests: Use descriptive test method names and comments.
Self Check

Question: Where in this folder structure would you add a new page object class for a Login page that uses findElement(By.tagName()) to locate input fields?

Key Result
Organize Selenium Java tests using Page Object Model with clear layers for pages, tests, utils, and config.