Jenkins + Selenium: How to Setup and Run Tests
June 13, 2021

Jenkins + Selenium: How to Setup and Run Tests

Open Source Automation

Jenkins has become very popular in the last few years. In fact, it has taken the place as one of the best open-source tools that allow continuous integration, build management and the part we are going to explore in this blog post - the capability of running automated tests without human interaction.

But before we reach the goal where we can just sit back and watch how every triggered build is being tested thoroughly by our automated tests, a certain amount of work is needed in order to setup up the entire environment. For this, we are going to learn how to setup a basic framework using common, known tools, some of them maybe already used in the process of building the application under test.

In this blog, you will learn how to run Selenium tests in Jenkins. This blog will cover:

  • An overview of the tools chosen for setting up the framework: Maven, JUnit, Selenium, and Jenkins, and how they interact.
  • Adding all the dependencies and plugins needed for the tools to a Java project
  • Creating a Selenium test
  • Integrating the created test into a continuous ecosystem using Jenkins

Let’s get started.

Back to top

Jenkins Selenium Testing Tools Overview

Jenkins offers a great variety of plugins, integrations with third party apps, nodes distribution for parallel testing and post-build actions. So, keep in mind that the combination of tools presented today to set-up the entire infrastructure is a personal choice. I chose these tools due to their easy installation and management.

A good tip I recommend you to take into account is to check the tools that are already used within the project and try to stick to them also for the automated tests, if it’s possible. These are tools you or your team have already worked with so the process will be smoother and easier for you. 

The tools I’m using (in addition to Jenkins) are very popular and well-established for a Java ecosystem:

  • Maven: A Java software project management tool
  • JUnit: A Java unit testing library
  • Selenium WebDriver: A library that is used to automate browser interaction

Here is how the tools interact:

jenkins, selenium, maven and junit together

  • As you can see, when a Jenkins build is triggered, Maven downloads the latest code changes and updates, packages them and performs the build.
  • One of the build goals is to run the automated tests and that will take place through the maven-surefire-plugin.
  • The plugins tell JUnit to run all the tests in the project that have the annotation @Test.
  • Once a test is found, the Selenium Webdriver comes into play, opens a new browser instance and runs the automated user interface test.
  • At the end of the build, a default report is generated by Jenkins with the results of the tests.
  • If there is at least one failed test, the build is marked as failed.

Let’s learn how to set this all up. We’ll assume Jenkins, Maven and Selenium Server are already installed (preferably on the same machine, for increased speed, but not mandatory). If you need help in installing any of the tools, check the links below, which will help, independent of the OS installed on your machine.

Back to top

Adding Dependencies and Plugins

For time saving purposes, I will consider you have a Java project already in place. You will need to know the repository URL of your project for further use.  The IDE used is up to your decision. Make sure you add the appropriate dependencies and plugins into your Java (maven pom.xml) project, like in the example below, but remember to check if a newer version is available. 
 

<dependency><groupId>org.seleniumhq.seleniumgroupId>
    			<artifactId>selenium-javaartifactId>
    			<version>3.5.3version>
    		dependency>
    		<dependency>
    			<groupId>junitgroupId>
    			<artifactId>junitartifactId>
    			<version>4.11version>
    			<scope>testscope>
    		dependency>
    		
    		
    			<plugins>
    			<plugin>
    				<groupId>org.apache.maven.pluginsgroupId>
    				<artifactId>maven-source-pluginartifactId>
    				<version>3.0.1version>
    			plugin>
    			<plugin>
    				<groupId>org.apache.maven.pluginsgroupId>
    				<artifactId>maven-compiler-pluginartifactId>
    				<version>3.5.1version>
    				<configuration>
    					<source>1.8source>
    					<target>1.8target>
    					<encoding>${project.build.sourceEncoding}encoding>
    				configuration>
    			plugin>
    			<plugin>
    				<groupId>org.apache.maven.pluginsgroupId>
    				<artifactId>maven-jar-pluginartifactId>
    				<version>3.0.2version>
    				<executions>
    					<execution>
    						<goals>
    							<goal>test-jargoal>
    						goals>
    					execution>
    				executions>
    			plugin>
    

 

For those of you not familiar with JUnit, know that it’s a testing framework designed for the Java language. Although it was mainly used for unit tests until recently, it works very well for the interface tests with Selenium as well.

