Running TestNG Simple Test with Jenkins Integration from Scratch.
I have started learning TestNG and Jenkins integration from scratch, though it is very easy, but, If you have no knowledge about the TestNG and its feature and Jenkins basic it is hard to integrate and running a simple TestNG test with Jenkins.
So, for all the novice learning the TestNG and Jenkins users, hope you can get started running your first test with Jenkins
Pre-requisite:
1) Jdk
2)Selenium Jars
3)Eclipse IDE (Editor)
4)Integrated TestNG Eclipse plugin
5)Running instance of Jenkins
Steps 1: Create a TestNG Test:
Open Eclipse>Create New Java Project>in src folder Create a TestNG class
Append the below example code:
package firsttestngpackage;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;import org.testng.Assert;import org.testng.annotations.Test;public class FirstTestNGFile { public String baseUrl = "http://demo.guru99.com/test/newtours/"; String driverPath = "D:\\geckodriver\\geckodriver.exe"; public WebDriver driver ; @Test public void f() { System.out.println("launching firefox browser"); System.setProperty("webdriver.firefox.marionette", driverPath); driver = new FirefoxDriver(); driver.get(baseUrl); String expectedTitle = "Welcome: Mercury Tours"; String actualTitle = driver.getTitle(); Assert.assertEquals(actualTitle, expectedTitle); driver.close(); }}
Now Check for any errors:
Add the Java Compiler in Build Path:
2. check your project structure should look similar like:
Note:The test-output folder will generate once you run the project.
Add description
2. Running The Project From Eclipse:
Now, Go to Run>Run TestNG Project. It will generate the test-output folder on a successful run.
3. Running the Project From Command Prompt.
Now, after Running the project you will find there is no lib folder,
I have created it manually to run the TestNG project from Command line:
You can create the same and add copy paste all the jars of selenium into the eclipse under lib folder.
Now, for running the project from command line, you have to convert the class into TestNG.xml by:
Step i: Open Eclipse and create a Java class
Step ii. Keep all the library files in a folder (here I create a folder name “lib”)
Step iii: Write a Java program
Step iv: Convert the Java Program into TestNG
right click on class>TestNG>Convert to TestNG
it will create the TestNg.xml in your workspace
Now, follow below command:
set the classpath similar to below:
C:\personal\FirstTestNGProject>set classpath=C:\personal\FirstTestNGProject\bin;
C:\personal\FirstTestNGProject\lib\*
and run the project with below command:
C:\personal\FirstTestNGProject>java org.testng.TestNG C:\personal\FirstTestNGProject\testng.xml
Also, you can create the bat file using below snippet and save it in testNG.bat
set projectLocation=C:\personal\FirstTestNGProjectcd %projectLocation%set classpath=%projectLocation%\bin;%projectLocation%\lib\*java org.testng.TestNG %projectLocation%\testng.xmlpause
It will run successfully from double clicking.
4. Running With Jenkins:
Now, you have your .bat file with you. Now just create a new Job Freestysle job in jenkins.
Provide the custom workspace in General tab.
And in build step give the name of your TestNG.bat.
Add description
Add description
Now, click build now.
It will run the job on your system and give the below output:
Add description
:) You have successfully integrated TestNG Test with Selenium.
You Can Find the project on my Github Repo on:
Happy Learning!