Jenkins has become the go-to solution for integrating Selenium with Maven as Jenkins enables quicker deployment and more efficient monitoring. The adoption of the CI/CD pipeline has become more prevalent in the software arena. In addition to being a resourceful DevOps tool, Jenkins is also very effective when it comes to building the CI/CD pipeline. As one of the best QA companies, we have used Jenkins as it helps to check the latest build that can be deployed to the production environment. It also makes the process of Selenium test automation much easier with the help of Maven. So in this Jenkins integration with Selenium guide, we will be going through the step-by-step process on how to do it. Before we proceed further, let’s take a look at a few basics.
An Introduction to Selenium and Jenkins:
Selenium is an open-source automation tool that is widely used for testing web applications across various browsers like Chrome, Mozilla, Firefox, & Safari. Selenium will be able to automate these browsers using Selenium WebDriver. Test scripts can be written in any programming language like Ruby, Java, NodeJS, PHP, Perl, Python, etc.
As stated earlier, we all know Jenkins is an open-source automation tool that allows continuous integration (CI) and continuous delivery (CD) and that it is a java based application. Jenkins is mainly used to build and test any software project, making it easy for developers to continuously work and integrate changes to the project.
Installation of Selenium and Jenkins
Before heading over to the Jenkins integration with Selenium, let’s take a look at how to install and set up both Jenkins and Selenium.
Steps to Download Jenkins
1. Download Jenkins from their official website.
2. Unzip the file and open the Jenkins exe file.
3. Click ‘Next’ to start the installation.
4. Click the Finish button to complete the installation process.
5. You will be redirected automatically to a local Jenkins page, and the port number would be 8080 by default. We can also launch a browser and navigate to the mentioned URL http://localhost:8080
Creating an admin account to access Jenkins
1. You can obtain and enter the secret password by navigating to the mentioned path as shown in the below image. Once you have entered the password, click on Continue.
2. After entering the secret password, you will be asked to install plugins. Click on the required plugins option so that the recommended plugins could be downloaded and installed.
3. After the successful installation, you will be asked to create an administrator account with the following details that are mentioned in the screenshot.
4. Once you have successfully installed Jenkins, the default Jenkins dashboard can be seen as shown below.
Jenkins integration with Selenium
The Jenkins integration with Selenium can be achieved in various ways and we will be focusing on how it can be established using Maven in this guide. Let us quickly introduce what is Maven, why we have chosen Maven, and go through the installation process as well.
What Is Maven?
Maven is a software project management & build management tool which allows us to add and manage all the dependencies in a single pom.xml file.
Why Maven for Jenkins integration with Selenium?
1. It can act as the central repository to get dependencies
2. Maven helps to maintain a common structure across the organization
3. It makes integration with CI tools easier.
How to Install Maven?
1. Download Maven from their official site.
2. Then add MAVEN_HOME under system variables as shown below
3. Now, set up the path for the bin directory of the maven directory.
4. To verify if maven has been installed successfully, type the following command in the command prompt.
mvn --v
5. Post the verification, you can go ahead and create a maven project and add the maven dependencies in the pom.xml file that will be used to achieve Jenkins Integration with Selenium.
POM.xml file
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>MyfirstDemo</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.4.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <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.22.1</version> <configuration> <suiteXmlFiles> <suiteXmlFile>E:\MyfirstDemo\testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> </plugins> </build> </project>
Using the Java class ‘Sample.java’, we create a WebDriver script. Jenkins Integration with Selenium
package Demo; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class Sample { @Test public void demo() { System.setProperty("webdriver.chrome.driver","src/Drivers/chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://www.cricbuzz.com/"); String baseTitle = driver.getTitle(); System.out.println("title =" + baseTitle); driver.manage().window().maximize(); } }
TestNG.xml file
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="TestSuite"> <test name="demo"> <classes> <class name="Demo.Sample"> </class> </classes> </test> </suite>
How To Integrate Selenium Tests In Maven With Jenkins?
So the next aspect that we are going to cover in this Jenkins Integration with Selenium guide is to see how to integrate Jenkins with Selenium WebDriver and how Maven is instrumental in the integration of Jenkins with Selenium test scripts. For a crystal clear understanding, we have also added screenshots of all the important steps.
Start the Jenkins server and launch the browser and navigate to the localhost using the mentioned URL http://localhost:8080.
Steps:
1. The first and foremost step would be to create a new project by choosing the ‘New Item’ option from the Jenkins dashboard.
2. Obviously, the next step would be to give the new project that we have created a name. In this case, we have used “DemoProject” as the name of the project for explanation purposes. It goes without saying that you can name the project as per your wish. Once the name has been entered, we have to select ‘Maven project’ as the option from the given list and then click on OK.
3. The created project file can be seen in the Jenkins dashboard, and so we would have to select the project from the list to proceed further.
4. From the many options that appear on the left, click on ‘Configure’.
5. Automatically Jenkins takes us to the project configuration view where we can configure the project-related details under the General tab. As shown in the image, this section includes the name and description of the project.
6. The next section is the Source Code Management, under which we have to select the ‘None’ option.
The Git Option:
Here, we select ‘None’ in the Source Code Management section as we just need a build.
If you select Git, you would need to commit to the repository and enter the credentials by clicking on ‘Add’. In this Jenkins Integration with Selenium guide, we will be only focusing on the aspects and features needed to make the integration possible.
7. So we can head straight to the Build section next, and it would require two important steps to load the POM.xml file. Under the Root POM, you have to enter the complete path of the pom.xml that you have created. Under the Goals and options, you would have to enter the following command
clean test
8. Once these crucial pieces of information have been filled in, we can scroll down and click on ‘Apply’ and then finally ‘Save’.
9. Once the above step is completed, we head back to the project where we have to click the ‘Build Now’ option.
10. Now that we have manually triggered the build in Jenkins, the job will run after the completion of the build.
11. The results can be viewed in the console output as shown below.
Conclusion:
We hope this step-by-step Jenkins integration with Selenium has been helpful to you. Using this integration, we have been able to build and test our software projects efficiently and achieve the goal of continuous integration. Using Jenkins, we have been able to provide the best Selenium testing services to our clients. Beyond the benefits that we have already seen, Jenkins allows us to schedule jobs to run at any time, and it also supports a wide range of selenium plugins that will come in handy to achieve a variety of project needs.
Comments(0)