NashTech Blog

Integrating Java UI Tests into CI/CD with GitHub Actions

Table of Contents

Tired of broken buttons, failed logins, or surprise UI bugs in production? As a result, with Java Web UI tests running in GitHub Actions, you can catch visual and functional issues long before they reach your users.

Starting Java Web UI Testing with GitHub Actions

As DevOps continues to shape modern development practices, integrating Java Web UI tests with GitHub Actions has emerged as a robust solution for enhancing the CI/CD experience. First of all, Java is a robust programming language often used in large-scale enterprise applications. When paired with GitHub’s flexible automation platform, it becomes significantly easier to build resilient testing workflows. This strategy enables teams to automatically run full-stack tests every time new code is pushed to the repository. As a result, it helps reduce the risk of introducing bugs into production.

Why Java Web UI Tests in CI/CD Pipelines Matter

A software development lifecycle that moves quickly requires more than just manual testing. UI tests help catch layout issues, broken links, JavaScript errors, and functionality problems that backend or API tests can miss. With GitHub Actions, it’s possible to automate these tests and run them consistently across different branches, ensuring higher software quality and faster delivery cycles. In turn, failed tests lead to immediate alerts, helping developers act quickly and maintain release quality.

Java Web UI Testing Setup with Selenium

First, create a Java project using Maven or Gradle, and then include essential dependencies such as Selenium WebDriver and TestNG or JUnit. Tools like WebDriverManager simplify driver management. A typical pom.xml setup for Maven includes:

<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-java</artifactId>
  <version>4.12.0</version>
</dependency>
<dependency>
  <groupId>io.github.bonigarcia</groupId>
  <artifactId>webdrivermanager</artifactId>
  <version>5.4.1</version>
</dependency>

This forms the foundation for writing robust and scalable UI test cases.

Creating GitHub Actions Workflow for Java Web UI Tests

Workflows for GitHub Actions are specified in YAML files stored in the projects .github/workflows/ directory.

Example:

name: Java UI Tests

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  run-ui-tests:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: Set up JDK
      uses: actions/setup-java@v3
      with:
        java-version: '17'

    - name: Cache Maven dependencies
      uses: actions/cache@v3
      with:
        path: ~/.m2
        key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}

    - name: Run Tests
      run: mvn clean test

In most development pipelines, this process runs each time a change is pushed or a pull request targets the main branch.

Run Java Web UI Tests Headlessly in CI/CD Pipelines

GitHub-hosted CI servers don’t support a display. Headless mode is essential for running browser-based tests. To perform browser testing, run browsers in headless mode.

Chrome Headless Configuration for Java Web UI Tests

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless", "--disable-gpu", "--no-sandbox");
WebDriver driver = new ChromeDriver(options);

Installing Browsers in GitHub Runners

- name: Install Chrome
  run: sudo apt-get install -y google-chrome-stable

Java Web UI Test Maintenance with Page Object Model

The Page Object Model (POM) is a design pattern that organizes user interface logic into reusable components, which in turn makes test maintenance simpler and more efficient. Each page of your application is modelled as a class, containing locators and actions.

Example:

public class LoginPage {
  WebDriver driver;

  By username = By.id("username");
  By password = By.id("password");
  By loginBtn = By.id("login");

  public LoginPage(WebDriver driver) {
    this.driver = driver;
  }

  public void login(String user, String pass) {
    driver.findElement(username).sendKeys(user);
    driver.findElement(password).sendKeys(pass);
    driver.findElement(loginBtn).click();
  }
}

Improving Test Stability with Explicit Waits and Robust Assertions

Flakiness is one of the most significant problems with UI testing. Use WebDriverWait to handle dynamic elements and improve reliability:

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dashboard")));

Combine this with assert statements to validate expected outcomes using frameworks like JUnit or TestNG.

Debugging Java Web UI Tests with GitHub Actions Using Screenshots

In case of test failures, it helps to capture screenshots and logs. GitHub Actions allows uploading them as artefacts:

- name: Upload Artifacts
  if: failure()
  uses: actions/upload-artifact@v3
  with:
    name: test-screenshots
    path: target/screenshots/

Modify your Java code to take screenshots on failure and store them in the target directory.

Enhancing Test Coverage with HTML Reports and Surefire Plugins

Generate readable HTML reports using Maven Surefire plugin or Allure. These reports can also be uploaded as artifacts for post-run inspection. It helps in quickly identifying which test case failed and why.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.0.0-M5</version>
</plugin>

Managing and Securing Environment Variables and Secrets in GitHub Actions

Avoid hardcoding sensitive information. Use GitHub Secrets to manage credentials and tokens. Inject them into your workflow using environment variables:

- name: Run Tests
  run: mvn test
  env:
    LOGIN_USER: ${{ secrets.LOGIN_USER }}
    LOGIN_PASS: ${{ secrets.LOGIN_PASS }}

In your test scripts, retrieve them using System.getenv() In Java.

Advanced Workflow Strategies: Matrix Builds and Scheduled Runs

The matrix strategy allows you to run your tests on a variety of Java versions and browsers:

strategy:
  matrix:
    java: [11, 17]

You can also set up cron jobs to schedule tests to run automatically every night.

on:
  schedule:
    - cron: '0 1 * * *'

This allows proactive bug detection before users even experience issues.

Boosting Developer Confidence with Automated UI Feedback Loops

By integrating Java Web UI tests into GitHub Actions, developers receive immediate feedback on their code, ensuring UI stability and preventing regressions. This feedback loop not only increases confidence but also enables faster, safer releases.

The ability to identify problems early on is ultimately what makes any CI/CD pipeline successful. UI tests provide that crucial visibility, and GitHub Actions automates the entire process seamlessly.

CI/CD without UI tests is like flying blind. GitHub Actions makes it easy to automate them—so go ahead, wire them in and ship with confidence.

References

https://testgrid.io/blog/selenium-with-java-tutorial

https://www.geeksforgeeks.org/what-is-ci-cd

https://medium.com/@pathirage/step-in-to-ci-cd-a-hands-on-guide-to-building-ci-cd-pipeline-with-github-actions-7490d6f7d8ff

Picture of shreyaawasthi

shreyaawasthi

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

Scroll to Top