Executing Selenium Tests ( Maven + Java ) with CI GitHub Actions

Saurabh Dubey
4 min readJul 17, 2020

--

In this article, I will explain the integration between GitHub actions and Selenium WebDriver for running Web UI tests, scripted in Java with Maven as build tool . This solution will help us to set up our CI server using Github Actions to run tests on it.

Github Actions ? What actually in Actions ?

Github Actions
Github Actions

GitHub Actions makes it easy to automate all build workflows with CI/CD. We can Build, Test, and deploy code right from GitHub code repository. Make code reviews, branch management, and issue triaging work the way you want. It is all about create Workflow and just run any Github event.

Lets Start With Creating Selenium project in Java

1- Create a new “Maven project” in your favourite IDE ( Eclipse or IntelliJ IDEA etc)

2- Open the pom. xml file and declare the dependencies. I have used Selenium, TestNG and WebdriverManager.

3- After declaring the dependencies in pom.xml, build the project running command in your root project folder.

mvn build 

4- Create new test as Java class under src/test/java

import io.github.bonigarcia.wdm.WebDriverManager;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.chrome.ChromeOptions;import org.testng.Assert;import org.testng.annotations.AfterClass;import org.testng.annotations.BeforeClass;import org.testng.annotations.Test;import java.util.concurrent.TimeUnit;public class SeleniumGitCI{private WebDriver driver;@BeforeClasspublic void setUp(){WebDriverManager.chromedriver().setup();ChromeOptions options = new ChromeOptions();options.addArguments("--no-sandbox");options.addArguments("--disable-dev-shm-usage");options.addArguments("--headless");driver = new ChromeDriver(options);driver.navigate().to("https://www.google.com");driver.manage().window().maximize();driver.manage().timeouts().implicitlyWait(120, TimeUnit.MILLISECONDS);}@Testpublic void userLogin(){WebElement searchTxt = driver.findElement(By.name("q"));searchTxt.sendKeys("automation");WebElement submitBtn = driver.findElement(By.name("btnK"));submitBtn.click();System.out.println("Current URL is:" + driver.getCurrentUrl());Assert.assertTrue(driver.getTitle().contains("automation - Google Search"));System.out.println("Current Title is:" + driver.getTitle());}@AfterClasspublic void tearDown(){if (driver != null) {driver.quit();     }  }}

5- You can now run the test using below command in root folder of project

mvn clean test 

Now let’s run our test using Github Actions

As you would have noticed that we are running our tests in headless mode as we have added below ChromeOptions

ChromeOptions options = new ChromeOptions();
options.addArguments("--no-sandbox");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--headless");

We don't have to worry about Chrome driver executables as this has been managed by Webdriver Manger dependency.

Now as we have to run this tests using GitHub Actions CI, it will execute on Linux machine. And lets assume that Linux machine does not have Chrome installed. So lets create a folder name scripts in your project root folder and create InstallChrome.sh. Add the following script in the file.

#!/bin/bash
set -ex
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo apt install ./google-chrome-stable_current_amd64.deb

Lets Integrate Selenium Project with Github Actgions

1- Create Github repository and push the code.

2- Click on Actions tab and click New Workflow

3- Click “Set up this Workflow” under Java with Maven

4- Replace the maven.yml file with below

name: Java CI with Mavenon:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-lateststeps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Install Google Chrome # Using shell script to install Google Chrome
run: |
chmod +x ./scripts/InstallChrome.sh
./scripts/InstallChrome.sh
- name: Build with Maven
run: mvn test --file pom.xml

5- Click the “Start Commit”

6- Select the Create a new branch and click the propose new file

7- Create a pull request.Once created you go to your pull request and you will find that the workflow is triggered and you can click on Actions to check the console for the logs

8- Once completed you go to check your console message outcome under Build with Maven.

9- Now you can merge your branch to apply the changes

Congratulations! we just created our CI workflow for executing Selenium tests with Java and Maven.

--

--