How To Use Selenium With CI/CD Pipelines For Automated Testing

How To Use Selenium With CI/CD Pipelines For Automated Testing

Automating the Selenium test process in response to code changes guarantees early problem discovery and seamless, reliable update delivery. Popular test automation tool Selenium is essential for increasing test execution, CI/CD pipeline efficiency, and the speed at which reliable software is delivered. It is an effective and adaptable framework for web application automated testing. It is an essential component of CI/CD pipelines because it can manage many test scenarios and allows parallel and cross-browser testing. It also aids in running tests in various production and testing settings to confirm the software’s functioning and stability in various scenarios. Repeating test cases takes a lot of time to run through manual testing; however, automation testing with Selenium can significantly cut down on this time and effort. The following steps are related to the guidelines necessary for the process of using Selenium with Ci/Cd pipelines for effective automated testing outcomes.

 

Step 1: Establishing Your CI/CD Environment

 

https://journeyofquality.com/wp-content/uploads/2019/11/capture.png?w=736

 

To begin utilizing Selenium with CI/CD pipelines, the starting step is establishing your CI/CD environment. That environment coordinates the automation of your software build, test, and deployment processes. Typically, Jenkins, GitLab CI, and CircleCI tools are utilized to oversee these pipelines.

 

Start by choosing and arranging a CI/CD tool that coordinates well with your version control system, like Git. For instance, in GitLab CI, you need to form a .gitlab-ci.yml file within the root of your repository to represent the pipeline stages. The following code includes a basic case:

 

stages:

– build

– test

build-job:

stage: build

script:

– echo “Building the project”

test-job:

stage: test

script:

– echo “Running Selenium tests”

 

In this arrangement, build and test are stages in your pipeline. The test job is where Selenium tests will, in the long run, be executed. Confirm that the CI/CD server or platform you employ has satisfactory resources and has an approach to the vital tools for driving Selenium tests.

 

Establishing the environment includes installing and arranging these tools and making sure that they work consistently along with your code repository. Appropriate setup affirms the foundation for joining Selenium tests into the pipeline, empowering automated testing with each code change.

 

Step 2: Incorporate Selenium And Your Testing Framework

 

https://pypi-camo.freetls.fastly.net/a5ceabc47f538dac23df95ef039c0453aa3b95e6/68747470733a2f2f6769746875622e636f6d2f7a6f6e63612f7079746573742d6970796e622f7261772f6d61737465722f696d672f7079746573742d6970796e625f6f75747075742e706e67

 

The second step is to incorporate Selenium along with your selected testing framework. This incorporation permits Selenium to perform automated tests inside your CI/CD pipeline. According to your programming language and testing needs, you need to select a framework that supports Selenium.

 

A standard testing system for Python is PyTest. Begin by installing Selenium and PyTest in your project environment using:

 

pip install selenium pytest

Following is a basic case of a Selenium test script utilizing PyTest:

# test_example.py

import pytest

from selenium import webdriver

def test_google_search():

driver = webdriver.Chrome()

