TestNG Framework for Selenium


TestNG Framework for Selenium

1) Introduction to TestNG Framework
2) Install TestNG and write first TestNG Test Case
3) Create multiple Test cases and Run.
4) Create and execute multiple Programs / Classes using XML
5) Grouping Test Cases and Run
6) Parallel Test Execution
7) Data Driven Testing using DataProvider 
--------------------------------------------------
1) Introduction to TestNG Testing Framework

In Selenium using Java there are two Testing frameworks available,

i) JUnit

ii) TestNG
-------------------------
Note:

Java - JUnit, TestNG
C# - NUnit, haskell
PHP - Behat + Mink
Ruby - Rspec, Test::Unit
Python - Pyunit, Py.test Etc...
------------------------------------------------------------
TestNG Framework Features

> TestNG is a Testing Framework designed to simplify, a broad range of Testing needs from Unit Testing to System Testing.
> Initially developed for Unit Testing, now used for all kinds of Testing
> TestNG is an open source framework, where NG stands for Next Generation.
> TestNG inspered by JUnit (Java Platform), and NUnit (.NET platform) but introduced some new functionalities.
--------------------------------------------------------
Advantages of TestNG:

i) TestNG Annotations are easy to create Test Cases
ii) Test cases can be grouped and prioritized more easily.
iii) Supports Parameterization.
iv) Execute multiple programs / classes using XML.
v) Generate HTML Reports.
vi) Parallel Test Execution.
--------------------------------------------------------
2) Install TestNG and write first TestNG Test Case.

In Eclipse IDE,

> Help menu
> Select "Install new Software"
> Click "Add"
> Enter Name "TestNG"
> Enter URL as "http://beust.com/eclipse/"
> Click "OK"
> Select "TestNG"
> Next > Next > accept the agreement> Finish
------------------------------------------------
Write first TestNG Test Case.

Manual Test case:

Test Case Name: Verify Page Title (Google)

Test Steps:
i) Launch the Browser
ii) Navigate to Google Homepage (www.google.com)

Verification Point:
Capture the Page Title and Compare with expected.

Expected: Google

Status:
-----------------------------------------------------
TestNG Test Case
Note:
i) main method is not used in TestNG Programs.
ii) TestNG program contains only Methods that contain @Test (Annotation)
iii) If we don't write @Test Annotation then the method is not going to be executed.
---------------------------------------------------------------
TestNG program/TestNG

public class Amitha {
@Test
public void verifyTitle(){
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
Assert.assertEquals("Google", driver.getTitle());
driver.close();
}
}
-------------------------------------------------------------
3) Create multiple Test cases and Run.

Create multiple Test Cases in a program / Class and Run

Notes:

Test Cases as per Program:
i) launchBrowser()
ii) verifyTitle()
iii) closeBrowser()
--------------------------------
Note: TestNG Test Cases are executed in Alphabetical order.

Test Cases as per Alphabetical order in this TestNG Program
i) closeBrowser()
ii) lanuchBrowser()
iii) verifyTitle()
-------------------------------
Example:
public class TestCase2 {
public static WebDriver driver;
@Test
public void launchBrowser(){
driver = new FirefoxDriver();
}
@Test
public void verifyTitle(){
driver.get("https://www.google.com");
String pageTitle = driver.getTitle();
Assert.assertEquals("Google", pageTitle);
}
@Test
public void closeBrowser(){
driver.close();
}
}
------------------------------------------------------------
Use priority Attribute to control the Test Execution, 
If we use priority Attribute, then program will Run as priority (Small Number to Big Number)

