Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Set Up and Run Selenium Tests in Jenkins Using Maven and JUnit

DZone's Guide to

How to Set Up and Run Selenium Tests in Jenkins Using Maven and JUnit

Learn how these tools interact and allow you to set up and run Selenium tests on your applications in this tutorial.

· Performance Zone ·
Free Resource

Maintain Application Performance with real-time monitoring and instrumentation for any application. Learn More!

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 set up a basic framework using common, known tools; some of them may already be used in the process of building the application under test.

A quick summary of this blog post is listed below:

  • 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.

Jenkins offers a great variety of plugins, integrations with 3rd 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 possible. These are tools you or your team have already worked with so the process will be smoother and easier for you. For example, click here to learn how to use Selenium WebDriver with Jenkins and Allure.

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:

  • 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.

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.

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.

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.

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.

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). You can find more examples and description of the methods provided by the Selenium API at this link.

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, log in with a valid account and click on "New Item."

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

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.

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).

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.

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.

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.

Now you can run the plan and see how the test code is being checked out and all the defined tests are being run. 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.

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.

Collect, analyze, and visualize performance data from mobile to mainframe with AutoPilot APM. Learn More!

Topics:
continuous integration ,functional testing ,junit ,selenium ,maven ,jenkins ,continuous testing ,performance ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}