TestNG Testing Framework for Selenium Part 2


TestNG Testing Framework for Selenium Part 2

TestNG Testing Framework for Part-1

I) Introduction to TestNG

II) Install TestNG and write first TestNG Test Case

III) Create multiple Test Cases and Execute
-----------------------------------------------
TestNG Testing Framework Part-2

IV) Executing multiple Programs / Classes using XML

V) Grouping Test Cases

VI) Parallel Test Execution
--------------------------------------------------------
IV) Executing multiple Programs / Classes using XML

i) Creating multiple Test Cases in a TestNG Program

TestNG Program

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

i) login
ii) addVendor
iii) addProduct
iv) addCurrency
v) logout
----------------------------
ii) Using BeforeMethod and AfterMethod Annotations

TestNG Program

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

Login Successful
Add Vendor Successful
Logout Successful

Login Successful
Add Product Successful
Logout Successful

Login Successful
Add Currency Successful
Logout Successful
----------------------------------------------
iii) Using BeforeClass and AfterClass Annotations

TestNG Program

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

Login Successful
Add Vendor Successful
Add Product Successful
Add Currency Successful
Logout Successful
-------------------------------------------------------------
XML file for executing multiple programs/classes
Tags in XML

<suite name = "Suite Name">
<test name ="Test Name">
<classes>
<class name ="package.Class1Name"/>
<class name ="package.Class2Name"/>
</class>
</classes>
</test>
</suite>
-----------------------------------
<classes>


</classes>

Or
<classes           />
--------------------------------------------
Navigation for creating XML file in Eclipse IDE

Select Java Project/Package > Right Click > New > Other...
> Enter TestNG and Select TestNG Class
> Enter source and package names
> Enter XML File name > Finish
--------------------------------
XML File

<suite name="Ecommerce" parallel="false">
  <test name="SanityTests">
    <classes>
      <class name="seleniumWebDriver.Abcd"/>
       <class name="seleniumWebDriver.Xyz"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->
--------------------------------------------
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=2)
public void addProduct(){
System.out.println("Add Product Successful");
}
@Test(priority=1)
public void addVendor(){
System.out.println("Add Vendor Successful");
}
@Test(priority=3)
public void addCurrency(){
System.out.println("Add Currency Successful");
}
}
-----------------------------------
Class 2

public class Xyz {
@BeforeClass
public void login(){
System.out.println("Login Successful");
}
@AfterClass
public void logout(){
System.out.println("Logout Successful");
}   
@Test(priority=2)
public void deleteProduct(){
System.out.println("Delete Product Successful");
}
@Test(priority=1)
public void deleteVendor(){
System.out.println("Delete Vendor Successful");
}
@Test(priority=3)
public void deleteCurrency(){
System.out.println("Delete Currency Successful");
}
}
--------------------------------------------------------
Using BeforeTest and AfterTest Annotations

Class 1
@BeforeTest
public void login(){
System.out.println("Login Successful");
}
@AfterTest
public void logout1(){
System.out.println("Logout Successful");
}   
@Test(priority=2)
public void addProduct(){
System.out.println("Add Product Successful");
}
@Test(priority=1)
public void addVendor(){
System.out.println("Add Vendor Successful");
}
@Test(priority=3)
public void addCurrency(){
System.out.println("Add Currency Successful");
}
}
-------------------------------------
Class 2

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

login
addVendor
addProduct
addCurrency
deleteVendor
deleteProduct
deleteCurrency
logout
----------------------------------------------
TestNG Annotations

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

@BeforeMethod - The annotated method will be run before each Test method
@AfterMetod - The annotated method will be run after each Test method

@BeforeClass - The annotated method will be run before 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 belonging to classes inside the tag is run.
@AfterTest -The annotated method will be run after all the Test methods belonging to the classes inside the tag.
----------------------------------------------------
V) Grouping Test Cases

XML File for grouping test cases

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

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

</test>
</suite>
-----------------------------------------------------------
1) XML file for executing multiple programs or classes
Tags

suite
test
classes
class

2) XML file for executing test groups

Tags

suite
test

groups
run
include

classes
class
----------------------------------------------------
Class File

public class ClassNew {
@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");
}
}
---------------------------------------------------
XML File

<suite name="Suite" parallel="false">
  <test name="Test">
  <groups>
  <run>
  <include name ="regression"/>
   </run>
 
  </groups>
    <classes>
      <class name="seleniumWebDriver.ClassNew"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->
--------------------------------------------------------------------
VI) Parallel Test Execution

Thread - A Thread is a concurrent unit of execution

1) Parallel Test Execution Methods

XML File

<suite name="Suite" parallel="methods" thread-count="2">
  <test name="Test">
    <classes>
      <class name="seleniumWebDriver.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 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);   
}
@Test
public void testCase4(){
long id = Thread.currentThread().getId();
System.out.println("Test Case 4 is Successful" + "Thread id is: "+ id);   
}
}
------------------------------------------------------
2) Parallel Test Execution Classes

XML File

<suite name="Suite" parallel="classes" thread-count="2">
  <test name="Test">
    <classes>
      <class name="seleniumWebDriver.Class1"/>
      <class name="seleniumWebDriver.Class2"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Class 1 File

public class Class1 {
@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);   
}
@Test
public void testCase4(){
long id = Thread.currentThread().getId();
System.out.println("Test Case 4 is Successful" + "Thread id is: "+ id);   
}
}

Class 2 File

public class Class2 {
@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);   
}
@Test
public void testCase7(){
long id = Thread.currentThread().getId();
System.out.println("Test Case 3 is Successful" + "Thread id is: "+ id);   
}
}
-------------------------------------------------------------
Parallel Test Execution using TestNG - Single Computer
Parallel Test Execution using Selenium Grid - Multiple Computers
-----------------------------------------------------------------------

TestNG Framework for Selenium Part-1 Link

0 comments:

Post a Comment