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 as well as write kickoff TestNG Test Case

III) Create multiple Test Cases as well as 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 inwards a TestNG Program

TestNG Program

public flat 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 as well as AfterMethod Annotations

TestNG Program

public flat 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 as well as 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 inwards XML

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


</classes>

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

Select Java Project/Package > Right Click > New > Other...
> Enter TestNG as well as Select TestNG Class
> Enter rootage as well as parcel names
> Enter XML File advert > 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 flat 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 as well as 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 flat 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 business office of a Test case

@BeforeMethod - The annotated method volition endure run earlier each Test method
@AfterMetod - The annotated method volition endure run afterwards each Test method

@BeforeClass - The annotated method volition endure run earlier the kickoff Test method inwards the electrical flow flat is invoked.
@AfterClass - The annotated method volition endure run afterwards all the Test methods inwards the electrical flow flat convey been run.

@BeforeTest -The annotated method volition endure run earlier whatsoever Test method belonging to classes within the tag is run.
@AfterTest -The annotated method volition endure run afterwards all the Test methods belonging to the classes within the tag.
----------------------------------------------------
V) Grouping Test Cases

XML File for grouping exam cases

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

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

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

suite
test
classes
class

2) XML file for executing exam groups

Tags

suite
test

groups
run
include

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

public flat 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 advert ="regression"/>
   </run>
 
  </groups>
    <classes>
      <class name="seleniumWebDriver.ClassNew"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->
--------------------------------------------------------------------
VI) Parallel Test Execution

Thread - Influenza A virus subtype H5N1 Thread is a concurrent unit of measurement 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 flat Class1 {
@Test
public void testCase1(){
long id = Thread.currentThread().getId();
System.out.println("Test Case i is Successful" + "Thread id is: "+ id);   
}
@Test
public void testCase2(){
long id = Thread.currentThread().getId();
System.out.println("Test Case two is Successful" + "Thread id is: "+ id);   
}
@Test
public void testCase3(){
long id = Thread.currentThread().getId();
System.out.println("Test Case three is Successful" + "Thread id is: "+ id);   
}
@Test
public void testCase4(){
long id = Thread.currentThread().getId();
System.out.println("Test Case four 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 i File

public flat Class1 {
@Test
public void testCase1(){
long id = Thread.currentThread().getId();
System.out.println("Test Case i is Successful" + "Thread id is: "+ id);   
}
@Test
public void testCase2(){
long id = Thread.currentThread().getId();
System.out.println("Test Case two is Successful" + "Thread id is: "+ id);   
}
@Test
public void testCase3(){
long id = Thread.currentThread().getId();
System.out.println("Test Case three is Successful" + "Thread id is: "+ id);   
}
@Test
public void testCase4(){
long id = Thread.currentThread().getId();
System.out.println("Test Case four is Successful" + "Thread id is: "+ id);   
}
}

Class two File

public flat Class2 {
@Test
public void testCase5(){
long id = Thread.currentThread().getId();
System.out.println("Test Case five is Successful" + "Thread id is: "+ id);   
}
@Test
public void testCase6(){
long id = Thread.currentThread().getId();
System.out.println("Test Case half-dozen is Successful" + "Thread id is: "+ id);   
}
@Test
public void testCase7(){
long id = Thread.currentThread().getId();
System.out.println("Test Case three 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

Sumber http://www.gcreddy.com/
Post a Comment (0)
Previous Post Next Post