For many developers, the absence of automated testing could mean their work is at risk or unreliable. Combined with the open-source automation server Jenkins, the popular web testing framework Selenium forms a powerful toolkit for managing testing automation.

This article will explore Jenkins and how to integrate them for automated testing with Selenium.

What is Jenkins?

Jenkins is an automation tool, primarily an open-source server, that allows developers to build, test, and deploy software. Written in Java, it requires Java to run. Jenkins facilitates continuous integration of projects (jobs) and supports end-to-end automation.

Jenkins continuously automates software builds, identifying errors early in the development cycle. Its popularity is bolstered by a strong community. Jenkins is highly customizable and offers various plugins, making it versatile and adaptable to diverse requirements.

Jenkins offers a variety of actions, including:

  • Software Builds: Utilizing build systems such as Gradle or Maven.
  • Automation Testing: With frameworks like Nose2, PyTest, Robot, Selenium, etc.
  • Executing Test Scripts: Through Windows terminal, Linux shell, etc.
  • Generating Test Reports: Displaying test results and performing post-test actions.
  • Exploring Test Scenarios: Testing different input combinations for improved coverage.
  • Continuous Integration (CI): Automating artefact creation and testing to detect issues early.
  • Plugin Support: Jenkins has nearly 1500 plugins contributed by the community, enabling extensive customization and accelerating various project activities like building, deploying, and automating tasks.

Features Of Jenkins

Here are the features offered by Jenkins:

  • Easy Setup: Jenkins is a Java-based application that installs effortlessly on various operating systems, including Windows, macOS, and Unix-based systems. Its straightforward installation process offers options for traditional installers or portable WAR files. With a user-friendly web interface, Jenkins simplifies configuration, allowing access for users with various technical backgrounds.
  • Plugin Variety: Jenkins offers an extensive ecosystem of over 1,500 plugins, each enhancing its functionality in various ways. These plugins support tasks ranging from integrating with code repositories and build tools to enabling advanced testing and deployment capabilities. With this vast selection, development teams can tailor Jenkins to align seamlessly with their specific workflows and requirements, ensuring an efficient development process.
  • Distributed Processing: Jenkins excels at distributing workloads across multiple machines and platforms, thereby accelerating build, testing, and deployment processes. This distributed architecture enhances team efficiency and enables faster software delivery. By leveraging available computing resources, Jenkins ensures that even resource-intensive tasks are managed efficiently and without bottlenecks.

What is Selenium?

Selenium is an open-source testing tool for testing web applications across different browsers and platforms. It consists of several components, including IDE, RC, WebDriver, and Selenium Grid, each serving a unique purpose.

As you progress through the Selenium suite, its applications expand, with each component offering its own advantages and disadvantages. However, the core of Selenium is browser testing. Developers and testers value its ability to emulate user interactions within the browser environment.

Developers create test scripts using Selenium, which can be executed locally or in the cloud. These scripts assess basic functional correctness and validate visual layouts. They range from simple JavaScript unit tests focusing on actions like button clicks to fully automated end-to-end functional tests that simulate complete user interactions such as the checkout or login process.

What is Jenkins Pipeline? 

A Jenkins pipeline is a set of plugins that facilitate the creation of automated, repeatable workflows, known as CI/CD pipelines. These pipelines integrate tools for orchestrating the testing, merging, packaging, shipping, and code deployment.

Pipelines are typically divided into stages and steps, where steps represent individual tasks and stages group related steps together. For instance, a pipeline might include stages like “Build,” “Test,” and “Deploy.” Additionally, existing jobs can be integrated into a pipeline to streamline and optimize the workflow.

Advantages of Jenkins Pipeline

