How to create an Ent-To-End Test with Selenium WebDriver, TestNG project and Jenkins (CI)

Author: Ferran Sánchez

Objective:

Create an End-To-End test using Selenium WebDriver with Java, TestNG project and running them to Jenkins (CI) periodically every day.

Preconditions:

  1. Download Eclipse IDE and Extract -> eclipse luna http://www.eclipse.org/downloads/packages/eclipse-ide-java-developers/junosr2
  2. Download Java (jdk) and Install http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html.
  3. Download the followings selenium items from http://www.seleniumhq.org/download/
  • Selenium Standalone Server
  • Selenium Html Runner.
  • The Internet Explorer Driver Server
  • Selenium Client & WebDriver Language Bindings for “Java” -> recommend “selenium-java-2.47.1”
  • Google Chrome Driver (latest version).
  • Install TestNG -> Help > Install New Sofware > TestNG http://beust.com/eclipse/
  • Add the libraries to “scr” project  > Add External JARS…:
  • selenium-server-standalone-2.47.1.jar
  • All files from C:\SeleniumDoc\Seleniumtests\JAR\selenium-java-2.47.1\selenium-2.47.1\libs folder
  • selenium-java-2.47.1.jar and selenium-java-2.47.1-srcs.jar

Steps:

We have a Java project “FirstTestNgProject” with 3 TestNG classes.

1.- First class name: FirstClassTestNg. It contains the following tests:

  • Launch Chrome
  • Get amazon.es webpage. Verify url and return passed/failed on console.
  • Verify the webpage title and return passed/failed on console.
  • Verify linktext and navigation of “Ofertas” and “Ofertas seguidas” links.
  • Navigation to Amazon main webpage. Verify the title and return passed/failed on console.
public class FirstClassTestNg {
	public static WebDriver driver;// driver declaration for run browser in all methods

	// Quit browser
	@AfterClass
	public void closeBrowser(){
	driver.close();
	}
	// Launch amazon.es main page and verify the current url
	@Test(priority=1)
	public void LaunchAmazonMain() throws InterruptedException {
	System.setProperty("webdriver.chrome.driver", "C:/SeleniumDoc/chromedriver_win32/chromedriver.exe");
	driver = new ChromeDriver();
	driver.manage().window().maximize();
	driver.get("https://www.amazon.es");
	Thread.sleep(5000);
	String Url = driver.getCurrentUrl();
	System.out.println(Url);
	if (Url.equals("https://www.amazon.es/")){
	System.out.println("Amazon current URL - Passed");
	}
	else {
	System.out.println("Amazon current URL - Failed");
	}
	}
	// Verify the web page title and current url are correct. It's shown a message
	@Test(priority=2)
	public void testAmazonMainPage() throws InterruptedException{
	String PageTitle = driver.getTitle();//String url = driver.getCurrentUrl(); -> it is other option.
	System.out.println(PageTitle);
	if (PageTitle.equals("Amazon.es: compra online de electrónica, libros, deporte, hogar, moda y mucho más.")){
	System.out.println("Amazon Application Launched - Passed");
	}
	else {
	System.out.println("Amazon Application Not Launched - Failed");
	}
	}
	// Verify linkText and url
	@Test(priority=3)
	public void verifyLinkText() throws InterruptedException{
	Assert.assertEquals(driver.findElement(By.id("nav-your-amazon")).getText(), "Mi Amazon.es");
	// Verify linkText "Ofertas"
	String OfertasLink = driver.findElement(By.linkText("Ofertas")).getText();
	Assert.assertEquals(OfertasLink, "Ofertas");
	driver.findElement(By.linkText(OfertasLink)).click();
	Thread.sleep(9000);
	// Verify linkText "Ofertas seguidas"
	String OfertasSeguidas = driver.findElement(By.linkText("Ofertas seguidas")).getText();
	Assert.assertEquals(OfertasSeguidas, "Ofertas seguidas");
	driver.findElement(By.linkText(OfertasSeguidas)).click();
	Thread.sleep(9000);
	Assert.assertEquals(driver.findElement(By.className("gbh1-bold")).getText(), "Ofertas que estás siguiendo");
}
	@Test(priority=4)
	public void verifyReturnHome() throws InterruptedException{
	driver.navigate().to("https://www.amazon.es");
	Thread.sleep(9000);
	String PageTitle = driver.getTitle();//String url = driver.getCurrentUrl(); -> it is other option.
	System.out.println(PageTitle);
	if (PageTitle.equals("Amazon.es: compra online de electrónica, libros, deporte, hogar, moda y mucho más.")){
	System.out.println("Amazon Application Home page - Passed");
	}
	}
}

