GitHub Actions configuration in Selenium Java - Build an Automation Script
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.
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
Practice
Solution
Step 1: Understand the role of GitHub Actions workflow
The workflow file defines automated steps that run on events like code changes.Step 2: Connect workflow to Selenium Java tests
It automates running Selenium Java tests whenever code is pushed or a pull request is made.Final Answer:
To automate running Selenium Java tests on code changes -> Option CQuick Check:
Workflow automates tests = D [OK]
- Confusing workflow with writing test code
- Thinking tests run only manually
- Assuming workflow stores test results
Solution
Step 1: Identify correct action usage syntax
The official way to set Java version is using 'uses' with 'actions/setup-java@v3' and 'with' parameters.Step 2: Check the correct parameter name and format
The parameter is 'java-version' with the version as a string, e.g., '17'.Final Answer:
uses: actions/setup-java@v3 with: java-version: '17' -> Option BQuick Check:
Correct action syntax = C [OK]
- Using 'run' instead of 'uses' for setup-java
- Missing 'with' keyword
- Incorrect parameter name or format
on:
push:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
with:
java-version: '11'
- run: mvn testSolution
Step 1: Analyze the trigger event
The workflow triggers on push events to the 'main' branch only.Step 2: Check Java version and test command
Java 11 is set up, and 'mvn test' runs the tests.Final Answer:
The workflow runs tests using Java 11 on push to main branch -> Option AQuick Check:
Push to main triggers tests with Java 11 = B [OK]
- Assuming tests run on all branches
- Confusing Java 11 with Java 17
- Ignoring the trigger event
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
with:
java-version: 11
- run: mvn clean testSolution
Step 1: Check the 'java-version' format
The 'java-version' parameter must be a string, so it should be '11' with quotes.Step 2: Verify other parts
Though 'on' is missing, the question focuses on the snippet error; 'mvn clean test' is correct; 'actions/checkout@v3' is current.Final Answer:
java-version should be a string, e.g., '11' -> Option AQuick Check:
Java version needs quotes = A [OK]
- Using number instead of string for java-version
- Confusing order of mvn commands
- Assuming checkout action is outdated
Solution
Step 1: Understand event triggers for push and pull_request
You want both push and pull_request events to trigger on 'develop' and 'release' branches.Step 2: Check correct syntax for multiple branches
Use 'branches' with a list of branch names under both push and pull_request keys.Final Answer:
on: push: branches: [develop, release] pull_request: branches: [develop, release] -> Option DQuick Check:
Push and PR on develop, release branches = A [OK]
- Using branches-ignore instead of branches
- Triggering on wrong branches like main
- Separating branches for push and pull_request incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
