Ultimate Guide On GitLab CI Automation Testing Tool – Devstringx

Back to Blog

Ultimate Guide On GitLab CI Automation Testing Tool – Devstringx

In this blog, we learn how to automate and execute test cases on the Gitlab pipeline step by step using selenium java.

Prerequisite:

  1. You must be familiar with the Selenium-Java concepts.
  2. You have an eclipse, JDK, maven, and git installed on your system.
  3. You should have a Gitlab account.

Follow these steps to create an automation script that supports Gitlab CI/CD Pipeline.

Steps:

  1. Add dependencies within pom.xml.
  2. Create Base Driver Class
  3. Create Test Class
  4. Create a TestNG suite file
  5. Add plugins within XML
  6. Create GitLab yml file
  7. Run tests on GitLab pipeline

Step 1: Add Dependencies Within pom.xml

The below dependencies “webdrivermanager”, “selenium”, and “testing” require in the pom.xml file.

pom.xml

 

 

 

 

 

 

 

 

Step 2: Create Base Driver Class

In src/test/package create a package called ‘init’ inside of the package create a new class named ‘DriverFactory’

Inside the constructor, the logic of the base driver logic is written into a switch condition block. We added driver initialization to the chrome browser along with the operating system that supports Linux and Windows. In addition, we can implement the same logic for different browsers.

To set up the web driver we use WebDriverManager in place of binary files (chrome driver.exe)

As the pipeline performs the tests in the headless mode, the “–headless” option is added and the “window-size=1280,1024” is optional.

To Solve the browser crash issue in the pipeline, we make sure that the chrome options should have ‘no-sandbox’, ‘disable-gpu’, and ‘disable-dev-shm-user’ options.

In order to overcome a duplicate web driver instance, we use the getter and setter methods to get the actual web driver instance.

  1. setDriver method helps to add the browser instance to ThreadLocal webdriver using a hashmap.
  2. getDriver method helps to obtain the browser instance ID which has been stored in the ThreadLocal webdriver hashmap.

Driver Class

Read Also:- Cucumber Automation Testing Tool

Step 3: Create Test Class

In src/test/java create a class named “DemoGitLabTest”.The test class contains the TestNG method implementation in which the browser configuration is set into startUp (beforeMethod), the browser close into tearDown (afterMethod), and the test script is written under the test annotations.

Create Test Class

Step 4: Create a TestNG Suite File

Inside the project, add a folder named “TestSuite” and create an xml file called “DemoTestSuite”.

TestNG suite file

Step 5: Add Plugins With the pom.xml File

To run the TestNG tests using Maven, we have to add a maven surefire plugin and inside the configuration provide the path to the TestNG suite where the XML file is located.

pom.xml file

Read Also:- Behavior Driven Development Testing Tool

Step 6: Create Gitlab yml file

Within the project, create a yml file named “GitLab-ci”. The steps to follow when writing the code into the yml – Gitlab Runner Configuration variable are in the following order.

  1. Stages
  2. Jobs
  3. Tags

Gitlab yml file

In the GitLab-ci yml file, stages are added in the names ‘chrome’ and ‘test’ the GitLab runner will execute the stage as block per block, each block containing a stage.

Following that, jobs were created in the names ‘‘chrome-install-job’ and ‘unit-test-job’. Inside the job, there should be an ‘image’ ‘stage’ ’script’

  • The image is a reference to the docker image we added to the tag in the file.
  • The stage is the block that executes the code block per block
  • The script is a command that runs our code through CLI.

Step 7: Run Tests On the Gitlab Pipeline

On your local machine, commit and push the latest code to Git and follow the steps below to run the tests:

  1. Log on to your GitLab account and open the automation project.
  2. Click the side panel ‘CI/CD’ and select the option ‘Pipeline’
  3. Click on the “Run Pipeline” option located at the top right corner
  4. Select your branch name and run the pipeline
  5. Next, you will see the pipeline being executed and check the status/progress bar.
  6. Following the run, the logs capture in the pipeline results.

Gitlab pipeline

If you are interested in even more software testing-related articles and information from us here at Devstringx, then we have a lot to choose from for you.

Share this post

Back to Blog