TestNG Framework for Selenium

TestNG Framework for Selenium. Install TestNG in Eclipse IDE, add TestNG library to Selenium WebDriver, create & execute TestNG Test cases.

TestNG Framework for Selenium, What is TestNG?, TestNG Annotations, TestNG Test Cases, TestNG Assert Methods, and TestNG Result Reporting.

TestNG Framework Tutorial

TestNG is designed to cover all categories of tests: Unit, Functional, End-to-end, Integration, etc…

TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionality that makes it more powerful and easier to use.

TestNG is basically a Unit Testing Framework, primarily designed for Unit Testing of Java Projects.

TestNG Testing Framework for Selenium

We use Testing Framework as Test Runner in Selenium, it is used to prioritize Test case, Grouping Test cases, Execute Test batches, and generate Test Reports.

If we select “Java Programming for Selenium”, then TestNG can be used as Testing Framework in Selenium Testing.

  1. Install TestNG and write first TestNG Program

  2. Create multiple Test cases & Run

  3. Execute/Run multiple programs using XML

  4. Grouping Test Cases

  5. Parallel Testing

  6. Data-Driven Testing using @DataProvider annotation


1. Install TestNG and write first TestNG Program

Steps for Installing TestNG for Eclipse IDE:

• Launch Eclipse IDE
• Select “Help” Menu
• Eclipse Marketplace
• Search for “TestNG, in the search bar
• Install TestNG
• Restart Eclipse IDE

a. Manual Test case

Test Name: Verify Page Title (Google)

Test Steps:
i. launch Browser
ii. Navigate to Google home page

Expected: Google

Test Data/Input:
NA

Verification point
Capture the Page Title and compare it with expected

TestNG Syntax:

i. main method is not used in TestNG programs
ii. TestNG programs contain methods only that contain annotations
iii. If we don’t use @Test annotation then the method won’t be executed.

TestNG Test case

