LambdaTest Community

Find answers, support, and inspiration from other users

Questions
LambdaTest Community
Answered

How can I integrate Selenium WebDriver with Jenkins?

Can anyone please help me with the integration of selenium webdriver with jenkins.

Up Vote Down Vote 0 Votes
Posted 4 months ago

Answers


Follow the below step-by-step procedure to use Jenkins with Selenium

**Step #1: **

Download Jenkins from the official website of Jenkins – Jenkins. Download the latest .war file. Jenkins can be started via the command line or can run in a web application server.

Refer to the below steps for the execution through the command line:

  1. Open the command prompt and type java –jar and enter the path of a .war file

  1. Press enter and check if your Jenkins.war file started to run and check the status information on the command prompt console.

It should show – Jenkins is fully up and running

  1. Now check whether your Jenkins is ready to use; by default, it uses port 8080.

Type “http://localhost:8080” in the browser and press enter. It will show you Jenkins UI.

It will load the Jenkins dashboard empty by default. I created some Jenkins job in the above screenshot as an example and hence, it did not load empty.

Step #2:

To use Selenium with Jenkins you need to configure Jenkins with Selenium.

Follow the below steps:

  • Go to Jenkins dashboard
  • Click on manage Jenkins
  • Click on configure Jenkins
  • Click on JDK installation – In JDK name section enter the name, under Java Home section – give your java path

The radio button, Install automatically is checked by default. You need to uncheck it because it will automatically update with the new Java version and there might be a possibility that Selenium doesn’t support the new Java version. It is better to uncheck it. Now click on apply and save.

Your Jenkins is configured with Selenium and is now ready to be used with Selenium. Both Jenkins and Selenium code is written in Java. Hence, if you give the Java path then internally it will communicate and process your job.

**Step #3: **

Now, create a Selenium script and a TestNG XML file. This TestNG XML file will be required for creating a batch file and we will use that batch file in Jenkins. Refer below TestNG code:

Refer below TestNG code:

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
publicvoidtestgooglrsearch(){
 
WebDriver driver = newFirefoxDriver();
//it will open the goggle page
driver.get("http://google.in"); 
//we expect the title “Google “ should be present 
String Expectedtitle = "Google";
//it will fetch the actual title 
String Actualtitle = driver.getTitle();
System.out.println("Before Assetion " + Expectedtitle + Actualtitle);
//it will compare actual title and expected title
Assert.assertEquals(Actualtitle, Expectedtitle);
//print out the result
System.out.println("After Assertion " + Expectedtitle + Actualtitle + " Title matched ");
 }
}

Output: Before Assertion GoogleGoogle

After Assertion, GoogleGoogle Title matched

PASSED: testgooglrsearch

Create a TestNG xml file, refer below code:

Step #4:

Go to your project root directory and create a library folder.

Refer the screenshot below:

Now, add all your jar files which are required for running your Selenium script:

**Step #5: **

Creating a batch file and using it in Jenkins Create a batch file by following the below steps:

  1. Open the notepad and type-: Java –cp bin;lib/* org.testng.TestNG testng.xml

By doing this, Java –cp will compile and execute a .class file which is located at bin directory and all our executable jar file is located at lib directory and we are using a TestNG framework so specify org.testng.TestNG. Also, specify the name of xml file which will trigger the expected TestNG script.

  1. Save the file with .bat extension and check the type of file. It should be “windows batch file”. To cross-check whether the batch file is created properly, double-click the batch file and it will execute the code. Refer the below code of batch file:

Step #6:

Next, we need to add a batch file in Jenkins.

For adding the batch file follow the below steps:

  1. Go to the Jenkins dashboard, create a new job in Jenkins

  2. Click on a new item and enter the item name and check the freestyle project radio button

  3. Click Advanced options

  4. Click on use custom workspace and give your Selenium script project workspace path: “E:\Automation_workspace\Demo-testNG”

  5. Then go to Build and Select an option from the drop-down box, execute your build through Windows batch command

  6. And give your batch file name here – “run.bat”

  7. Click on apply and save

  8. Click on the build now and see the build result on console output

Up Vote Down Vote 0 Votes
Posted 2 months ago