Running Selenium Web Tests with GitHub Actions

Running Selenium Web Tests with GitHub Actions

In this article, I will explain the integration between GitHub actions and Selenium WebDriver for running Web UI tests. This solution will help us to set up our CI server without any headache like Jenkins CI.

What is Selenium WebDriver?

Selenium WebDriver drives a browser natively, as a real user would, either locally or on remote machines.

Through a simple setup, WebDriver can be used with all major browsers. Automate real user interactions in Firefox, Safari, Edge, Chrome, Internet Explorer and more!

Selenium WebDriver refers to both the language bindings and the implementations of the individual browser controlling code. This is commonly referred to as just WebDriver.

You can check more details here

What about GitHub Actions?

No alt text provided for this image

GitHub Actions make it easy to automate all your software workflows, now with world-class CI/CD. Build, test, and deploy your code right from GitHub. Make code reviews, branch management, and issue triaging work the way you want.

Run a workflow on any GitHub event

Kick-off workflows with GitHub events like push, issue creation, or a new release. Combine and configure actions for the services you use, built and maintained by the community.

You can know more about the features from this link


Create our Selenium Web Project

1- Open IntelliJ IDEA and select Create New Project

No alt text provided for this image

2- Select Gradle from the templates and click Next

No alt text provided for this image

3- Rename your project and click Finish

4- In the build.gradle file we need to add our dependencies for Selenium WebDriver, TestNG and WebDriver Manager (will know why)

dependencies {
    testCompile group: 'org.testng', name: 'testng', version: '7.1.0'
    compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.141.59'
    testCompile("io.github.bonigarcia:webdrivermanager:3.8.1")

}


test { //this is the gradle task to be executed
    useTestNG() { //Tells Gradle to use TestNG
    }
}

5- Create new Java class under src/test/java with a name for example SeleniumDemo

6- Inside the class, we will add the following code:

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 SeleniumDemo
{
    private WebDriver driver;

    @BeforeClass
    public 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://the-internet.herokuapp.com/login");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(120, TimeUnit.MILLISECONDS);
    }

    @Test
    public void userLogin()
    {
        WebElement usernameTxt = driver.findElement(By.id("username"));
        usernameTxt.sendKeys("tomsmith");
        WebElement passwordTxt = driver.findElement(By.id("password"));
        passwordTxt.sendKeys("SuperSecretPassword!");
        WebElement submitBtn = driver.findElement(By.className("radius"));
        submitBtn.click();
        System.out.println("Current URL is:" + driver.getCurrentUrl());
        Assert.assertTrue(driver.getCurrentUrl().contains("secure"));
    }

    @AfterClass
    public void tearDown(){
        if (driver != null) {
            driver.quit();
        }
    }
}

In the above code, we are opening our application under test The Internet and goto the login page then enter the username and password and click the login button, after that we are asserting that the secure page is displayed from the URL.

7- you can now run your test from IntelliJ IDEA as shown below

No alt text provided for this image

8- You can also run it from the command line with the following command:

 ./gradlew clean test --info

After the test passed we can now go to the second part for running our test using GitHub Actions

The Tricky Part ;)

You will be noticed that we need to run our tests in a headless mode because that we add the following Chrome options:

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

And also because we don't need to think about the Chromedriver.exe we are using WebDriver Manager to handle that instead of adding it in our project like the following:

WebDriverManager.chromedriver().setup();


But wait do we have a Chrome Browser installed on the machine ??

Let's assume that we don't have it, So we will create a shell script for installing the Chrome browser using the apt-get command (Because we will use a Linux Machine this time :D)

9- In the project root folder create a new folder with name scripts, create a new shell script with name InstallChrome.sh and add the following script:

#!/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

Now we are ready to integrate and running our code with GitHub Action

Integrate our Selenium Project with GitHub Actions

1- Create your GitHub repository and push your code to be ready this step

2- Click on Actions tab and click New Workflow

No alt text provided for this image

3- Select Gradle Workflow

No alt text provided for this image

4- We will replace the current workflow with the following yml file and change the file to be main.yml instead of gradle

name: Selenium Java CI

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest # Using linux machine

    steps:
    - uses: actions/checkout@v2 # Checkout the code
    - name: Set up JDK 1.8
      uses: actions/setup-java@v1 # Setup JAVA
      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: Grant execute permission for gradlew
      run: chmod +x gradlew # give permission to Gradle to run the commands
    - name: Build with Gradle
      run: ./gradlew test --info # Run our tests using Gradle

5- Commit the change and create a Pull Request to add the file and to be able to check the workflow

No alt text provided for this image

6- And now go to the PR and you will find that the workflow is triggered and you can click on Actions to check the console for the logs

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

You will notice that our print message is displayed with the text that we are using

No alt text provided for this image

And also we can add one more step to get the test report as shown above.

7- You can merge your branch now to apply the changes

And the final result we will have a new folder in our project .github/workflows including the main.yml file for the workflow

No alt text provided for this image

Congratulations! we just created our CI workflow for running our Appium test cases for Android App.

You can check the repository from the following link


The Conclusion

With GitHub Actions, you can set up your CI pipeline and running your tests in parallel with few steps.

Thank you for reading

Happy Testing

Moataz

Iliyan Velichkov

Head of Java Technologies at codbex

1 年

Moataz Nabil you can setup chrome using the following github action: https://github.com/marketplace/actions/setup-chrome

回复
Oscar Carrillo

Senior Quality Assurance Engineer

2 年

What if I want to perform my tests in Windows OS, don't know if it's necesary to install chrome also?

回复
Michael Shaffer

Seanior Engineer/Team Lead at Sierra7, Inc.

3 年

Ok. I seem to be missing something. This looks like it just runs Selenium tests on a site that is already up. Pretty much 100% useless for GitHub Actions. Who cares what the production site is doing. The tests need to be run on new code. Yet this article ignores that from what I can see.

回复
Temirlan Utegenov

Senior QA / Team Lead Test Engineer. ISTQB certified

3 年

Thank you! A very helpful article

回复

要查看或添加评论,请登录

Moataz Nabil的更多文章

社区洞察

其他会员也浏览了