@Test
public void launch() {
System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\user\\Desktop\\chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(“https://www.google.com/”);

String pageTitle = driver.getTitle();

Assert.assertEquals(pageTitle, “Google”);
driver.close();
}


2. Create multiple Test cases & Run

Example 1:

TestNG Class/Program:

WebDriver driver;

@Test (priority = 1)
public void launchBrowser() {
System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\user\\Desktop\\chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test (priority = 2)
public void verifyTitle() {
driver.get(“https://www.google.com/”);
String pageTitle = driver.getTitle();
Assert.assertEquals(pageTitle, “Google”);
}
@Test (priority = 3)
public void closeBrowser() {
driver.close();
}

Example 2:

Test cases:

i. launchBrowser()
ii. verifyGoogleTitle()
iii. verifyYahooTitle()
iv. verifyIciciBankTitle()
v. verifyBankofAmericaTitle()
vi. closeBrowser()

TestNG Class/Program:

WebDriver driver;

@Test (priority = 1)
public void launchBrowser() {
System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\user\\Desktop\\chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test (priority = 2)
public void verifyGoogleTitle() {
driver.get(“https://www.google.com/”);
String pageTitle = driver.getTitle();
Assert.assertEquals(pageTitle, “abcde”);
}
@Test (priority = 3)
public void verifyYahooTitle() {
driver.get(“https://in.yahoo.com/”);
String pageTitle = driver.getTitle();
Assert.assertEquals(pageTitle, “Yahoo India | News, Finance, Cricket, Lifestyle and Entertainment”);
}

@Test (priority = 4)
public void verifyIciciBankTitle() {
driver.get(“https://www.icicibank.com/”);
String pageTitle = driver.getTitle();
Assert.assertEquals(pageTitle, “Personal Banking & Netbanking Services Online – ICICI Bank”);
}

@Test (priority = 5)
public void verifyBankofAmericaTitle() {
driver.get(“https://www.bankofamerica.com//”);
String pageTitle = driver.getTitle();
Assert.assertEquals(pageTitle, “Bank of America – Banking, Credit Cards, Loans and Merrill Investing”);
}
@Test (priority = 6)
public void closeBrowser() {
driver.close();
}

}

Note: Using the “priority” attribute we can prioritize Test cases then we can control the test execution process


3. Execute/Run multiple programs using XML

TestNG Annotations

@Test – The annotated method is a Test Case

@BeforMethod – The annotated method will be run before each Test case/method in the current class is invoked
@AfterMethod -The annotated method will be run after each Test case in the current class have been run

@BeforeClass – The annotated method will be run the first Test Method in the current class is invoked
@AfterClass – The annotated method will be run after all the Test methods in the current class have been run

@BeforeTest – The method which comes under the @BeforeTest annotation will be executed first before any test belonging to that folder.
@AfterTest – The test method under the @AfterTest annotated method is executed after the execution of all the test methods of the available classes which are kept inside the tag in the testng.xml file in the suite.

Examples:

Navigation for creating an XML file in Eclipse IDE,

In Eclipse IDE,
• Select Java Package and right-click

• New
• Other
• Enter “TestNG”
• Enter source and package names
• Enter XML file name
• Finish

XML File to execute/run multuple programs/clases:

<suite name =”SuiteName”>
<test name = “TestName” >
<classes>
<class name = “Package.Class1Name” />
<class name = “Package.Class2Name” />
<class name = “Package.Class3Name” />
</classes>
</test name = “TestName” >
</suite name =”Suite Name”>

1. Execute multiple programs / classes using xml with @BeforeMethod and @AfterMethod

Class1 File:

public class Class1 {
WebDriver driver;

@BeforeMethod
public void launchBrowser(){
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test (priority=1)
public void verifyGoogleTitle(){
driver.get(“https://www.google.com/”);
Assert.assertEquals(“Google”, driver.getTitle());
}
@Test (priority=2)
public void verifyYahooTitle(){
driver.get(“https://in.yahoo.com/”);
Assert.assertEquals(“Yahoo”, driver.getTitle());
}
@Test (priority=3)
public void verifyGcritTitle(){
driver.get(“http://www.gcrit.com/build3/admin/”);
Assert.assertEquals(“GCR Shop”, driver.getTitle());
}
@AfterMethod
public void closeBrowser(){
driver.close();
}
}

Class2 File:

public class Class2 {
WebDriver driver;

@BeforeMethod
public void launchBrowser(){
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test (priority=4)
public void verifyBankOfAmericaTitle(){
driver.get(“https://www.bankofamerica.com/”);
Assert.assertEquals(“Bank of America – Banking, Credit Cards, Home Loans and Auto Loans”, driver.getTitle());
}
@Test (priority=5)
public void verifyICICIBankTitle(){
driver.get(“https://www.icicibank.com/”);
Assert.assertEquals(“Personal Banking, Online Banking Services – ICICI Bank”, driver.getTitle());
}
@Test (priority=6)
public void verifyGcreddyTitle(){
driver.get(“http://www.gcreddy.com/”);
Assert.assertEquals(“Software Testing – A blog about Manual Testing, Selenium, UFT/QTP, SQL, Java and Python Step by Step Tutorials by G C Reddy.”, driver.getTitle());
}
@AfterMethod
public void closeBrowser(){
driver.close();
}
}

2. Execute multiple programs / classes using xml with @BeforeClass and @AfterClass

Class1 File:

public class Class1 {
WebDriver driver;

@BeforeClass
public void launchBrowser(){
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test (priority=1)
public void verifyGoogleTitle(){
driver.get(“https://www.google.com/”);
Assert.assertEquals(“Google”, driver.getTitle());
}
@Test (priority=2)
public void verifyYahooTitle(){
driver.get(“https://in.yahoo.com/”);
Assert.assertEquals(“Yahoo”, driver.getTitle());
}
@Test (priority=3)
public void verifyGcritTitle(){
driver.get(“http://www.gcrit.com/build3/admin/”);
Assert.assertEquals(“GCR Shop”, driver.getTitle());
}
@AfterClass
public void closeBrowser(){
driver.close();
}
}

Class2 File:

public class Class2 {
WebDriver driver;

@BeforeClass
public void launchBrowser(){
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test (priority=4)
public void verifyBankOfAmericaTitle(){
driver.get(“https://www.bankofamerica.com/”);
Assert.assertEquals(“Bank of America – Banking, Credit Cards, Home Loans and Auto Loans”, driver.getTitle());
}
@Test (priority=5)
public void verifyICICIBankTitle(){
driver.get(“https://www.icicibank.com/”);
Assert.assertEquals(“Personal Banking, Online Banking Services – ICICI Bank”, driver.getTitle());
}
@Test (priority=6)
public void verifyGcreddyTitle(){
driver.get(“http://www.gcreddy.com/”);
Assert.assertEquals(“Software Testing – A blog about Manual Testing, Selenium, UFT/QTP, SQL, Java and Python Step by Step Tutorials by G C Reddy.”, driver.getTitle());
}
@AfterClass
public void closeBrowser(){
driver.close();
}
}

3. Execute multiple programs / classes using xml with @BeforeTest and @AfterTest Annotations

Parent File:

public class ClassP {
static WebDriver driver;

@BeforeTest
public void launchBrowser(){
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@AfterTest
public void closeBrowser(){
driver.close();
}
}

Class1 File:

public class Class1 extends ClassP {

@Test (priority=1)
public void verifyGoogleTitle(){
driver.get(“https://www.google.com/”);
Assert.assertEquals(“Google”, driver.getTitle());
}
@Test (priority=2)
public void verifyYahooTitle(){
driver.get(“https://in.yahoo.com/”);
Assert.assertEquals(“Yahoo”, driver.getTitle());
}
@Test (priority=3)
public void verifyGcritTitle(){
driver.get(“http://www.gcrit.com/build3/admin/”);
Assert.assertEquals(“GCR Shop”, driver.getTitle());
}
}

Class2 File:

public class Class2 extends ClassP{

@Test (priority=4)
public void verifyBankOfAmericaTitle(){
driver.get(“https://www.bankofamerica.com/”);
Assert.assertEquals(“Bank of America – Banking, Credit Cards, Home Loans and Auto Loans”, driver.getTitle());
}
@Test (priority=5)
public void verifyICICIBankTitle(){
driver.get(“https://www.icicibank.com/”);
Assert.assertEquals(“Personal Banking, Online Banking Services – ICICI Bank”, driver.getTitle());
}
@Test (priority=6)
public void verifyGcreddyTitle(){
driver.get(“http://www.gcreddy.com/”);
Assert.assertEquals(“Software Testing – A blog about Manual Testing, Selenium, UFT/QTP, SQL, Java and Python Step by Step Tutorials by G C Reddy.”, driver.getTitle());
}
}


4. Grouping Test Cases

a. tags in the XML file for executing multiple programs
suite,
test,
classes,
class,

b. Tags in the XML file for grouping Test Cases
suite,
test,
groups,
run,
include,
classes,
class,

Test Groups

1.Sanity Tests

i. launchBrowser()
ii. verifyGoogleTitle()
iii. verifyGcritTitle()
iv. closeBrowser()

2. Regression Tests

i. launchBrowser()
ii. verifyYahooTitle()
iii. verifyGcritTitle()
iv. verifyGcreddyTitle()
v. closeBrowser()

XML File:

<suite name=”Suite” >
<test name=”Test”>
<groups>
<run>
<include name = “regression”/>
</run>
</groups>
<classes>
<class name=”abcd.Class1″/>
</classes>
</test> <!– Test –>
</suite> <!– Suite –>

Class File:

public class Class1 {
WebDriver driver;

@Test(groups = {“sanity”, “regression”}, priority=1)
public void launchBrowser(){
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test(groups = {“sanity”, “regression”}, priority=6)
public void closeBrowser(){
driver.close();
}

@Test (groups = {“sanity”}, priority=2)
public void verifyGoogleTitle(){
driver.get(“https://www.google.com/”);
Assert.assertEquals(“Google”, driver.getTitle());
}
@Test (groups = {“regression”}, priority=3)
public void verifyYahooTitle(){
driver.get(“https://in.yahoo.com/”);
Assert.assertEquals(“Yahoo”, driver.getTitle());
}
@Test (groups = {“sanity”, “regression”}, priority=4)
public void verifyGcritTitle(){
driver.get(“http://www.gcrit.com/build3/admin/”);
Assert.assertEquals(“GCR Shop”, driver.getTitle());
}
@Test (groups = {“regression”}, priority=5)
public void verifyGcreddyTitle(){
driver.get(“http://www.gcreddy.com/”);
Assert.assertEquals(“Software Testing – A blog about Manual Testing, Selenium, UFT/QTP, SQL, Java and Python Step by Step Tutorials by G C Reddy.”, driver.getTitle());
}
}


5. Parallel Testing

a. Parallel Test Execution (Methods)

XML File

<suite name=”Suite” parallel=”methods” thread-count =”3″>
<test name=”Test”>
<classes>
<class name=”abcd.NewTest5″/>
</classes>
</test> <!– Test –>
</suite> <!– Suite –>

Class File

public class NewTest5 {
@Test
public void testCase1(){
long id = Thread.currentThread().getId();
System.out.println(“Test Case 1 is Successful – Thread id is: “+ id);
}
@Test
public void testCase2(){
long id = Thread.currentThread().getId();
System.out.println(“Test Case 2 is Successful – Thread id is: “+ id);
}
@Test
public void testCase3(){
long id = Thread.currentThread().getId();
System.out.println(“Test Case 3 is Successful – Thread id is: “+ id);
}
}

Note: A Thread is a concurrent unit of execution.

b. Parallel Test Execution (Classes)

XML File

<suite name=”Suite” parallel=”classes” thread-count =”2″>
<test name=”Test”>
<classes>
<class name=”abcd.NewTest5″/>
<class name=”abcd.NewTest6″/>
</classes>
</test> <!– Test –>
</suite> <!– Suite –>

Class 1 File

public class NewTest5 {
@Test
public void testCase1(){
long id = Thread.currentThread().getId();
System.out.println(“Test Case 1 is Successful – Thread id is: “+ id);
}
@Test
public void testCase2(){
long id = Thread.currentThread().getId();
System.out.println(“Test Case 2 is Successful – Thread id is: “+ id);
}
@Test
public void testCase3(){
long id = Thread.currentThread().getId();
System.out.println(“Test Case 3 is Successful – Thread id is: “+ id);
}
}

Class 2 File

public class NewTest6 {
@Test
public void testCase4(){
long id = Thread.currentThread().getId();
System.out.println(“Test Case 4 is Successful – Thread id is: “+ id);
}
@Test
public void testCase5(){
long id = Thread.currentThread().getId();
System.out.println(“Test Case 5 is Successful – Thread id is: “+ id);
}
@Test
public void testCase6(){
long id = Thread.currentThread().getId();
System.out.println(“Test Case 6 is Successful – Thread id is: “+ id);
}
}

TestNG Framework for Selenium


6. Data Driven Testing using @DataProvider annotation

If you want to handle Excel files in Java then we need to download the excel jar file from the internet, add the excel jar to the Java project in Eclipse IDE.

Examples:
i. Read Data (String Data) from an Excel file and conduct Data Driven Testing for Admin Login Functionality.

public class DataDriven {
WebDriver driver;

@Test (dataProvider = “loginData”)
public void adminLogin(String User, String Pwd) {
System.setProperty(“webdriver.chrome.driver”, “E://chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(“http://gcreddy.com/project/admin/login.php”);
driver.findElement(By.name(“username”)).sendKeys(User);
driver.findElement(By.name(“password”)).sendKeys(Pwd);
driver.findElement(By.id(“tdb1”)).click();

String url = driver.getCurrentUrl();

Assert.assertEquals(url, “http://gcreddy.com/project/admin/index.php”);
}

@AfterMethod
public void closeBrowser() {
driver.close();
}

@DataProvider (name=”loginData”)
public Object[][]readExcel() throws BiffException, IOException {
File f = new File(“C:\\Users\\gcreddy\\Desktop\\input.xls”);
Workbook w = Workbook.getWorkbook(f);
Sheet s= w.getSheet(“login”);

int rows = s.getRows();
int columns=s.getColumns();
//System.out.println(rows+”, “+columns);

String inputData [][] = new String [rows] [columns];

for (int i=0; i<rows; i++) {
for (int j=0; j<columns; j++) {
Cell c = s.getCell(j, i);
inputData[i][j] = c.getContents();
//System.out.println(inputData[i][j]);
}
}
return inputData;

}
}

ii. Read data (Integer) from an Excel file and conduct an “Addition” Operation.

(Read data from 2nd sheet of the file, add first 3 values and compare with 4th field. (addion of first 3 fields is 4th field.)

public class DataDriven2 {

@Test (dataProvider=”myData”)
public void addition(String val1, String val2, String val3, String val4) {
int num1= Integer.parseInt(val1);
int num2= Integer.parseInt(val2);
int num3= Integer.parseInt(val3);
int num4= Integer.parseInt(val4);

Assert.assertEquals(num1+num2+num3, num4);
}

@DataProvider(name = “myData”)
public Object[][]readMyfile() throws BiffException, IOException {
File myFile = new File(“C:\\Users\\gcreddy\\Desktop\\input.xls”);
Workbook myWorkbook = Workbook.getWorkbook(myFile);
Sheet mySheet = myWorkbook.getSheet(1);

int rows = mySheet.getRows();
int columns = mySheet.getColumns();

//System.out.println(rows+”, “+columns);

String inputData [][] = new String [rows][columns];

for (int i=0; i<rows; i++) {
for (int j=0; j<columns; j++) {
Cell myCell = mySheet.getCell(j, i);
inputData [i][j] = myCell.getContents();

}
}
return inputData;
}
}

TestNG Framework for Selenium


Manual Testing Tutorial

Java Tutorial

Python Tutorial

SQL Tutorial

Selenium, Java, and Testing Videos

Follow me on social media: