Selenium with Java · Building a Framework

Selenium Framework Structure, Reports and CI

Lesson 9 of 9 · 12 min read

A framework is just your tests plus the code that makes them easy to write, run and debug. Here is a structure that is simple enough for a beginner and good enough to show in an interview.

Folder structure

selenium-practice/
  pom.xml
  testng.xml
  src/test/java/
    base/
      BaseTest.java          start and quit the browser, screenshot on failure
      DriverFactory.java     creates Chrome, Firefox or Edge
    pages/
      BasePage.java
      LoginPage.java
      AccountPage.java
    tests/
      LoginTest.java
      RegisterTest.java
    utils/
      Config.java            reads config.properties
      TestData.java          unique emails, names, dates
  src/test/resources/
    config.properties

Config instead of hard-coded values

# src/test/resources/config.properties
baseUrl=https://testqualityhub.com
browser=chrome
headless=false
public final class Config {
    private static final Properties props = new Properties();

    static {
        try (InputStream in = Config.class.getResourceAsStream("/config.properties")) {
            props.load(in);
        } catch (IOException e) {
            throw new IllegalStateException("Cannot read config.properties", e);
        }
    }

    public static String get(String key) {
        return System.getProperty(key, props.getProperty(key));
    }
}

System.getProperty first means you can override any value from the command line: mvn test -Dbrowser=firefox -Dheadless=true.

Driver factory

public final class DriverFactory {
    public static WebDriver create() {
        boolean headless = Boolean.parseBoolean(Config.get("headless"));
        switch (Config.get("browser")) {
            case "firefox":
                FirefoxOptions ff = new FirefoxOptions();
                if (headless) ff.addArguments("-headless");
                return new FirefoxDriver(ff);
            default:
                ChromeOptions chrome = new ChromeOptions();
                if (headless) chrome.addArguments("--headless=new");
                return new ChromeDriver(chrome);
        }
    }
}

BaseTest with a screenshot on failure

public abstract class BaseTest {
    protected WebDriver driver;

    @BeforeMethod
    public void setUp() {
        driver = DriverFactory.create();
    }

    @AfterMethod
    public void tearDown(ITestResult result) throws IOException {
        if (result.getStatus() == ITestResult.FAILURE) {
            File shot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            Files.createDirectories(Path.of("target/screenshots"));
            Files.copy(shot.toPath(), Path.of("target/screenshots/" + result.getName() + ".png"),
                    StandardCopyOption.REPLACE_EXISTING);
        }
        driver.quit();
    }
}

Reports

TestNG writes a basic HTML report to target/surefire-reports. For a nicer report with screenshots, add Allure or ExtentReports. Pick one, set it up once, and attach the failure screenshot to it.

Run in CI with GitHub Actions

# .github/workflows/tests.yml
name: UI tests
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: 17
      - run: mvn -B test -Dheadless=true
      - uses: actions/upload-artifact@v4
        if: always()
        with:
          name: test-results
          path: target/

Chrome is already installed on GitHub's Ubuntu runners, and Selenium Manager gets the driver.

Good habits that make a framework last

  • Every test creates its own data and can run alone, in any order.
  • Use unique values (add a timestamp to emails) so parallel runs do not clash.
  • No Thread.sleep, no absolute XPath, no passwords in code.
  • Test names describe behaviour: wrongPasswordShowsError, not test2.
  • Put the project on GitHub with a README. It is your portfolio.
You finished the Selenium track. Next, learn to test the layer under the UI in the API Testing tutorials. API tests are faster and catch bugs earlier.
All 9 lessons in Selenium with Java
  1. What Is Selenium WebDriver?
  2. Set Up Java, Maven and Selenium
  3. Write Your First Selenium Test
  4. Selenium Locators: ID, CSS, XPath and More
  5. Clicks, Typing, Dropdowns, Alerts, Frames and Windows
  6. Waits in Selenium: Implicit, Explicit and Fluent
  7. TestNG for Selenium: Annotations, Assertions and Data
  8. Page Object Model in Selenium Java
  9. Selenium Framework Structure, Reports and CI