TestNG Framework Quick Tutorial for Selenium

TestNG Framework Quick Tutorial for Selenium

i) Introduction to TestNG Framework

ii) Install TestNG and write first TestNG Program

iii) Create multiple Test cases and prioritize Test Cases.

iv) Execute multiple Classes / Programs using XML

v) Grouping Test Cases
--------------------------------------------
i) Introduction to TestNG Framework

> In Selenium using Java there are two Frameworks available,

1) JUnit

2) TestNG

> TestNG is a Testing Framework designed to simplify a broad range of Testing activities 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 inspired from JUnit (Java platform and NUnit (.NET platform), but introduced some new functionality.
--------------------------------------------
Advantages of TestNG

> TestNG Annotations are easy to create Test Cases

> Test Cases can be grouped and prioritized more easily.

> Supports Parameterization

> Execute Test Batches

> Parallel Test Execution

> Generate HTML Test Reports.
--------------------------------------------
ii) Install TestNG and write first TestNG Program

In Eclipse IDE,

> Help Menu
> Install New Software
> Click Add
> Enter name as "TestNG"
> Enter URL as : "http://beust.com/eclipse/"
> Select "TestNG"
> Next > Next > accept License Agreement > finish
--------------------------------------------
Write first TestNG Test Case

Manual Test Case: Verify the Title of Gmail Home Page

Test Steps:
i) Launch the Browser
ii) Navigate to "https://www.gmail.com"

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

Expected: Gmail

Input Data: None
--------------------------------------------
public class VerifyTitle {
@Test
public void verifyTitle(){
WebDriver driver = new FirefoxDriver();
driver.get("https://www.gmail.com");

String pageTitle = driver.getTitle();
Assert.assertEquals (pageTitle, "Gmail");
driver.close();
}
}
--------------------------------------------
Note:

1) main method is not used for TestNG programs.
2) TestNG Program contains only methods that contain @Test Annotations
3) If we don't write @Test Annotation then the method is not going to be executed.
--------------------------------------------
iii) Create multiple Test cases and prioritize Test Cases.

public class VerifyTitle {
@Test
public void logout(){
Assert.assertEquals("Gmail", "Gmail");
}
@Test
public void abcd(){
Assert.assertEquals("abcd", "abcd");
}
@Test
public void xyz(){
Assert.assertEquals("Yahoo", "Yahoo");
}
@Test
public void login(){
Assert.assertEquals("Google", "Google");
}
}
---------------------------------------
Test Execution Flow as per the Program:

logout
abcd
xyz
login
--------------------------------
abcd
login
logout
xyz

Note: TestNG Test Cases are executed in Alphabetical Order.

If you want control the Test Execution then use priority attribute or dependsOnMethods attribute
---------------------------------------
Required Flow:
login
abcd
xyz
logout

Using priority attribute

public class VerifyTitle {
@Test(priority = 4)
public void logout(){
Assert.assertEquals("Gmail", "Gmail");
}
@Test(priority = 2)
public void abcd(){
Assert.assertEquals("abcd", "abcd");
}
@Test(priority = 3)
public void xyz(){
Assert.assertEquals("Yahoo", "Yahoo");
}
@Test(priority = 1)
public void login(){
Assert.assertEquals("Google", "Google");
}
}
--------------------------------------------
Using dependsOnMethods attribute

import org.testng.Assert;
import org.testng.annotations.Test;

public class VerifyTitle {
@Test(dependsOnMethods = {"xyz"})
public void logout(){
Assert.assertEquals("Gmail", "Gmail");
}
@Test(dependsOnMethods = {"login"})
public void abcd(){
Assert.assertEquals("abcd", "abcd");
}
@Test(dependsOnMethods = {"abcd"})
public void xyz(){
Assert.assertEquals("Yahoo", "Yahoo");
}
@Test
public void login(){
Assert.assertEquals("Google", "Google");
}
}
--------------------------------------------
login -Pre condition for every Test case
logout -Post Condition for Evey Test Case

Required Flow:

login
abcd
logout

login
xyz
logout
--------------------------------------------
public class VerifyTitle {
@AfterMethod
public void logout(){
System.out.println("Logout Successful");
}
@Test
public void abcd(){
System.out.println("abcd Successful");
}
@Test
public void xyz(){
    System.out.println("xyz Successful");
}
@BeforeMethod
public void login(){
System.out.println("Login Successful");
}
}
-------------------------------------------
login -Pre condition for all Test cases in the Program
logout -Post Condition for all Test cases in the Program

