How to Run Selenium Automation Tests on Gitlab CI/CD & Github Actions?

TL;DR: Use headless driver and this appropriate version of this image according to your needs.

Emre Baş
Dolap Tech
2 min readDec 10, 2021

--

Image by Jirsak on iStock

Why?

You may need to run your end-to-end tests on the project after small/big/harmless(who believes that!?) PR is merged into master branch. Your customer may have noticed that PR changed something else before your team or as a team, you don’t have enough resources in your team.

What is Headless Driver?

It’s a web browser that you are using to read this super article right now but it does not have any graphical user unit (GUI). It will interact with web pages as your current browser does. It can be controlled programmatically.

Google released headless version of Chrome just 4 years ago.

What does example repo do?

In example repos (Github & Gitlab) for this blog post, you can find the creation and usage of headless Chrome driver for interact web elements.

Also, you can use parallel test execution with EdgeDriver and FirefoxDriver by creating their objects and running them in a Docker container.

According to your needs, you can use them as a boilerplate project.

Project

Prerequisites

  • Java (choose your SDK)
  • Maven (I am more comfortable with Maven than Gradle, it is up to you!)
  • A GitHub/Gitlab account

Step 1

I will create a Base class and it will have the creation of WebDriver and its wait. Also, WebDriver specifications will be provided here.

Base class for web UI tests

Step 2

I tend to use page object model for UI tests as much as I can. Selenium provides PageFactory class, it will make page object mode make easier and does not require the initialization of every object.

Elements and actions of the home page

Step 3

So far, so good. There is something missing. Test class. It’s a very simple test, we are clicking only two elements, no assertion.

A simple test for home page

Gitlab section

You need to add .gitlab-ci.yml file to your project and push your commit to master branch.

.gitlab-ci.yml

The result can be found here.

Github section

Click Actions-> Set up this workflow on Java with maven on repo folder.

Default Github Actions page

Change the last line as “mvn clean test” and commit.

yml file for GitHub Actions

Check the result here.

Conclusion

--

--