Using Jenkins Pipeline offers several advantages that can significantly enhance your delivery process:

  • Code-Driven Methodology: Defining your pipeline as code using Groovy-based DSL promotes better collaboration, change tracking, and code reviews.
  • Longevity and Continuation: Jenkins Pipeline supports durable executions, ensuring your pipeline can resume from where it left off after a Jenkins master restart.
  • Simultaneous Execution: By defining stages to run in parallel, you can accelerate your delivery process. This is particularly effective for tasks like running tests or deploying to various environments simultaneously.
  • Extensibility: The Jenkins Pipeline is highly adaptable. It can easily connect with different tools and services by integrating various plugins or custom code.
  • Better Teamwork: Defining your pipeline as code promotes transparency and collaboration among team members, facilitating collaboration, review, and improvement suggestions.
  • Automation: Jenkins Pipeline enables automation of the entire delivery process, reducing the risk of human error and ensuring consistency.
  • Resilience to Unplanned Restarts: With durable execution support, your pipeline can resume from the last checkpoint even after unexpected Jenkins master restarts, ensuring an uninterrupted delivery process.

Prerequisites

Before beginning the integration of Jenkins with Selenium, ensure you have the following prerequisites prepared:

  • Jenkins Installation: Jenkins needs to be installed and running on your computer. You can get it from the official Jenkins site by following the installation instructions.
  • Selenium WebDriver: Ensure that Selenium WebDriver is installed. This tool is crucial for automating web browsers. You can install it via a package manager or download it from the official Selenium website.
  • Java Development Kit (JDK): Both Jenkins and Selenium are written in Java, so installing the JDK is necessary. Ensure the JDK is properly set up on your system to enable seamless integration. Download it from the official Oracle website or use an open-source version like OpenJDK.

Integrating Jenkins with Selenium (using Jenkins Dashboard)

Install Selenium Plugin:

  1. In the Jenkins Dashboard, go to Manage Jenkins > Manage Plugins > Available and search for “Selenium Plugin”.
  2. Install the plugin.

Configure Selenium Environment:

  1. After installing the plugin, go to Manage Jenkins > Configure System and scroll down to the “Selenium Remote Control” section.
  2. Here, you can configure the desired browser and provide the necessary driver for Selenium.

Example:

Browser Name: chrome
Maximum Instances: 3
Maximum Session Duration: 20 (minutes)
Configuration:
  – NAME: Chrome
    MAX_INSTANCES: 3
    MAX_SESSION_TIME: 20
    BROWSER_TIMEOUT: 0
    HOST: localhost
    PORT: 4444
    ENV_VARS: “CHROME_BINARY=/path/to/chrome/binary”

Create a Jenkins Job:

  1. Create a new freestyle project job.
  2. Specify the source code repository containing your Selenium test scripts in the job configuration.

Build Steps:

  1. In the “Build” section, add a step to compile your Selenium test scripts. For example, if your tests are written in Java, you can add an “Invoke Ant” step to compile the tests.
//xml
<target name=”compile-tests” depends=”resolve”>
  <mkdir dir=”${junit.output.dir}”/>
  <javac srcdir=”${src.dir}” destdir=”${junit.output.dir}” includeantruntime=”false” encoding=”UTF-8″>
    <classpath refid=”test.classpath”/>
  </javac>
</target>

Post-build Actions:

  1. Click on “Add post-build action” or “Add post-build step,” depending on your Jenkins version.
  2. Select “Selenium Remote Control” from the available options.
  3. Specify the test suite you want to run in the designated field. You can provide the path to the test suite or specify them by name.
  4. Choose the desired browser for testing from the dropdown menu or by entering the browser name manually.
  5. Save your configuration changes.

Example:

Test Suite: path/to/test/suite
Browser Selection: MOZILLA
User Extensions: none
Logging Level: NORMAL

With these steps, you can integrate Jenkins with Selenium WebDriver and automate your web application testing process. Jenkins will compile your Selenium test scripts, launch the specified browser(s), and execute the tests after each successful build.

Integrating Jenkins with Selenium using Maven

Install Maven Integration Plugin:

  • Go to Manage Jenkins > Manage Plugins > Available in the Jenkins Dashboard and search for “Maven Integration”.
  • Install the plugin.