Required Flow:

login
xyz
abcd
logout
-----------------------------------------
public class VerifyTitle {
@AfterClass
public void logout(){
System.out.println("Logout Successful");
}
@Test(priority = 2)
public void abcd(){
System.out.println("abcd Successful");
}
@Test(priority = 1)
public void xyz(){
    System.out.println("xyz Successful");
}
@BeforeClass
public void login(){
System.out.println("Login Successful");
}
}
--------------------------------------------
iv) Execute multiple Classes / Programs using XML
XML file for executing multiple Classes/Programs.

<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">

Tags

suite
test
classes
class
-------------------------------
Navigation to create XML file

Select Java Package > Right Click > New > Others...
> Enter "TestNG" and Select TestNg Class
> Enter source and Package names
> Enter XML file name
> Finish
--------------------------------------------
Test Cases:

login
addVendor
addCurrency
addProduct
logout
-------------------------------------
XML File

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Ecommerce" parallel="false">
  <test name="Smoke Tests">
    <classes>
      <class name="abcd.Class1"/>
      <class name="abcd.Class2"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->
---------------------------------------
Class 1

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 = 3)
public void addProduct() {
System.out.println("Add product Successful");
}
@Test(priority = 2)
public void addCurrency() {
System.out.println("Add Currency Successful");
}
}
------------------------------------------------
Class 2:

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 = 3)
public void deleteProduct() {
System.out.println("Delete Product Successful");
}
@Test(priority = 2)
public void deleteCurrency() {
System.out.println("Delete Currency Successful");
}
}
--------------------------------------------
v) Grouping Test Cases

XML file for grouping Test Cases

<suite name ="Suite Name">
<test name ="Test Name">

<groups>
<run>
<include name ="GroupName"/>
</run>
</groups>

<classes>
<class name = "Package.Class1"/>
<class name = "Package.Class1"/>
</classes>

</test name ="Test Name">
</suite name ="SuiteName">
--------------------------------------------
Tags

suite
test
groups
run
include
classes
class
--------------------------------------------
Sanity group

login
search
prepaidRecharge
logout

Regression group

login
advancedSearch
prepaidRecharge
billPayments
logout
-------------------------------------
XML File

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
  <test name="Test">
 
  <groups>
  <run>
  <include name ="Regression"/>
  </run>
 
  </groups>
    <classes>
      <class name="abcd.Class3"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->
-------------------------------------------
Class File

public class Class3 {
@Test(groups = {"Sanity", "Regression"}, priority=1)
public void login() {
System.out.println("Login Successful"); 
}
@Test(groups = {"Sanity", "Regression"}, priority=10)
public void logout() {
System.out.println("Logout Successful"); 
}
@Test(groups = {"Sanity"}, priority=2)
public void search() {
System.out.println("Search Successful"); 
}
@Test(groups = {"Regression"}, priority=2)
public void advancedSearch() {
System.out.println("Advanced Search Successful"); 
}

@Test (groups = {"Sanity", "Regression"}, priority=3)
public void prepaidRecharge() {
System.out.println("Prepaid Recharge Successful"); 
}
@Test (groups = {"Regression"}, priority=4)
public void billPayments() {
System.out.println("Bill Payments Successful"); 
}
}
--------------------------------------------
Selenium Webdriver Test Cases
1) Launch Browser
2) Verify Gmail Home Page
3) Verify Yahoo Home Page
4) Close Browser
-----------------------------------
TestNG Program:

public class Sample {
public WebDriver driver;

@BeforeClass
public void launchBrowser(){
driver = new FirefoxDriver();   
}
@AfterClass
public void closeBrowser(){
driver.close();
}
@Test(priority=1)
public void verifyGmailpage(){
driver.get("https://www.gmail.com");
Assert.assertEquals("Gmail", driver.getTitle());
}
@Test(priority=2)
public void verifyYahoopage(){
driver.get("https://in.yahoo.com");
Assert.assertEquals("Yahoo", driver.getTitle());
}
}
--------------------------------------------

0 comments:

Post a Comment