public class TestCase2 {
public static WebDriver driver;
@Test (priority = 1)
public void launchBrowser(){
driver = new FirefoxDriver();
}
@Test (priority = 2)
public void verifyTitle(){
driver.get("https://www.google.com");
String pageTitle = driver.getTitle();
Assert.assertEquals("Google", pageTitle);
}
@Test (priority = 3)
public void closeBrowser(){
driver.close();
}
}
----------------------------------------------------------
We can give any number for priority, but it will execute from Small Number to Big Number.
public class TestCase2 {
public static WebDriver driver;
@Test (priority = -4)
public void launchBrowser(){
driver = new FirefoxDriver();
}
@Test (priority = 2)
public void verifyTitle(){
driver.get("https://www.google.com");
String pageTitle = driver.getTitle();
Assert.assertEquals("Google", pageTitle);
}
@Test (priority = 13)
public void closeBrowser(){
driver.close();
}
}
----------------------------------------------------------
Prioritize Test Cases:

Using priority Attribute we can control the Test Execution
Or
Using DependsOnMethods Attribute we can control the Test Execution

Syntax:

@Test (priority = number)
@Test (DependsOnmethods = "BeforeMethod")

Using priority Attribute:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class TestCase2 {
public static WebDriver driver;
@Test (priority = 1)
public void launchBrowser(){
driver = new FirefoxDriver();
}
@Test (priority = 2)
public void verifygoogleTitle(){
driver.get("https://www.google.com");
String pageTitle = driver.getTitle();
Assert.assertEquals("Googlea", pageTitle);
}
@Test (priority = 3)
public void verifyyahooTitle(){
driver.get("https://in.yahoo.com/");
String pageTitle = driver.getTitle();
Assert.assertEquals("Yahoo", pageTitle);
}
@Test (priority = 4)
public void closeBrowser(){
driver.close();
}
}
---------------------------------------------
Using DependsonMethods Attribute:

public class TestCase2 {
public static WebDriver driver;
@Test 
public void launchBrowser(){
driver = new FirefoxDriver();
}
@Test (dependsOnMethods = "launchBrowser")
public void verifygoogleTitle(){
driver.get("https://www.google.com");
String pageTitle = driver.getTitle();
Assert.assertEquals("Google", pageTitle);
}
@Test (dependsOnMethods = "verifygoogleTitle")
public void verifyyahooTitle(){
driver.get("https://in.yahoo.com/");
String pageTitle = driver.getTitle();
Assert.assertEquals("Yahoo", pageTitle);
}

@Test (dependsOnMethods = "verifyyahooTitle")
public void closeBrowser(){
driver.close();
}
}
-------------------------------------------------------
public class TestCase2 {
public static WebDriver driver;
@Test 
public void launchBrowser(){
driver = new FirefoxDriver();
}
@Test (dependsOnMethods = "verifyyahooTitle")
public void verifygoogleTitle(){
driver.get("https://www.google.com");
String pageTitle = driver.getTitle();
Assert.assertEquals("Google", pageTitle);
}
@Test (dependsOnMethods = "launchBrowser")
public void verifyyahooTitle(){
driver.get("https://in.yahoo.com/");
String pageTitle = driver.getTitle();
Assert.assertEquals("Yahoo", pageTitle);
}

@Test (dependsOnMethods = "verifygoogleTitle")
public void closeBrowser(){
driver.close();
}
}
-----------------------------------------------------
public class TestCase2 {
public static WebDriver driver;
@Test 
public void launchBrowser(){
driver = new FirefoxDriver();
}
@Test (dependsOnMethods = "verifyyahooTitle")
public void verifygoogleTitle(){
driver.get("https://www.google.com");
String pageTitle = driver.getTitle();
Assert.assertEquals("Google", pageTitle);
}
@Test (dependsOnMethods = "launchBrowser")
public void verifyyahooTitle(){
driver.get("https://in.yahoo.com/");
String pageTitle = driver.getTitle();
Assert.assertEquals("Yahooa", pageTitle);
}

@Test (dependsOnMethods = "verifygoogleTitle")
public void closeBrowser(){
driver.close();
}
}
-------------------------------------------------------------------
Note:

If we use "priority" attribute then all Test Classes can be executed.

If we use "DependsOnMethods" attribute, then it will skip the test case execution whenever DependsOnMethods Test case
fails, if you want to execute the test case forcibly then us "alwaysRun attribute.


