This in-depth guide will teach you how to install and configure Jenkins with Selenium:
Our previous DevOps tutorial focused on Ansible Roles and their integration with Jenkins.
Recommended IPTV Service Providers
- IPTVGREAT – Rating 4.8/5 ( 600+ Reviews )
- IPTVRESALE – Rating 5/5 ( 200+ Reviews )
- IPTVGANG – Rating 4.7/5 ( 1200+ Reviews )
- IPTVUNLOCK – Rating 5/5 ( 65 Reviews )
- IPTVFOLLOW -Rating 5/5 ( 48 Reviews )
- IPTVTOPS – Rating 5/5 ( 43 Reviews )
Jenkins is an open-source tool written in Java. It offers continuous delivery and continuous integration services for software development. Jenkins automates the manual task of deploying code from the development environment to QA, Stage, and Production environments.
To explore extensively, refer to our detailed DevOps Training Tutorial Series.
Jenkins supports multiple plugins, such as Git, SVN, build pipeline, and many more.
The primary function of Jenkins is executing a pre-set sequence of steps based on time or event triggers.
For instance: A job can be scheduled to run every 20 minutes or activated following a new commit to a Git repository.
This guide will address the following topics:
- Selenium’s usage and integration with Jenkins
- Generation of a batch file and its deployment in Jenkins
- Schedule a Jenkins job and add email notifications
- Command-line execution of a Selenium script
Jenkins provides several benefits, including:
- It operates across various platforms such as Windows, Linux, Mac OS, and Solaris
- It’s freely available and open source
- It boasts broad adoption and comprehensive documentation
- It integrates with a wide array of tools and technologies
Aside from Jenkins, there are other comparable tools available, including:
- Anthill
- Bamboo
- Cruise Control
- Team City, along with several others.
Your Learning Journey:
Implementation and Integration of Jenkins with Selenium
Follow this detailed guide to working with Jenkins and Selenium:
Step 1::
- Download the latest Jenkins .war file from the official website – Jenkins. You can start Jenkins via a web application server or the command line.
To launch Jenkins from the command line, follow these steps:
1) Open the command prompt and enter java –jar, followed by the path to the .war file.
2) Hit enter and ensure that the Jenkins.war file begins running. Observe the status information displayed in the command prompt console. It should state that “Jenkins is fully up and running”.
3) Open your web browser and enter “http://localhost:8080” to access the Jenkins UI.
You’ll initially be presented with an empty Jenkins dashboard. Jenkins jobs can be created to populate the dashboard.
Step 2::
To work with Selenium and Jenkins, configure Jenkins with Selenium by following these steps:
- Navigate to the Jenkins dashboard
- Select “Manage Jenkins”
- Click “Configure Jenkins”
- Within “JDK installation”, input the name into the JDK name field and define the Java Home path
Note: De-select “Install automatically” because it might update Java to a version that Selenium does not support. Remember to save your changes.
Your Jenkins is now set up with Selenium and prepared for usage. Both Jenkins and Selenium’s codebases are written in Java, so establishing the Java path enables communication between them.
Step 3::
Create a Selenium script and a TestNG XML file. The TestNG XML file is used to create a batch file, executed in Jenkins. Refer to the TestNG code below:
package session_2; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.Test; public class jenkins_demo { @Test public void testgooglrsearch(){ WebDriver driver = new FirefoxDriver(); //it will open the goggle page driver.get("http://google.in"); //we expect the title "Google" to be present String Expectedtitle = "Google"; //it will fetch the actual title String Actualtitle = driver.getTitle(); System.out.println("Before Assertion " + Expectedtitle + Actualtitle); //it will compare the actual title and the expected title Assert.assertEquals(Actualtitle, Expectedtitle); //print out the result System.out.println("After Assertion " + Expectedtitle + Actualtitle + " Title matched "); } }
Results: Before Assertion GoogleGoogle
After Assertion: GoogleGoogle Title matched
PASSED: testgooglrsearch
Next, create a TestNG XML file. Refer to the code below:
Step 4::
Within your project’s root directory, establish a “library” folder. Deposit all the necessary jar files required to run your Selenium script.
Step 5::
Generation of a Batch File and its Deployment in Jenkins
Generate a batch file by following these instructions:
1) Open Notepad and type:
Java –cp bin;lib/* org.testng.TestNG testng.xml
2) Save the file with a .bat extension. Confirm the file type is “Windows Batch File”. Double-click the batch file to ensure it executes correctly.
Note: Verify that you have a properly configured TestNG environment to run the tests.
Add the batch file in Jenkins by adhering to these guidelines:
1) Go to the Jenkins dashboard and originate a new job
2) Provide the job a name, and choose the freestyle project option
3) Click “Advanced options”
4) Enable custom workspace and specify the Selenium script project’s workspace path.
5) In the build section, select the option to execute the build via a Windows batch command and mention the batch file name.
6) Save the modifications and click on “Build now” to view the build result in console output.
Job Scheduling in Jenkins
To schedule your batch file, perform the following:
- Navigate to the Jenkins dashboard and select the job
- Click “Configure” and open the advanced options
- Within “Build triggers”, select “Build periodically” and input your cron job pattern
To understand the cron job pattern, refer to this link.
As an example, “* * * * *” signifies job execution every minute.
Do not forget to save your changes.
Adding Email Notifications
Apply these steps to add email notifications:
- Visit “Manage Jenkins”
- Click “Configure system”
- Select “Email notification”
- Provide your SMTP server address. Use “smtp.gmail.com” if you’re using Gmail.
- Tick the “Use SMTP Authentication” checkbox and paint the username, password, and SMTP port number (465 for Gmail, e.g.).
- Ensure the charset is set to UTF-8.
- Test the email configuration by clicking the “Test configuration” button.
You will now receive email notifications each time your build is successful or fails.
Executing Selenium Scripts via Command Line
To execute a Selenium script through the command line, adhere to these steps:
- Launch the command prompt and go to your project’s root path
- Set your script’s classpath by defining the location of your binary and library files
- Run your TestNG XML file using the command “java org.testng.TestNG testng.xml“
If you bump into the error “Could not find or load main class org.testng.TestNG,” close the command prompt, re-set the classpath, and repeat the steps. This should correct the error and allow your script to execute.
Wrapping Up
The integration of Jenkins with Selenium allows for the automatic execution of scripts whenever there is a code change, as well as the deployment to new environments. Jenkins enables storage of execution history and test reports.
Essentially, Jenkins is an influential tool for performing nightly builds and offers easy execution of test cases with a single click. Creating or scheduling builds allows running your test cases via a batch file.
Recommended further reading: Integrating Selenium with a Maven project
Note: This guide is part of the Selenium and DevOps tutorial series. Refer to the provided links for earlier and subsequent tutorials in the DevOps series.
Please leave your queries in the comments section below if you have any.
PREV Tutorial | NEXT Tutorial