DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Recipe To Implement the Jenkins Pipeline For MuleSoft Application [Videos]
  • Travis CI vs Jenkins: Which CI/CD Tool Is Right For You?
  • Integrating Jenkins With Playwright TypeScript: A Complete Guide
  • Jenkins in the Age of Kubernetes: Strengths, Weaknesses, and Its Future in CI/CD

Trending

  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  • Designing a Java Connector for Software Integrations
  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  • Breaking Bottlenecks: Applying the Theory of Constraints to Software Development
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Running Selenium Tests in Jenkins

Running Selenium Tests in Jenkins

Wondering how to integrate Jenkins with Selenium WebDriver? This guide will help you run Selenium tests in Jenkins. Learn more.

By 
Shalini Baskaran user avatar
Shalini Baskaran
·
Apr. 16, 21 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
26.0K Views

Join the DZone community and get the full member experience.

Join For Free

DevOps, an organizational approach that has been used immensely for quicker development and deployment of applications, has been the latest technical buzz. Today organizations are adopting DevOps, as it promises to provide best practices starting right from planning and testing to deployment and monitoring. CI/CD tools like Jenkins play an integral role in allowing organizations to implement DevOps, and Jenkins integration with Selenium makes the entire process of Selenium test automation so much easier.

In this article, I will take you through a quick introduction to Jenkins & Selenium, followed by the method to integrate Jenkins with Selenium WebDriver. If you are already familiar with the concepts, you can jump to the section explaining Jenkins integration with Selenium directly.

What Is Jenkins?

Jenkins is an open-source DevOps tool that has been popularly used for continuous integration and continuous delivery processes. It is a Java-based application and platform-independent. It is a build tool; used for running builds from the source code repository, running unit tests, and sending the build reports to the respective member or team.

You can refer to our detailed guide on ‘What is Jenkins?’ for more information.

What Is Selenium?

Selenium is an open-source automation tool that has been widely used for testing web applications. It is easy to use, and it provides support forums, which makes it popular among the testing community. Selenium has four main components: Selenium IDE, Selenium RC, Selenium WebDriver, and Selenium Grid, designed and used for different purposes. Selenium provides cross-browser testing and parallel testing features, which allows the testers to execute their test cases in different operating systems and browsers, which ensures browser compatibility of the web application.

You can refer to our detailed guide on ‘What is Selenium?’ for more information.

How to Integrate Jenkins With Selenium WebDriver?

Now that I have introduced Jenkins & Selenium briefly, it is time to dive right into the integration. But, do you have it all set up in your system? Before we look into Jenkins integration with Selenium, let us quickly dive into Jenkins and Selenium’s installation and setup.

Steps to Download Jenkins

Step 1: To download Jenkins, you can click the download link on the Jenkins official website. The installation process is explained in detail here.

Step 2: Download this Jenkins.war file and place it in the desired path.

Step 3: Open the command prompt and navigate to the folder containing the war file.

Step 4: Enter the command java -jar Jenkins.war. This would start the Jenkins server.

Step 5: Jenkins would usually start in port 8080 by default. In case if the port is being used by any other service, an alternate port can be defined by using the below command

java -jar jenkins.war --httpPort=8081

Any available ports can be defined in the above command and can be used to start the Jenkins server. 

 

While starting Jenkins for the first time, a secret password will be generated, which will be printed at the end of the console.

The white shaded part contains the secret password for setting up Jenkins in your machine. When the installation is done successfully, you might see the “Jenkins is fully up and running” message in the console.

Step 6: Launch your browser and navigate to the localhost. By default, the port number would be 8080. In my case, I have defined the port as 8081 and hence use that port while navigating to the localhost.

Now the Jenkins setup page will be launched, which is used for creating the admin account for Jenkins.

Step 7: Enter the secret password that was generated and displayed in the console earlier in the setup page and click Continue.

Step 8: After entering the secret password, you will be asked to install plugins. 

If you are not sure of the plugins you want, you can select “Install suggested plugins,” which would install a list of commonly used plugins. But if you know the plugins based on the requirement, you can select the plugins to install option and click the required plugins.

Now the plugins would be downloaded and installed.

Step 9: Once the plugins are installed successfully, you will be asked to create an administrator account by providing a username, password, and email address. Once the admin profile is created, you will be navigated to the Jenkins dashboard.

Now the Jenkins is installed and set up in the localhost, and you will be able to see the Jenkins dashboard.

There are multiple ways to integrate Jenkins with Selenium WebDriver. I will take you through different ways to do so, which would help implement based on your project requirements.

Jenkins Integration With Selenium – Method 1

Step 1: In the Jenkins dashboard, click New Item to create a new project. Specify the name of the project and click Freestyle Project option. Click Ok.

Step 2: In the General section, enter the project description in the Description box. 

Step 3: In the Source Code Management section, select None. 

Step 4: To schedule the jobs, we can select the required option from the list given. Let us now specify the duration to schedule the jobs. To mention the duration, there is a set of rules that has to be followed as below.

THE PERIOD HAS TO BE MENTIONED IN THE FORMAT MINUTE HOUR DOM MONTH DOW
MINUTE Minutes within the hour (0–59)
HOUR The hour of the day (0–23)
DOM The day of the month (1–31)
MONTH The month (1–12)
DOW The day of the week (0–7) where 0 and 7 are Sunday

Step 5: Create a project for running the tests using Selenium WebDriver and TestNG.

Java
 




x
52


 
1
package Pages;
2
import static org.testng.Assert.assertEquals;
3
import java.util.concurrent.TimeUnit;
4
import org.openqa.selenium.By;
5
import org.openqa.selenium.WebDriver;
6
import org.openqa.selenium.chrome.ChromeDriver;
7
import org.testng.annotations.AfterTest;
8
import org.testng.annotations.BeforeTest;
9
import org.testng.annotations.Test;
10
public class LoginPage {
11
    
12
    WebDriver driver;
13
    
14
    @BeforeTest
15
    public void setUp() {
16
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Shalini\\Downloads\\chrom86_driver\\chromedriver.exe");
17
        driver = new ChromeDriver();
18
    }
19
    
20
    public void login() {
21
        String login_url = "https://opensource-demo.orangehrmlive.com/";
22
        driver.get(login_url);
23
        
24
        driver.manage().window().maximize();
25
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
26
        driver.findElement(By.id("txtUsername")).sendKeys("Admin");
27
        driver.findElement(By.id("txtPassword")).sendKeys("admin123");
28
        System.out.println(driver.getTitle());
29
    }
30
    
31
    @Test
32
    public void dashboard() {
33
        
34
        driver.findElement(By.id("menu_dashboard_index")).click();
35
        String textPresent = driver.findElement(By.xpath("//*[@id=\"content\"]/div/div[1]/h1")).getText();
36
        
37
        String textToBePresent = "DashBoard";
38
        assertEquals(textPresent, textToBePresent);
39
    }
40
    
41
    
42
    @AfterTest
43
    public void tearDown() {
44
            driver.quit();
45
    }
46

          
47
}


Step 6: Create a TestNG.xml file 

XML
 




xxxxxxxxxx
1
15


 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3
<suite name="TestSuite">
4
<test name="LoginTest">
5
 <groups>
6
      <run>
7
        <include name="DemoTest"/>
8
      </run>
9
    </groups>
10
<classes>
11
<class name="Pages.LoginPage">
12
</class>
13
</classes>
14
</test>
15
</suite>


Step 7: In the project folder, add all the dependencies in a separate folder. Create a bat file with the below content. 

ava –cp bin;lib/* org.testng.TestNG TestNG.xml

Specify the exact name of the TestNG.xml file that you have created in your project.

Step 8: In Jenkins dashboard, select the project that we have created initially. Click Configure. In the General section, click Advanced and then check the “Use custom workplace” option. Specify the test project path.

 Step 9: In the Build section, click the Add Build Step dropdown box and select the “Execute Windows batch Command” option. Specify the name of the bat file that has been created in Step 7. 

Step 10: Click Apply and then Save to apply all the changes to your Jenkins project. Now Click Build and you will be able to see the tests running on the Jenkins project. 

Jenkins Integration With Selenium – Method 2

We can also perform Selenium test automation in Jenkins by integrating with Maven. Before we look into the integration, let us have a quick introduction to Maven and why it is popularly used with Jenkins to run the tests.

What Is Maven?

Maven is a build automation tool that allows us to add and manage all the dependencies in a single pom.xml file. This allows users to reuse the same dependencies across multiple projects. By adding the dependencies in a pom.xml file, the dependencies get automatically downloaded and added to the project, thereby eliminating the manual effort of adding each and every dependency as a JAR file to the project.

Thus, by integrating Maven, Jenkins, and Selenium WebDriver, the DevOps model could be adopted, thereby achieving Continuous Integration in the project.

How To Install Maven?

Step 1: Download Maven from their official website.

Step 2: Add MAVEN_HOME system variable.

Step 3: Set the path which points to the bin directory of the maven directory. 

Step 4: To verify the successful installation of Maven, type mvn –version in the command prompt. 

Step 5: Create a maven project and add all the maven dependencies in the pom.xml file. 

XML
 




xxxxxxxxxx
1
51


 
1
<project
2
xmlns="http://maven.apache.org/POM/4.0.0" 
3

          
4
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">
5

          
6
  <modelVersion>4.0.0</modelVersion>
7

          
8
  <groupId>demoProject</groupId>
9

          
10
  <artifactId>demoProject</artifactId>
11
  <version>0.0.1-SNAPSHOT</version>
12
  <build>
13
    <sourceDirectory>src</sourceDirectory>
14
    <plugins>
15
    <plugin>
16
        <artifactId>maven-compiler-plugin</artifactId>
17
        <version>3.8.0</version>
18

          
19
        <configuration>
20

          
21
        <source>1.8</source>
22

          
23
        <target>1.8</target>
24

          
25
        </configuration>
26

          
27
    </plugin>
28

          
29
    </plugins>
30

          
31
  </build>
32
  <dependencies>
33

          
34
                <dependency>
35

          
36
              <groupId>org.seleniumhq.selenium</groupId>
37

          
38
              <artifactId>selenium-java</artifactId>
39

          
40
          <version>3.141.59</version>
41

          
42
                </dependency>
43
<dependency>
44

          
45
    <groupId>org.testng</groupId>
46
    <artifactId>testng</artifactId>
47
    <version>7.3.0</version>
48
    <scope>test</scope>
49
</dependency>
50
  </dependencies>
51
</project>


Java
 




xxxxxxxxxx
1
39


 
1
package WebDriverProject;
2

          
3
import org.junit.Assert;
4
import org.openqa.selenium.By;
5
import org.openqa.selenium.WebDriver;
6
import org.openqa.selenium.firefox.FirefoxDriver;
7

          
8
public class LoginClass {
9
    
10
    WebDriver driver;
11
        
12
    @BeforeTest
13
             public void setup(){
14
    System.setProperty("WebDriver.gecko.driver","C:\\Users\\shalini\\Downloads\\geckodriver-v0.26.0-win64\\geckodriver.exe");
15
        
16
        driver = new FirefoxDriver();                    //initialise the WebDriver
17
    }
18

          
19
            @Test
20
                         public void loginTest(){   
21
        driver.get("https://opensource-demo.orangehrmlive.com/"); //define the url
22
    String pageTitle = driver.getTitle();       //get the title of the webpage
23
    System.out.println("The title of this page is ===> " +pageTitle);
24
    Assert.assertEquals("OrangeHRM", pageTitle);    //verify the title of the webpage
25
        
26
        driver.findElement(By.id("txtUsername")).clear();//clear the input field before entering any value
27
        driver.findElement(By.id("txtUsername")).sendKeys("Admin");//enter the value of username
28
        driver.findElement(By.id("txtPassword")).clear();
29
        driver.findElement(By.id("txtPassword")).sendKeys("admin123");//enter the value of password
30
        driver.findElement(By.id("btnLogin")).click();      //click Login button
31
        System.out.println(“Successfully logged in”);
32
}
33

          
34
@AfterTest
35
             public void teardown(){
36
        driver.quit();
37
}
38
        
39
}


XML
 




xxxxxxxxxx
1
15


 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3
<suite name="TestSuite">
4
<test name="LoginTest">
5
 <groups>
6
      <run>
7
        <include name="DemoTest"/>
8
      </run>
9
    </groups>
10
<classes>
11
<class name=" WebDriverProject.LoginClass">
12
</class>
13
</classes>
14
</test>
15
</suite>


How To Integrate Selenium Tests In Maven With Jenkins?

We saw how to integrate Jenkins with Selenium WebDriver in the sections above. The best use case of this integration is to implement it for Selenium test automation. Jenkins integration with Seenium WebDriver makes your cross-browser testing scripts sturdier than ever. In this section, I will explain how to integrate Jenkins with Selenium test scripts with the help of Maven.

Step 1: Start the Jenkins server.

Step 2: Open the browser and navigate to the localhost and the port in which Jenkins is running.

Step 3: Click New Item in the dashboard. 

Step 4: Enter the project name and select the project type as Maven project. 

Step 5: Click Ok. Now you could see a job being created successfully in the dashboard. 

Step 6: Click the project and click Configure. 

Step 7: Under the Build section, enter the complete path of your pom.xml . In the Goals and options, enter the command clean test. 

Step 8: Click Apply and then Save.

Step 9: Click Build Now.

Step 10: Now the build will run and, after successful completion of the build, the results would be displayed. To view the complete logs, click the console output. 

Wrapping Up!

To summarize, we have covered why Jenkins is so important and how it can be integrated with Selenium WebDriver to run our tests efficiently and achieve the goal of continuous integration. Using Jenkins to run our tests is time-effective, and the results can be visualized with complete logs. It helps fulfill the complete software development life cycle starting from development, deployment, testing, monitoring, and releasing. It supports various plugins for achieving the project requirements. It also supports alerting the user via emails regarding the stability of the build.

I hope now you can easily integrate Jenkins with Selenium WebDriver. Let us know your feedback on this article in the comment section below.

Happy Testing!

Jenkins (software) Testing Continuous Integration/Deployment Integration Apache Maven Open source

Published at DZone with permission of Shalini Baskaran. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Recipe To Implement the Jenkins Pipeline For MuleSoft Application [Videos]
  • Travis CI vs Jenkins: Which CI/CD Tool Is Right For You?
  • Integrating Jenkins With Playwright TypeScript: A Complete Guide
  • Jenkins in the Age of Kubernetes: Strengths, Weaknesses, and Its Future in CI/CD

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!