All IT Courses 50% Off
Selenium Tutorials

Maven & Jenkins Integration with Selenium

Complete Tutorial

In this article, you can learn to implement Maven & Jenkins Integration with Selenium, Maven installation, Jenkins installation steps.

What is Jenkins?

Jenkins is an Open source tool to automate software development processes and it is often referred as automation server. Jenkins is mostly widely used and very matured CI tool among other CI tool such as Bamboo, Teamcity, buddy etc. Jenkins is written in Java and it makes runnable on any Operating System that is supporting java. Jenkins is a Continuous Integration tool that allows continuous development, continuous deployment of newly created codes. The main usage of Jenkins is to monitor any kind of job which can be SVN checkout, or any application states. When a particular step occurs in jobs it will fire pre-configured actions.

Important Features of Jenkins

  • Easy Installation: Jenkins is a platform-agnostic which is easy to install either by using direct installation file i.e exe file or through war file to deploy using application server.
  • Easy Configuration: We can easily configure various tasks on Jenkins
  • Change Support: Jenkins generates the list of all changes done in the repositories like SVN.
  • Permanent links: For easy communication Jenkins will provide direct links to the latest build or failed build that can be used.
  • Email Integration: Jenkins can be configured email content of the status of the build.
  • TestNG Test: After each SVN build Jenkins can be configured to run the automation test build on TestNG.
  • Multiple VMs: On multiple machines Jenkins can be configured to distribute the build.
  • Project build: Jenkins will documents the details of jar, jar version and mapping of build and jar numbers.
  • Plugins: To use features and additional functionality Jenkins will configure 3rd party plugin.

Why Jenkins and Selenium?

  • Running Selenium tests in Jenkins allows us to run our tests every time when your software changes and software is deployed to a new environment when the tests pass.
  • Execution history and Test Reports can be saved.
  • A specific time can be scheduled to run your tests.
  • Jenkins supports Maven for deployment and testing a project in continuous integration.

What is Maven?

Maven is a management of software and comprehension tool which is based Project Object Model (POM) which can manage project build, reporting and documentation from a central part of information for Maven such as construction directory, dependency, source directory, Goals, plugins, etc.

Why Maven & Jenkins

Selenium WebDriver is most popular tool for browser automation. It feels underpowered when it is used for testing and building a test framework. Integrating Maven with Selenium provides following benefits.

Features of Maven:

  • Maven defines project structure, dependencies, test management, and build.
  • We can configure maven dependencies which are needed for building testing and running code using POM.xml
  • Dependency management
  • Maven will automatically download the necessary files from the repository while building the project.

Maven Installtion and use it with TestNG Selenium

In this article, we will use Eclipse IDE to set up Selenium WebDriver project, and additionally we add m2eclipse plugin to eclipse to help the build process and to create POM.xml file.

All IT Courses 50% Off

Steps to add m2eclipse plugin to Eclipse:

 Step 1: Launch Eclipse IDE and click on Help > Select Install New Software

Step 2: Enter the URL http://download.eclipse.org/technology/m2e/releases/ on the Install dialog. Select Work with and m2e plugin as shown in the below screenshot.

maveninstall.PNG

Step 3: Click on Next button.

Step 4: Accept the License agreement and click Finish button.

Step 5: Click On Restart Now to restart the eclipse.

Configure Eclipse with Maven

We now need to create Maven project once m2e plugin is installed.

Step 1: Launch Eclipse IDE and create a New Project by selecting File > New > Other from Eclipse menu.

Step 2: On the New dialog box, Expand Maven and select Maven Project and click Next.

mavenselection.PNG

Step 3: On the New Maven Project, check the Create a simple project and click on Next.

newmavenproject.PNG

Step 4: Enter TestWebdriver in Group Id, Artifact Id and click on Finish.

groupid.PNG

Step 5: Eclipse will create TestWebdriver with following structure:

mavenprojectstructure.PNG

Step 6: Right-click on JRE System Library and select the option Properties from the menu.

Jreproperties.PNG

On JRE System Library Properties dialog box, make sure Workspace default JRE is selected and click Apply and Close.

workspace.PNG

Step 7: Double-click on pom.xml from Project Explorer.

mavenprojectstructure.PNG

pom.xml file will Open in Editor section

pom.PNG

Step 8: Add the Selenium, Maven, TestNG, Junit dependencies to pom.xml in the <project> node:

junittestngverion.PNG

