Introduction To TestNG Framework

Posted By : Mohd Sheemal | 27-May-2018

Introduction of TestNG Framework :

In selenium using java there are two frameworks are there -

1- JUnit

2- TestNG (Next Generation)

Selection of testing framework is depends on programing platform :

If you select java, supported frameworks are :

Java – Junit, TestNG.

C# .Net – Nunit.

PHP – Behat + Mink.

Ruby – Repect, Test:Unit.

Python – PyUnit, Py:Test.

 

-> TestNG is a testing framework was design in to simplify a broad range of testing needs from unit testing to system testing. It is open source and third party . It is also used for other level of testing.

-> Initially it was developed for unit testing, now used for all levels of testing.

-> TestNG is a open-source software where NG stands for Next Generation.

-> TestNG inspired by JUnit & NUnit but introduced some new functionalities.

 

Advantages of TestNG :

-> TestNG annotations are easy to create testcases.

-> Testcases can be grouped and prioritized more easily.

-> Executing multiple programs / Classes using XML file.

 

Scenario -1

-> We have one class and it has four testcases, Execute then four testcases will be executed continously.

Scenario -2

-> We have four java classes, each class having four testcases (Total sixteen testcases),

Execute using TestNG all four classes can be executed continously.

 

Install TestNG and write first TestNG test case:

1- Launch Eclipse IDE and Select Help menu.

2- Select Install New Software.

 

3- Click “Add Button”.

 

4- Enter name as TestNG.

 

5- Enter url as : http://beust.com/eclipse/.

 

6- Click OK.

Then TestNG option will be populated.

7- Select TestNG.


 

8- Next > Next > Accept the Agreement > Finish.

 

After finish restart the eclipse IDE then you can use TestNG.

 

Verify the TestNG -

Write a TestNG program / Test case and execute :

 

Test Steps :

1- Launh the browser.

2- Navigate to google home page.(https://www.google.co.in/)

 

Verification point :

1- Capture the page title and compare with expected.

Expected : “Google”.

Test data : N/A.

 

package com.framework;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import junit.framework.Assert;

public class Example1 
{

	@Test
	public void example()
	{
	   System.setProperty("webdriver.chrome.driver","./exefiles/chromedriver");
       WebDriver driver=new ChromeDriver();
       driver.manage().window().maximize();
       driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
       driver.get("https://www.google.co.in/");
       String pageTitle = driver.getTitle();
       Assert.assertEquals(pageTitle, "Google");
       driver.close();
    }
}

 

 

Note : Main method is not used in TestNG program.

-> TestNG program contains methods only, that contain annotation.

-> If we don't write @Test annotation then method / testcase won't be executed.

-> Create Multiple test cases :

1- Launch browser().

2- Verify Title().

3- Close Browser().

In this TestNG program there are three testcases -

 

package com.framework;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import junit.framework.Assert;

public class Example2 
{
	static WebDriver driver;
	@Test (priority=1)
	public void launchBrowser()
	{
	   System.setProperty("webdriver.chrome.driver","./exefiles/chromedriver");
       driver=new ChromeDriver();
       driver.manage().window().maximize();
	}
	@Test (priority=2)
	public void verifyTitle()
	{
		driver.get("https://www.google.co.in/");
	    String pageTitle = driver.getTitle();
	    Assert.assertEquals(pageTitle, "Google");
       
    }
	@Test (priority=3)
	public void closeBrowser()
	{
		driver.close();
	       
    }
}

-> TestNG test cases are executed in alphabetical order, If you want to control the test execution process then use priority attribute.

Related Tags

About Author

Author Image
Mohd Sheemal

Mohd Sheemal is a Bright QA Engineer. He has a good knowledge over Selenium.

Request for Proposal

Name is required

Comment is required

Sending message..