Scenario 1: If No functionality dependency among Test Cases in a TestNG program  - Use "priority" Attribute.
Scenario 2: If functionality dependency among Test Cases in a TestNG program - Use "DependsOnMethods" Attribute
Scenario 3: Functionality Dependency for Some Test Cases only in a TestNG program "DependsOnMethods" and "alwaysRun" Attributes.
---------------------------------------------------
public class TestCase2 {
public static WebDriver driver;
@Test 
public void launchBrowser(){
driver = new FirefoxDriver();
}
@Test (dependsOnMethods = "verifyyahooTitle", alwaysRun = true)
public void verifygoogleTitle(){
driver.get("https://www.google.com");
String pageTitle = driver.getTitle();
Assert.assertEquals("Googlea", pageTitle);
}
@Test (dependsOnMethods = "launchBrowser")
public void verifyyahooTitle(){
driver.get("https://in.yahoo.com/");
String pageTitle = driver.getTitle();
Assert.assertEquals("Yahooa", pageTitle);
}
@Test (dependsOnMethods = "verifygoogleTitle", alwaysRun=true)
public void closeBrowser(){
driver.close();
}
}
-------------------------------------------------------
Sample TestNG Program:

Test Execution Flow:

i) launchBrowser
ii) verifygoogleTitle
iii) verifyyahooTitle
iv) closeBrowser
------------------------------------------------
TestNG Annotations:

@Test: The annotated method is part of a Test case

@BeforeMethod: The annotated method will be run before each Test method
@AfterMethod: The annotated method will be run after each Test Method

@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 annotated method will be run before any Test Method beloging to classes inside the tage is run
@AfterTest: The annotated method will be run after all the Test Methods beloging to classes inside the tag.
--------------------------------------------------------------------------------
TestNG Program with "priority"

public class TestCase2 {
public static WebDriver driver;
@Test (priority =1)
public void launchBrowser(){
driver = new FirefoxDriver();
}
@Test (priority =2)
public void verifygoogleTitle(){
driver.get("https://www.google.com");
String pageTitle = driver.getTitle();
Assert.assertEquals("Google", pageTitle);
}
@Test (priority =3)
public void verifyyahooTitle(){
driver.get("https://in.yahoo.com/");
String pageTitle = driver.getTitle();
Assert.assertEquals("Yahoo", pageTitle);
}
@Test (priority =4)
public void verifybankofindiaTitle(){
driver.get("http://www.bankofindia.co.in/english/home.aspx");
String pageTitle = driver.getTitle();
Assert.assertEquals("Bank Of India - Home", pageTitle);
}
@Test (priority =5)
public void closeBrowser(){
driver.close();
}
}
--------------------------------------------------------
Test Execution Flow:

launchBrowser
verifygoogleTitle
verifyyahooTitle
verifybankofindiaTitle
closeBrowser
------------------------------------------------------------------------
TestNG Program with BeforeMethod and AfterMethod Annotations and priority

public class TestCase2 {
public static WebDriver driver;
@BeforeMethod
public void launchBrowser(){
driver = new FirefoxDriver();
}
@Test (priority =1)
public void verifygoogleTitle(){
driver.get("https://www.google.com");
String pageTitle = driver.getTitle();
Assert.assertEquals("Google", pageTitle);
}
@Test (priority =2)
public void verifyyahooTitle(){
driver.get("https://in.yahoo.com/");
String pageTitle = driver.getTitle();
Assert.assertEquals("Yahoo", pageTitle);
}
@Test (priority =3)
public void verifybankofindiaTitle(){
driver.get("http://www.bankofindia.co.in/english/home.aspx");
String pageTitle = driver.getTitle();
Assert.assertEquals("Bank Of India - Home", pageTitle);
}
@AfterMethod
public void closeBrowser(){
driver.close();
}
}
---------------------------------------------------------------
Text Execution Flow:

launchBrowser
verifygoogleTitle
closeBrowser

launchBrowser
verifyyahooTitle
closeBrowser

launchBrowser
verifybankofindiaTitle
closeBrowser
--------------------------------------
TestNG Program with BeforeClass and AfterClass Annotations and priority

Text Execution Flow:

launchBrowser
verifygoogleTitle
verifyyahooTitle
verifybankofindiaTitle
closeBrowser
--------------------------------------
public class TestCase2 {
public static WebDriver driver;
@BeforeClass
public void launchBrowser(){
driver = new FirefoxDriver();
}
@Test (priority =1)
public void verifygoogleTitle(){
driver.get("https://www.google.com");
String pageTitle = driver.getTitle();
if (pageTitle.equals("Google")){
System.out.println("passed");
}
}
@Test (priority =2)
public void verifyyahooTitle(){
driver.get("https://in.yahoo.com/");
String pageTitle = driver.getTitle();
if (pageTitle.equals("Yahoo")){
System.out.println("passed");
}
}
@Test (priority =3)
public void verifybankofindiaTitle(){
driver.get("http://www.bankofindia.co.in/english/home.aspx");
String pageTitle = driver.getTitle();
if (pageTitle.equals("Bank Of India - Home")){
System.out.println("passed");
}
}
@AfterClass
public void closeBrowser(){
driver.close();
}
}
------------------------------------------------------------
4) Executing multiple Programs / Classes using XML

XML for executing multiple Programs / Classes,

<suite name = "Suite Name">
<test name = "Test Name">
<classes>
<class name = "package.Class1Name"/>
<class name = "package.Class2Name"/>
</classes>
--------------------------------------------------
Navigation to create XML file in Eclipse / TestNG,
In Eclipse IDE,
Select Java project / package > Right Click > New > Others...
> Enter "TestNG" > Select "TestNG Class"
> Enter source and package names
> Enter XML File name > Finish
-------------------------------------------------
Test Execution Flow:

login
addVendor
addProduct
addCurrency
logout
login
deleteVendor
deleteProduct
deleteCurrency
logout

Class 1:
@BeforeClass
public void login(){
System.out.println("Login Successful");
}
@AfterClass
public void logout(){
System.out.println("Logout Successful");

@Test (priority =1)
public void addVendor(){
System.out.println("Add Vendor Successful");
}
@Test (priority =2)
public void addProduct(){
System.out.println("Add Product Successful");
}
@Test (priority =3)
public void addCurrency(){
System.out.println("Add Currency Successful");
}
}
--------------------------------------------
Class2:
public class Class2 {
@BeforeClass
public void login(){
System.out.println("Login Successful");
}
@AfterClass
public void logout(){
System.out.println("Logout Successful");

@Test (priority =1)
public void deleteVendor(){
System.out.println("Delete Vendor Successful");
}
@Test (priority =2)
public void deleteProduct(){
System.out.println("Delete Product Successful");
}
@Test (priority =3)
public void deleteCurrency(){
System.out.println("delete Currency Successful");
}
}
---------------------------------------------
Test execution Flow:

login
addVendor
addProduct
addCurrency
deleteVendor
deleteProduct
deleteCurrency
logout
---------------------------------------------
Class1: 

public class Class1 {
@BeforeTest
public void login(){
System.out.println("Login Successful");
}
@AfterTest
public void logout(){
System.out.println("Logout Successful");

@Test (priority =1)
public void addVendor(){
System.out.println("Add Vendor Successful");
}
@Test (priority =2)
public void addProduct(){
System.out.println("Add Product Successful");
}
@Test (priority =3)
public void addCurrency(){
System.out.println("Add Currency Successful");
}
}
---------------------------------------------------
public class Class2 {
@Test (priority =1)
public void deleteVendor(){
System.out.println("Delete Vendor Successful");
}
@Test (priority =2)
public void deleteProduct(){
System.out.println("Delete Product Successful");
}
@Test (priority =3)
public void deleteCurrency(){
System.out.println("delete Currency Successful");
}
}
---------------------------------------------------------------
5) Grouping Test Cases

Test Execution Levels in Manual Testing,
Smoke Testing / BVT / BAT...
Comprehensive Testing
Sanity Testing / BVT /BAT...
General Regression Testing Cycle1, 2, 3...
Final Regression Testing
--------------------------------------
XML File for grouping Classes,

<suite name ="Suite Name">
< test name = "Test Name">
<groups>
<run>
<include name = "group name"/>
</run>

<classes>
<class name = "package.ClassName"/> 
<classes/> 
</groups>
---------------------------------------------------------------
Tags in 1st File

suite,
test,
classes,
class

Tags in 2nd File

suite,
test,
groups,
run,
include,
classes,
class
--------------------------------------------------------------------------
Class File:

@Test (groups = {"sanity", "regression"}, priority =1)
public void login(){
System.out.println("Login Successful");
}
@Test (groups = {"sanity", "regression"}, priority =6)
public void logout(){
System.out.println("Logout Successful");
}
@Test (groups = {"sanity"}, priority = 2)
public void search(){
System.out.println("Search is Successful");
}
@Test (groups = {"regression"}, priority = 3)
public void advancedSearch(){
System.out.println("Advanced Search is Successful");
}
@Test (groups = {"regression"}, priority = 4)
public void billPayment(){
System.out.println("BillPayment is Successful");
}
@Test (groups = {"sanity", "regression"}, priority =5 )
public void prepaidRecharge(){
System.out.println("Prepaid Recharge is Successful");
}
}
-----------------------------------------------------------------
XML File:
<suite name="Suite" >
  <test name="Test">
  <groups>
  <run>
  <include name = "sanity"/>
  </run>
  </groups>
    <classes>
      <class name="abcd.Class3"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->
------------------------------------------------------
In this Grouping XML,

1) Sanity Group,

login
search
prepaidRecharge
logout

2) Regression Group,

login
advancedSearch
prepaidRecharge
billPayment
logout
---------------------------------------------------------------
6) Parallel Test Execution

Parallel Testing using Selenium Grid

we use multiple (2 or 3) computers to execute Tests,

Parallel Testing using TestNG Testing Framework,

we use single Computer to execute Tests,
--------------------------
Thread - A Theard is a cuncurrent unit of execution,

1) Parallel Test execution (Methods)
XML file for parallel Testing

<suite name = "SuiteName" parallel = "methods" thread-count="2">
<test name = "TestName">
<classes>
<class name = "package.ClassName"/>
<classes/>
</test>
</suite>
-------------------------------------------------------------
XML File:

<suite name="Suite" parallel="methods" thread-count="2">
  <test name="Test">
    <classes>
      <class name="abcd.Class1"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->
--------------------------------------------
Class File:

public class Class1 {
@Test
public void testCase1(){
long id = Thread.currentThread().getId();
System.out.println("Test Case 1 Successful "+ "Thread Count is " + id);
}
@Test
public void testCase2(){
long id = Thread.currentThread().getId();
System.out.println("Test Case 2 Successful "+ "Thread Count is " + id);
}
@Test
public void testCase3(){
long id = Thread.currentThread().getId();
System.out.println("Test Case 3 Successful "+ "Thread Count is " + id);
}
@Test
public void testCase4(){
long id = Thread.currentThread().getId();
System.out.println("Test Case 4 Successful "+ "Thread Count is " + id);
}
}
-------------------------------------------------------------------
2) Parallel Test execution (Classes)
<suite name="Suite" parallel="classes" thread-count="2">
  <test name="Test">
    <classes>
      <class name="abcd.Class1"/>
      <class name="abcd.Class2"/>
      <class name="abcd.Class3"/>
      <class name="abcd.Class4"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->
-------------------------------------

import org.testng.annotations.Test;

public class Class1 {
@Test
public void testCase1(){
long id = Thread.currentThread().getId();
System.out.println("Test Case 1 Successful "+ "Thread Count is " + id);
}
@Test
public void testCase2(){
long id = Thread.currentThread().getId();
System.out.println("Test Case 2 Successful "+ "Thread Count is " + id);
}
@Test
public void testCase3(){
long id = Thread.currentThread().getId();
System.out.println("Test Case 3 Successful "+ "Thread Count is " + id);
}
@Test
public void testCase4(){
long id = Thread.currentThread().getId();
System.out.println("Test Case 4 Successful "+ "Thread Count is " + id);
}
}
-------------------------------------------
import org.testng.annotations.Test;

public class Class2 {
@Test
public void testCase5(){
long id = Thread.currentThread().getId();
System.out.println("Test Case 5 Successful "+ "Thread Count is " + id);
}
@Test
public void testCase6(){
long id = Thread.currentThread().getId();
System.out.println("Test Case 6 Successful "+ "Thread Count is " + id);
}
@Test
public void testCase7(){
long id = Thread.currentThread().getId();
System.out.println("Test Case 7 Successful "+ "Thread Count is " + id);
}
@Test
public void testCase8(){
long id = Thread.currentThread().getId();
System.out.println("Test Case 8 Successful "+ "Thread Count is " + id);
}
}
-------------------------------------------------------------
Parallel Testing methods option,

Class
method1
method2
method3
method4
Thread-count -2/4

Parallel Testing classes option,

Class1
method1
method2
method3
method4

Class2
method1
method2
method3
method4

Thread-count -2/4
--------------------------------------------------
7) Data Driven Testing using DataProvider 

i) Read Test Data (String Type) from Excel file and perform Data driven testing for Admin Login Functionality.
ii) Read Test Data (Integer Type) from Excel file and conduct Data driven Testing for Addition functionality
------------------------------------------------------------------
i) Read Test Data (String Type) from Excel file and perform Data driven testing for Admin Login Functionality.

Manual Test Case:

Test Steps:
i) Launch the browser
ii) Navigate to gcrShop Admin Interface (http://gcrit.com/build3/admin/)
iii) Enter "Username"
iv) Enter "Password"
v) Click "Login" Button

Verification point:
Capture the URL, and compare with Expected (Compare for multiple sets of Test Data)

Test Data:
Username: admin
Password: admin@123

Expected: http://www.gcrit.com/build3/admin/index.php
-----------------------------------------------
Prepare Test Data File:

> Download Excel jar and extract
> Add Excel jar to Java project in Eclipse IDE
-----------------------------------------------
TestNG Program:

public class DataDriven {
public WebDriver driver;
@Test (dataProvider = "testData")
public void adminLogin (String uname, String pwd){
driver = new FirefoxDriver();
driver.get("http://gcrit.com/build3/admin/");
driver.findElement(By.name("username")).sendKeys(uname);
driver.findElement(By.name("password")).sendKeys(pwd);
driver.findElement(By.id("tdb1")).click();
String s = driver.getCurrentUrl();
System.out.println(s);
if (s.contains("http://www.gcrit.com/build3/admin/index.php")){
System.out.println("Login Successful");
}
else
{
System.out.println("Login Unuccessful");
}
}
@AfterMethod
public void closeBrowser(){
driver.close();
}
@DataProvider (name = "testData")
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("Sheet1");
int columns = s.getColumns();
int rows = s.getRows();
//System.out.println(columns +", " + rows);

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 Test Data (Integer Type) from Excel file and conduct Data driven Testing for Addition functionality

public class Addition {
@Test (dataProvider = "ahalya")
public void addition (String num1, String num2, String num3){
int a = Integer.parseInt(num1);
int b = Integer.parseInt(num2);
int c = Integer.parseInt(num3);
int result = a + b + c;
System.out.println(result);
}
@DataProvider(name = "ahalya")
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(0);

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

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();
}
}
return inputData;
}
}
-----------------------------------------------
Note 1: Irrespective of fields size (number of fields) we can use 2D Array (we are handling Excel File, excel has 2D Data only)
Note 2: Irrespective of rows and columns size (number of Rows and Columns) we can use nested loop and logic...
-----------------------------------------------
TestNG Tutorial

0 comments:

Post a Comment