In JUnit, the class names containing the tests have to end in “Test”. In addition, all of the test cases you would like to translate into automated tests and run in a continuous environment need to be annotated with the specific JUnit annotation @Test. This allows Maven to identify the test class and run the tests inside without any additional configuration.

For this example we are going to create a UI test with the help of Selenium.

Back to top

Creating a Selenium Test for Jenkins

At this point, we have in place almost everything we need in order to run automated tests in a CI environment. Now we only need to write a test as an example and configure Jenkins to run it. So let’s talk about Selenium Webdriver and how it should be configured.

WebDriver is a tool for automating web application testing, and in particular, to verify that they work as expected. It aims to provide a friendly API that’s easy to explore and understand.

The screenshot below shows an overview of the structure of a Java project. The class named “App” hosts the code of the example-project and in the class “AppTest” we will add the needed tests.

overview of the structure of a Java project

The easiest way to add Selenium Webdriver to your project is by using Maven. In the main pom.xml file add the dependency of the selenium-java artifact, like it is displayed in the screenshots above.

Selenium allows engineers to automate browser interaction through code, start different browsers, call different URLs, click links and other web elements, type text and many other things, all done through the Selenium API, which is pretty intuitive and easy to use.

The code below illustrates examples of methods from the Selenium API used in order to interact with an HTML element or verify something about it.

@OverridepublicvoidsendKeys(CharSequence...value){parent.sendKeys(this,value);}@OverridepublicbooleanisEnabled(){assertElementNotStale();return!element.hasAttribute("disabled");}@OverridepublicbooleanisDisplayed(){assertElementNotStale();returnelement.isDisplayed();}

 

The first method sendKeys, simulates typing a string into a web element, while the other ones check if a web element is displayed or enabled (ready to interact with it). 

Back to top

Integrating Your Selenium Test Into Jenkins

Now that we have all the pieces in place, all we need to do is create and configure a new Jenkins plan that will run the automated tests.

For that, go to the server where Jenkins is installed, login with a valid account and click on “New Item”.

New in Jenkins

Enter an appropriate name for the new Job, select “Maven Project ” project and click on “Save”.

new jenkins job for selenium with maven

A new, empty job has been created at this point. Now, perform the following configuration steps:

Under “Source Management”, select the appropriate repository for the location of your project and pass the URL and credentials. In the below example, the repository used is Git.

Git for source code management

Usually, if a versioning system is used in a project, there will be a structure of a main branch called master and various other branches. In Jenkins, you need to specify which branch of the project should be used, or leave blank if there is no versioning system used or it has only the main one (master).

branches to build section

Next, in the “Build Triggers” section, you can choose how and when the build should be triggered and run. In the example below, the build is scheduled to run on a daily basis, starting at 20:00 PM. Other options include starting the build after each commit in the project, or after another module of the project was built.

build triggers section

In the “Pre-steps” build section, another set of parameters can be passed to the Jenkins build. Here you will specify the Maven targets that need to be executed in order to run your test, in this case "clean test". Another parameter that is being passed in this example is the type of browser on which the tests will be run. How you can achieve that inside the Java project is the topic for another article.

junit and selenium tests in jenkins

Last but not least, in the configuration of a new build, Jenkins allows the possibility of notifying specified users regarding the results of a build via e-mail or the feature of archiving different artifacts generated at the end of a build. In our case it could be test results saved in an excel file, other types of test reports, screenshots taken during the execution of the tests and so on.

jenkins open source selenium

Now you can run the plan and see how the test code is being checked out and all the defined tests are being ran. In this example, the server which hosts the AUT (application under test) is defined as a parameter within the Java project, but that doesn’t mean it couldn’t be stored as a variable and passed to the Jenkins plan like a parameter.

It all depends on what the engineer wants to accomplish through her/his automation framework. There is no one solution that fits all projects, but I do encourage you to take this example of configuration as a starting point and modify it as you go according to your needs.

Back to top

Running Your Selenium Test in Jenkins with BlazeMeter

After creating your Selenium script you can also easily upload and run it through BlazeMeter. This option makes your tests more manageable and shareable, and allows you to combine your functional testing with your load testing. Then, you can just run your BlazeMeter tests in Jenkins with BlazeMeter’s Jenkins plugin.

This blog was originally published on May 23, 2018, and has since been updated for accuracy and relevance.

START TESTING NOW

 

Related Resources


 

Back to top