Create a Maven Job:

  • Create a new Maven job in Jenkins and specify the location of your Maven project containing the Selenium tests.

Configure Maven Goals:

  • In the job configuration, specify the Maven goals to be executed during the build process. For example:
clean install

This will compile, test, and package your Selenium tests.

Selenium Dependencies:

  • In your project’s pom.xml file, include the necessary dependencies for Selenium and any related libraries (e.g., TestNG, JUnit).
//Xml
<dependencies>
  <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
  </dependency>
  <dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.1.0</version>
    <scope>test</scope>
  </dependency>
</dependencies>

Post-build Actions and Reporting:

  • In the job configuration, go to the “Post-build Actions” section and add a “Selenium Report” step to generate reports using the Selenium plugin.
  • You can configure various reporting options here, such as specifying the test result file patterns and generating HTML reports.

By following these steps, you can integrate Jenkins with Selenium using Maven. Jenkins will automatically compile and execute your Selenium tests as part of the Maven build process, and you can leverage the reporting capabilities of the Selenium plugin to generate detailed test reports.

Best Practices for Integration with Jenkins for Selenium Testing

Here are the best practices for integrating Jenkins with Selenium for efficient testing:

Test Organization:

  • Establish distinct Jenkins tasks or workflows for varied test collections or modules, fostering manageability and scalability.
  • Utilize Jenkins’ folder or multi-branch pipeline architecture to group associated tasks or workflows, facilitating collaboration and management.

Version Management for Tests:

  • Maintain version control for Selenium test scripts via Git (with platforms like GitHub or GitLab) to track alterations, enable cooperation, and ensure uniformity.
  • Committing test scripts to a version-controlled repository allows teams to comprehend test coverage, monitor modifications, and efficiently onboard new members.

Complementary Tool Integration:

  • Augment Jenkins and Selenium by incorporating additional tools to enhance your software testing capabilities.
  • Consider leveraging platforms like LambdaTest for cross browser testing and parallel test execution. LambdaTest is an AI-powered test orchestration and execution platform that lets you run manual and automated tests at scale with over 3000+ real devices, browsers, and OS combinations.

Parallel and Distributed Testing:

  • Harness Jenkins’ capabilities to run Selenium tests concurrently across multiple machines or containers, reducing overall test execution duration.
  • Implement distributed testing with tools like Selenium Grid or cloud-based services to execute tests simultaneously on different browsers and environments.

Test Reporting and Notifications:

  • Configure Jenkins to generate comprehensive test reports encompassing details such as executed test cases, pass/fail status, and error logs.
  • Establish notifications (e.g., email, Slack) to inform the team about build and test results, enabling swift identification and resolution of issues.

Test Environment Management:

  • Maintain separate test environments to simulate real-world scenarios and ensure reliable test results accurately.
  • Automate the setup and teardown processes of test environments using tools like Docker or Kubernetes to ensure consistent and reproducible test runs.

Conclusion

Paired with Selenium, Jenkins empowers developers to construct an automated application testing framework. Acting as the conductor, Jenkins oversees and executes the Selenium test suite.

While setting up Jenkins and Selenium integration involves initial configuration steps, the long-term benefits are substantial. Once seamlessly configured, Jenkins can automatically initiate the execution of Selenium tests whenever code changes occur. This proactive approach aids in catching and addressing bugs early, preventing potential larger issues.

Automated testing offers notable time and resource efficiencies compared to manual methods. With Jenkins and Selenium integration, developers are relieved from manually triggering tests after each code alteration. Instead, tests are automatically launched on a predetermined schedule or with new code commits.

Combining Jenkins’ automation capabilities with Selenium’s web testing prowess creates a robust continuous testing pipeline. This ensures that applications undergo consistent validation through automated checks, leading to prompt bug detection and resolution. Ultimately, this results in higher-quality software with dependable performance for end users. Integrating Jenkins and Selenium for automated testing fosters efficient development practices, enabling teams to optimize their testing processes and focus on delivering value.