2.- Second class name: SecondClassTestNgIe. It contains the following tests:

  • Launch Iexplorer browser
  • Get amazon.es webpage. Verify url and return passed/failed on console.
  • Navigate to gmail.com webpage. Verify the title and return passed/failed on console.
  • Return to back webpage. Verify the text “Mi  amazon.es”.
public class SecondClassTestNgIe {
	public static WebDriver driver;// driver declaration for run browser in all methods

	// Launch amazon.es main on IExplorer browser
	@Test
	public void LaunchAmazonMain() throws InterruptedException {
	// Launch webDriver
	System.setProperty("webdriver.ie.driver", "C:/SeleniumDoc/IEDriverServer_Win32_2.53.1/IEDriverServer.exe");
	WebDriver driver = new InternetExplorerDriver();
	driver.manage().window().maximize();
	// Amazon Main Page
	driver.get("https://www.amazon.es");
	Thread.sleep(5000);
	String Url = driver.getCurrentUrl();
	System.out.println(Url);
	if (Url.equals("https://www.amazon.es/")){
	System.out.println("Amazon current URL - Passed");
	}
	else {
	System.out.println("Amazon current URL - Failed");
	}
	// Navigate to gmail web page
	driver.navigate().to("www.gmail.com");
	Thread.sleep(5000);
	String GmailTitle = driver.getTitle();
	System.out.println(GmailTitle);
	if (GmailTitle.equals("Gmail: Correo electrónico y almacenamiento gratuito de Google")){
		System.out.println("The Gmail web page tittle - Passed");
	}
	else {
			System.out.println("The Gmail web page tittle - Failed");
		}
	// Return to Amazon Main Page
	driver.navigate().back();
	Thread.sleep(5000);
	Assert.assertEquals(driver.findElement(By.id("nav-your-amazon")).getText(), "Mi Amazon.es");
	// Close IExplorer and Quit WebDriver
	driver.close();
	driver.quit();
	}
}

3.- Third class name: ThirdClassTestNg. It contains the following tests:

  • Launch Chrome browser.
  • Data driven test from external DataInput.txt file.
  • Data input login and password failed from external file .txt.
  • Verify the url and count the iteration. Verify the error and return message passed/failed.
public class ThirdClassTestNg {
	public static WebDriver driver;// driver declaration for run browser in all methods
	public static String line;
	public static int Iteration;

	// Data Driven test from DataInput.txt file for login and password
	@Test
	public void DataDrivenLogin() throws IOException, InterruptedException {
		FileReader file = new FileReader("C:/SeleniumDoc/FirsTestNgProject/bin/TestNgPackage/DataInput.txt");
		BufferedReader br = new BufferedReader(file); 

		int count =0;
		Iteration =0;
		while ((line = br.readLine()) != null){
		count = count +1;
		if (count > 1) {
		Iteration = Iteration + 1;
		String [] InputData = line.split(", ", 2);//Read the two columns from DataInput.txt file

		System.setProperty("webdriver.chrome.driver", "C:/SeleniumDoc/chromedriver_win32/chromedriver.exe");
		driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.get("https://www.amazon.es");
		Thread.sleep(5000);
		driver.findElement(By.id("nav-link-yourAccount")).click();
		driver.findElement(By.id("ap_email")).sendKeys(InputData[0]);
		driver.findElement(By.id("ap_password")).sendKeys(InputData[1]);
		driver.findElement(By.id("signInSubmit")).click();
		Thread.sleep(5000);

		String url = driver.getCurrentUrl();

		// Login user and password failed
		if (url.equals("https://www.amazon.es/ap/signin")){
		Assert.assertEquals(driver.findElement(By.className("a-spacing-small")).getText(), "Iniciar sesión");
		System.out.println("Iteration-"+Iteration+ " - Login Unsuccessful - Failed");
		}
		else {
		System.out.println("Iteration-"+Iteration+ " - Login Successful - Passed");
		}
		driver.close();
		}
		}
		file.close();
		br.close();
  }
}

