Ultimate Guide On GitLab CI Automation Testing Tool – DevstringxRahul Sakthi
In this blog, we learn how to automate and execute test cases on the Gitlab pipeline step by step using selenium java.
- You must be familiar with the Selenium-Java concepts.
- You have an eclipse, JDK, maven, and git installed on your system.
- You should have a Gitlab account.
Follow these steps to create an automation script that supports Gitlab CI/CD Pipeline.
- Add dependencies within pom.xml.
- Create Base Driver Class
- Create Test Class
- Create a TestNG suite file
- Add plugins within XML
- Create GitLab yml file
- 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.
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.
- setDriver method helps to add the browser instance to ThreadLocal webdriver using a hashmap.
- getDriver method helps to obtain the browser instance ID which has been stored in the ThreadLocal webdriver hashmap.
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.
Step 4: Create a TestNG Suite File
Inside the project, add a folder named “TestSuite” and create an xml file called “DemoTestSuite”.
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.
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.
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:
- Log on to your GitLab account and open the automation project.
- Click the side panel ‘CI/CD’ and select the option ‘Pipeline’
- Click on the “Run Pipeline” option located at the top right corner
- Select your branch name and run the pipeline
- Next, you will see the pipeline being executed and check the status/progress bar.
- Following the run, the logs capture in the pipeline results.
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.