driver.get(“https://www.google.com”)

assert “Google” in driver.title

driver.quit()

For Java, JUnit could be a renowned option. Include the Selenium dependency in your pom.xml file:

<dependency>

<groupId>org.seleniumhq.selenium</groupId>

<artifactId>selenium-java</artifactId>

<version>4.0.0</version>

</dependency>

Following is a simple JUnit test utilizing Selenium:

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.junit.Test;

import static org.junit.Assert.assertTrue;

public class GoogleSearchTest {

@Test

public void testGoogleSearch() {

WebDriver driver = new ChromeDriver();

driver.get(“https://www.google.com”);

assertTrue(driver.getTitle().contains(“Google”));

driver.quit();

}

}

 

Joining Selenium to your testing framework guarantees that automated tests can be conducted viably during the CI/CD process, approving your application’s capability and implementation.

 

Step 3: Configuring Your Selenium Test Scripts

 

https://cdn.prod.website-files.com/619e15d781b21202de206fb5/62663945f84dfc563dd48fd1_xWl1Ky-BZpxyZYNenY3nYwqxFmzTC5oQK4Y2eLVHP9PjxT-nVFuJqPZCe_RfwsaM76lDX-vK7ZEM5zs4ZTjOA3H8M4ZuKXT8OpOLgPFz6bj2ktpeIxvDHi9Z2MByWyzjP0EcMPUH.png

 

The third step includes configuring your Selenium test scripts for the CI/CD pipeline, which is fundamental to confirm that they run smoothly in an automated setup. It includes adjusting your tests to function in a headless mode and making them congruous with the pipeline’s limitations.

 

In Python-based Selenium tests, adjust your script to run Chrome in headless mode using the following code:

 

from selenium import webdriver

from selenium.webdriver.chrome.options import Options

def test_google_search():

chrome_options = Options()

chrome_options.add_argument(“–headless”) # Run in headless mode

driver = webdriver.Chrome(options=chrome_options)

driver.get(“https://www.google.com”)

assert “Google” in driver.title

driver.quit()

 

Driving browsers in headless mode permits tests to perform without a graphical user interface, making them appropriate for server situations utilized in CI/CD pipelines.

 

For Java tests with JUnit, design the browser to run headlessly. Follow the given code to set Chrome to headless mode:

 

import org.openqa.selenium.chrome.ChromeOptions;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.junit.Test;

import static org.junit.Assert.assertTrue;

public class GoogleSearchTest {

@Test

public void testGoogleSearch() {

ChromeOptions options = new ChromeOptions();

options.addArguments(“–headless”); // Run in headless mode

WebDriver driver = new ChromeDriver(options);

driver.get(“https://www.google.com”);

assertTrue(driver.getTitle().contains(“Google”));

driver.quit();

}

}

 

Make sure that your test scripts are self-contained and don’t depend on manual inputs or graphical components. Arrange them to manage environment-specific factors, such as URLs or credentials, utilizing environment variables or configuration files. This approach guarantees that your tests run smoothly and assuredly inside the CI/CD pipeline, giving precise results with each move.

 

Step 4: Installing The Essential Dependencies

 

https://qaautomation.expert/wp-content/uploads/2023/08/image-75.png?w=1200

 

This step is about installing the essential dependencies to run Selenium tests in your CI/CD pipeline. These dependencies incorporate the Selenium WebDriver, browser drivers like ChromeDriver or GeckoDriver, and the browser itself, if not already accessible within the pipeline environment.

 

For Python projects utilizing Selenium, you can alter your CI/CD configuration file to guarantee the installation of dependencies before the tests are performed. Following is an illustration of a GitLab CI configuration:

 

test-job:

stage: test

image: python:3.8

before_script:

– pip install selenium # Install Selenium

– wget https://chromedriver.storage.googleapis.com/91.0.4472.101/chromedriver_linux64.zip

– unzip chromedriver_linux64.zip

– mv chromedriver /usr/local/bin/

– apt-get update && apt-get install -y chromium # Install Chrome browser

script:

– pytest

 

According to this illustration, the pip install selenium command guarantees that Selenium WebDriver is accessible within the environment. Secondly, the wget command gets the ChromeDriver, which is essential for executing the Selenium tests in a Chrome browser. The driver is unfastened and moved to a directory where it can be gotten to. Further, the apt-get install -y chromium command can install the Chromium browser, a flimsy form of Chrome appropriate for headless testing.

 

In the case of Java projects, you’d adjust the configuration similarly to guarantee that Maven is installed, together with any fundamental dependencies from the pom.xml file. The given illustration employs Maven to oversee dependencies in a Java task:

 

test-job:

stage: test

image: maven:3.6.3-jdk-8

script:

– mvn install # Install dependencies

– mvn test # Run Selenium tests

 

This structure installs dependencies and plans the environment, guaranteeing that Selenium tests can run automatically in your CI/CD pipeline.

 

Step 5: Configuring Your CI/CD Pipeline To Run Selenium Tests

 

Along with your environment and dependencies in form, the following phase is to configure your CI/CD pipeline to implement Selenium tests automatically. That will guarantee that tests are conducted with each code change or build, giving quick input on the quality of the code.

 

In Python projects utilizing GitLab CI, you’ll be able to indicate the test execution command in your .gitlab-ci.yml file like:

 

test-job:

stage: test

image: python:3.8

before_script:

– pip install selenium

– wget https://chromedriver.storage.googleapis.com/91.0.4472.101/chromedriver_linux64.zip

– unzip chromedriver_linux64.zip

– mv chromedriver /usr/local/bin/

– apt-get update && apt-get install -y chromium

script:

– pytest # Run Selenium tests using PyTest

 

In the given command, the pytest command initiates the execution of your Selenium test scripts. The before_script segment establishes the basic environment, whereas the script section runs the tests.

 

In Java projects utilizing Maven, upgrade your Jenkinsfile or .gitlab-ci.yml file to incorporate the test execution step:

 

test-job:

stage: test

image: maven:3.6.3-jdk-8

script:

– mvn install # Install dependencies

– mvn test # Run Selenium tests

 

The mvn test command drives your JUnit or TestNG tests, including those composed with Selenium. This step guarantees that your Selenium tests are executed as components of the CI/CD pipeline, giving profitable input on code quality and functionality.

 

By joining test execution into your CI/CD pipeline, you automate the method of validating your web application with each code change, making a difference in detecting issues early and keeping up high software quality.

 

Step 6: Reporting The Test Results

 

https://www.testmo.com/wp-content/uploads/2024/06/YXNzZXRzL3NjcmVlbnNob3RzL2F1dG9tYXRpb24tcnVucy1pbmRleC1mdWxsLnBuZw-1.png

 

Once you have run Selenium tests in your CI/CD pipeline, the final step is to examine and report the results of the test. This aids in rapidly recognizing issues and sustaining high quality by giving detailed insights into test results.

 

In Python projects utilizing GitLab CI, you’ll be able to configure the pipeline to produce and display test reports. Below is an example of how to adjust your .gitlab-ci.yml file to deliver and store test reports:

 

test-job:

stage: test

image: python:3.8

before_script:

– pip install selenium

– wget https://chromedriver.storage.googleapis.com/91.0.4472.101/chromedriver_linux64.zip

– unzip chromedriver_linux64.zip

– mv chromedriver /usr/local/bin/

– apt-get update && apt-get install -y chromium

script:

– pytest –junitxml=report.xml # Generate test report in JUnit XML format

artifacts:

paths:

– report.xml # Store the test report

expire_in: 1 week # Retain the report for one week

 

In this setup, the pytest –junitxml=report.xml command creates a test report in JUnit XML format, and the artifacts segment guarantees the report file is stored as an artifact, making it accessible for download and audit.

 

In Java projects with Maven, you’ll configure your Jenkinsfile or .gitlab-ci.yml file to handle test results in the same way:

 

test-job:

stage: test

image: maven:3.6.3-jdk-8

script:

– mvn install

– mvn test # Run tests and generate reports

artifacts:

paths:

– target/surefire-reports # Directory containing test reports

expire_in: 1 week

 

In this example, Maven automatically creates test reports within the target/surefire-reports directory, which are stored as artifacts at that point.

 

Joining reporting into your CI/CD pipeline enables teams to rapidly recognize and address issues, enhancing overall software quality and simplifying the development workflow. The configuration of detailed test reports and storing them as artifacts gives visibility into test outcomes, encouraging better decision-making and more immediate resolution of imperfections.

 

Conclusion

 

In summary, the software development environment has become more competitive in recent times, and CI/CD has come forward with a revolutionary framework. The integration of Selenium into the CI/CD pipeline can offer reliable software, improve test automation, and shorten test execution times. It ensures that your web applications are completely tested at every degree of development. It also covers various types of testing, including cross-browser testing, regression testing, or parallel testing; Selenium offers the adaptability and power to fortify and optimize the whole development process. In short, Selenium is an essential tool for test automation and a vital aspect of reaching optimal CI/CD performance.

No Comments

Post a Comment

Comment
Name
Email
Website