Step 9: Create a New TestNG Class. Enter Package name as “testing” and “NewTest” in the Name: textbox and click on the Finish button

testng.PNG

Step 10: Eclipse will create the NewTest class as shown in the below screenshot:

testngproject.PNG

Step 11: Add the below code to the NewTest class

package testing;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;

public class NewTest {
    private WebDriver driver;
@Test
public void testEasy() {
driver.get("https://www.facebook.com/");  
String title = driver.getTitle();  
//Assert.assertTrue(title.contains("Facebook"));
Assert.assertTrue(driver.getTitle().contains("Facebook"));

}
@BeforeTest
public void beforeTest() {
    driver = new FirefoxDriver();  
}
@AfterTest
public void afterTest() {
driver.quit();
}

}

Step 12: Right-click on the TestWebdriver and select TestNG and Convert to TestNG. Eclipse will create testng.xml which says that you need to run only one test with the name NewTest as shown in the below screenshot:

converttestng.PNG
generatetestng.PNG

Update the project and make sure that file appears in the tree Package Explorer (right click on the project and click Refresh).

addedtestng.PNG

Step 13: Now run the above test through this testng.xml.

Go to the Run Configurations under Run and create a new launch TestNG, select the project and field Suite as testng.xml and click Run

run.PNG
suite.PNG

This will launch the website and finished the build successfully.

Step 14: Additionally, to pom.xml we need to add

  1. maven-compiler-plugin
  2. maven-surefire-plugin
  3. testng.xml

The maven-surefire-plugin is used to configure and execute the tests. To configure the testing.xml for TestNG test and generate to test reports this plugin is used.

The maven-compiler-plugin is used in compiling the code and using the particular JDK version for compilation. Add all the dependencies in the below code, to pom.xml in the <plugin> node:

<plugins>
  <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2.</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
  <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<inherited>true</inherited>
<configuration>
<suiteXmlFiles>
  <suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>

Step 15: To run the tests in the Maven lifecycle, Right-click on the TestWebdriver and select Run As Maven test. From the project Maven will execute the test.

maven1.PNG

This will launch the website and finished the build successfully.

Installation steps for Jenkins and configure it to Run Maven with TestNg 

Step 1: Go to the URL https://www.jenkins.io/ and download the package for your OS and click Download button.

Jenkins Download.PNG

Step 2: Unzip the Jenkins folder and run the exe file as shown in the below image:

zjenkinsexe.PNG

Step 3: In Jenkins 2.233 Setup window click on Next button.

jenkins233.PNG

Step 4: Click on Install button.

Jeninstall.PNG

Step 5: Once installation is complete, it automatically navigate to the Jenkins Dashboard (http://localhost:8080) in the browser window.

autojenkins.PNG

Step 6: Click on the New Item to create a job.

Jennewitem.PNG

Step 7: Enter an Item Name and click OK button

Itemname.PNG

Step 8: A new job with name “TestWebdriver” is created in Jenkins Dashboard.

newjenkinsfile.PNG

Step 9: Go to Manage Jenkins and select Global Tool Configuration as shown in the below image.

configurejenins.PNG

Click on JDK installations and configure JDK as in the following image:

Enter JDK Name: java 1.8.0

JAVA_HOME C:\Program Files\Java\jdk1.8.0_121

jdk.PNG

Step 10: Go to the Build section of created job.

Add the pom.xml file’s path in the Root POM option.

rootpom.PNG

Step 11: Click on the Apply button.

build.PNG

Step 12: On the TestWebdriver project page, click on the Build Now link.

buildnow.PNG

Maven will build the project. It will have TestNG execute the test cases.

Step 13: Once the build process is completed, in Jenkins Dashboard click on the TestWebdriver project.

Testweb.PNG

Step 14: The TestWebdriver project page displays the build history and links to the results as shown in the below image:

buildhistory.PNG

Step 15: To view the test results click on the “Latest Test Result” link.

Benefits of using Jenkins

  1. Finding Early issues – we can detect the Bugs detected in early phase of the software development
  2. Automatic integration – no need of separate effort required to integrate all changes made
  3. Installer – We can deploy system available at any point of development
  4. Records –build records are maintained
  5. Support and Plugins: One of the important reasons for Jenkin’s popularity is the accessibility of large community support. Also, more ready-made plugins are available which help us to expand its functionality.
Facebook Comments

Related Articles

Back to top button