Integrating Maven & Jenkins with Selenium

maven-jenkins-selenium-integration

Selenium is used for web automation testing but when we talk about delivering a client to a product, we need continuous integration with the production environment so that live deployments can be checked and then if it satisfies the pass percentage, then only they are pushed to the production environment. A continuous integration pipeline has to be set using Jenkins which checks the latest build which can then be deployed to the production environment. Maven is used as a build management tool which can ease the process of build and make the jars available in the easiest way.

Maven and Jenkins: At a Glance

Jenkins is an open source continuous integration tool. It can be used on Windows, Linux, MAC and Solaris environment. This means that it supports different systems. Jenkins will monitor a job when we are building a continuous integration pipeline and this job can be SVN checkout, cron or any applicable state. It then performs an action when a particular job is triggered.

Maven is a build management tool which is used to make the build process easy. You can easily define all the dependencies with the help of Maven and it will provide you all the jars in one go. You don’t need to worry about getting it from the internet and then importing it. With the help of pom.xml, you can define all the dependencies using artefact and version id. Maven will download the specified jar files from maven repository and place them in. /m2 repository. Also, when you want to share the project with other team members, you can’t manually send the jars. People scattered over the team can use a different version of jars which can bring inconsistency in the project. It is better to have a common structure of the project which can be shared throughout the team.

Why use Maven and Jenkins?

  • Whenever a new deployment is made. Jenkins will keep a check on that and it will run the smoke test against the latest build. If the build gets passed then that build will be deployed to the production environment.

  • It is not easy to run all the test cases by the time you reach office. If you know that your test or regression suite will take almost 10 hours to execute then you can have nightly build run on Jenkins which will run the test cases during night time so that by the time you reach the office, you can get the latest test result without wasting any more time.

  • Jenkins will serve as a common server on which results will be published. Client and all stakeholders can log in to the server and see the test results.

  • Using Maven in your project removes the hard coding of jars.

  • Build process will become very easy with Maven build management tool.

  • Updating hard coding jars is a hectic process. It’s better to update the version number in pom.xml so that jars can be updated on the go. You don’t need to worry about deleting the old jar and getting the latest jar.

  • When the team is very big then you need a common structure to share the project structure. It’s easy to share the artefact id and version id so that people in the team can clone the project.

Steps to install Maven with TestNg in Selenium

Prerequisite is Eclipse. You have to download m2eclispe plugins which you can find in the Eclipse marketplace. You can go to the Eclipse marketplace and then search Maven in it. The first plug-in which will come in the search results is the one which you have to download.

Steps of creating a Maven project

  • Follow File -> New Project-> Other to create a project in Eclipse and then you have to select Maven -> Maven project. It will help in creating a Maven project.

  • Press Next button and you have to then specify the Artifact Id and Group Id. This will be unique for your project as when pushed to the git hub repository, anybody can clone the project using these. Group Id is your Project Name and Artifact Id is your Project folder name.

  • You have to then select a template for the project. You have to then select “Maven quikstart template”.

  • You will then get a Project with a structure of two folders. There will be two folders – src/main/java and src/main/test. You can main your tests in src/main/test folder and other things in src/main/java.

  • You can then see pom.xml in your project structure. POM is the heart of the maven. Now, open the pom.xml and add the below code in it so that it can download all the dependencies and place them in project structure.

    <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.45.0</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8</version> <scope>test</scope> </dependency> </dependencies>
  • For every jar file, go to the maven repository official site and search for that particular jar. You can then copy the dependency tag and then add it in the pom.xml file.

  • You can integrate the testng framework with this by making a NewTest.xml file in the project structure.

  • For running the test through maven, you need one plugins which is known as maven sure-fire plugins. For running the test through testng you need testng.xml plug-in. Maven compiler plugins is also required for compiling the code. This plug-ins will ensure that tests are running without any hiccups

    <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> <configuration> <suiteXmlFiles> <suiteXmlFile>TestNG.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> </plugins> </build>
  • Now, you can go to the project path where pom.xml is placed. Open cmd there. You can then fire below commands to clean, compile and test the code.

  • mvn clean

  • mvn compile

  • mvn test // for all tests to be executed.

Steps to install Jenkins Server

Now, you have built the project using Maven and you need a continuous integration pipeline setup using Jenkins. You can then follow the below steps to install Jenkins server on which tests will be running.

  • Click here and download Jenkins for your machine. You can then unzip Jenkins folder and then you will get its .exe file which you can run to install the Jenkins server.

  • 2. After the first step, you are done with installation of Jenkins. You have to then follow the below command to run the server on the local host. The command is:

    java -jar jenkins.war

  • The Jenkins server will be up and running on port number 8080. Though, you can change the port number if you want it to run on any other port.

  • You can go to https://localhost:8080. You will see Jenkisn server running on this port and then you can do the installation of necessary plugins.

  • Click on New Item and then select the Maven project

  • Click on the OK button and a new job will be created with the name specified by you in Jenkins Server

  • Go to Manage Jenkins and select Configure Systems. You have to then configure Maven and JDK with the version number and go to Build Section and enter the full path of your pom.xml.

  • Click on Apply button. Click on the “Build Now” button.

  • In the left section, you can see the test execution history which will show you how many tests got passed and how many got failed.

  • You can schedule the regression test cases using Jenkins for nightly builds. You have to go to modify the configuration and select “Build Periodically” and then, enter the time in the manner like “0 22 * * *”. It will trigger every day at 10 pm. You can specify any time according to your convenience.

Conclusion

After maintaining the structure in local, you can add maven and Jenkins to your project to make the build process easy and to add continuous integration with the production environment. We ensure that if you are a Software Testing Company, then it will help you! All the best!!

Some of our clients

team