4.- The main point for execute all TestNG classes is the XML file. Generate XML TestNG for run project. The name is: “testng.xml”

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”&gt;
<suite name=”Suite”>
<test name=”Test”>
<classes>
<class name=”TestNgPackage.FirstClassTestNg”>
<methods>
<include name=”LaunchAmazonMain” />
<include name=”testAmazonMainPage” />
<include name=”verifyLinkText” />
<include name=”verifyReturnHome” />
</methods>
</class>
<class name=”TestNgPackage.SecondClassTestNgIe”>
<methods>
<include name=”LaunchAmazonMain” />
</methods>
</class>
<class name=”TestNgPackage.ThirdClassTestNg”>
<methods>
<include name=”DataDrivenLogin” />
</methods>
</class>
</classes>
</test> <!– Test –>
</suite> <!– Suite –>

5.- Download Jenkins (Continuous Integration) and run xml file periodically every day.

  • Download Jenkins (jenkins.war file) https://jenkins.io/download/
  • Open jenkins.war execute in CMD> Project_home_Directory> java -jar Jenkins.war
  • Ex…       cd C:\SeleniumDoc\FirsTestNgProject
  •                 C:\SeleniumDoc\FirsTestNgProject> java -jar Jenkins.war 
  • Go to browser> localhost:8080 > Manage Jenkins:
  • > System configuration > main jenkins directory -> C:\Users\<userName>\.jenkins -> email: your mail address.
  • > Manage Jenkins > JDK Installations -> JDK NAME: JAVA_HOME -> C:\Program Files\Java\jdk1.8.0_73 -> Apply and Save.

jenkins-1

  • Create a New Task > Name: SeleniumFirstProject >  Build a free-style software project > OK
  • > Navigate to Advanced Project Options > Use custom workspace -> C:\SeleniumDoc\FirsTestNgProject
  • Create .bat file execution > new .txt file, including: java -cp bin;C:\SeleniumDoc\Seleniumtests\JAR\selenium-java-2.47.1\selenium-2.47.1\libs/* org.testng.TestNG testng.xml
  • And save with the name “run.bat” on folder C:\SeleniumDoc\FirsTestNgProject.

jenkins-8-_packageexplorer

  • Execute Windows batch command -> run.bat

jenkins-2

  • Apply and Save.
  • Build Now. -> Results are shown in Console output.

jenkins-4

jenkins-5

jenkins-6.png

6.- Add a nice TestNG report results with TestNG Results plugin to Jenkins.

  • Download “TestNG Results plugin” for xml file on Jenkins manage plugins.
  • Mofify the configuration of “FirstTestNgproject” Jenkins project.
  • Add new action after execution in the project.
  • Select “Publish TestNG Results” > TestNG XML report pattern = **/testng-results.xml > Apply and Save.
  • Build now.
  • After build appears “TestNG Results” link > you can see the details of results on table. Also, you can see the methods results.

jenkins-9

jenkins-10

7.- Schedule your build in Jenkins for periodic execution:

  • Jenkins will accept 5 parameter lets discuss one by one. For our example is:
  • 01 * * * * -> means every hour + 1 minut. Ex. 08:01, 09:01, 10:01…
  • * * * * *
  • Here the first parameter- specify minute and range will vary from 0-59
  • Here the second parameter- specify hours and range will vary from 0-11
  • Here the third parameter- specify day and range will vary from 0-7 here 0 and 7 will be Sunday
  • Here the fourth parameter- specify month and range will vary from 1-12
  • Here the fifth parameter- specify year so here you can specify *
  • Example 1- if you specify    00 22 * * *  it means your build will run daily @ 10 PM
  • Example 2- if you specify    50 * * * *  it means your build will run daily  50 min  after every hour
  • Example 3- if you specify    00 22 1 * *  it means your build will run every Monday @ 10 PM

jenkins-7

8.- References: