0
0
Selenium Javatesting~15 mins

GitHub Actions configuration in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Automate Selenium Java test execution using GitHub Actions
Preconditions (3)
Step 1: Create a GitHub Actions workflow file named 'selenium-test.yml' in the '.github/workflows' directory
Step 2: Configure the workflow to trigger on push to the 'main' branch
Step 3: Set up the workflow to run on the latest Ubuntu runner
Step 4: Add steps to checkout the repository code
Step 5: Add steps to set up JDK 17
Step 6: Add steps to run 'mvn clean test' to execute Selenium tests
Step 7: Verify that the workflow completes successfully and test results are reported
✅ Expected Result: GitHub Actions workflow runs automatically on push to main branch, executes Selenium Java tests using Maven, and reports test results with a successful status if tests pass
Automation Requirements - GitHub Actions YAML
Assertions Needed:
Workflow triggers on push to main branch
JDK 17 is set up correctly
Maven clean test command runs successfully
Test results are reported and workflow completes with success
Best Practices:
Use official actions for checkout and setup-java
Use explicit JDK version
Keep workflow file organized with clear job and step names
Use caching for Maven dependencies (optional but recommended)
Automated Solution
Selenium Java
name: Selenium Java Tests

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v3

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

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

    - name: Run Maven tests
      run: mvn clean test

This GitHub Actions workflow is named 'Selenium Java Tests'. It triggers on any push to the 'main' branch.

The job named 'build' runs on the latest Ubuntu runner.

First, it checks out the repository code using the official actions/checkout@v3 action.

Then, it sets up JDK 17 using actions/setup-java@v3 with the Temurin distribution, ensuring the correct Java version for the Selenium tests.

Next, it caches Maven dependencies located in ~/.m2/repository to speed up future builds, using the hash of the pom.xml file as the cache key.

Finally, it runs the Maven command mvn clean test to compile and execute the Selenium tests.

If the tests pass, the workflow completes successfully and the test results are visible in the GitHub Actions interface.

Common Mistakes - 4 Pitfalls
{'mistake': 'Not specifying the JDK version explicitly', 'why_bad': 'The workflow may run with an unexpected Java version causing test failures or incompatibility', 'correct_approach': "Use the 'actions/setup-java' action with 'java-version' set explicitly to '17'"}
{'mistake': 'Not checking out the repository code before running tests', 'why_bad': 'The workflow will fail because the code to test is missing', 'correct_approach': "Include the 'actions/checkout' step before running any build or test commands"}
{'mistake': 'Running tests without cleaning the Maven project', 'why_bad': 'Old compiled files may cause inconsistent test results', 'correct_approach': "Run 'mvn clean test' to ensure a fresh build and test execution"}
{'mistake': 'Hardcoding runner OS without considering availability', 'why_bad': 'Using an unsupported or unavailable runner can cause workflow failures', 'correct_approach': "Use 'ubuntu-latest' which is a stable and widely supported runner"}
Bonus Challenge

Now add data-driven testing with 3 different login credentials in your Selenium Java tests and ensure the GitHub Actions workflow runs all test cases

Show Hint