TestNG Framework for Selenium

TestNG Framework for Selenium
 

TestNG Framework
1) TestNG Introduction

2) Benefits of TestNG

3) TestNG Installation

4) TestNG Test Case example

5) Create multiple Test Cases.
-----------------------------------
6) Execute multiple programs/Classes using XML file.
 

Create XML File
Select Java Project and Right Click
> New > Other
> Enter XML and select XML file
> Enter File Name
> Finish
--------------------------
Tags

suite tag

test tag

classes tag

class tag (multiple)
-------------------
XML File:
<suite name = "Ecommerce suite">
<test name = "Sanity Tests">
<classes>
<class name = "seleniumTests.Class1"/>
<class name = "seleniumTests.Class2"/>
</classes>

</test>
</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 search(){
    System.out.println("Search Successful");
    }
    @Test (priority = 2)
    public void advancedSearch(){
    System.out.println("Advanced Search Successful");
    }
    @Test (priority = 3)
    public void buyProducts(){
    System.out.println("Buying Products 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 accountSummary(){
    System.out.println("Account Summary Successful");
    }
    @Test (priority = 2)
    public void fundTransfer(){
    System.out.println("Fund Transfer Successful");
    }
    @Test (priority = 3)
    public void billPayment(){
    System.out.println("Bill Payment Successful");
    }
    }
-----------------------------------------
TestNG Annotations
 

@Test -Makes Method as Test Case

@BeforeMethod - Pre-condition for every Test Case in a Class
@AfterMethod -Post-condition for every Test Case in a Class

@BeforeClass -Pre-condition for all Test Cases in a Class
@AfterClass -Post-condition for all Test Cases in a Class

@BeforeTest - Pre-condition for all Test Cases in multiple Class
@AfterTest - Post-condition for all Test Cases in multiple Class
--------------------------------------
7) Parallel Test execution
 

> Parallel Test execution using TestNG (using single machine/Computer)

> Parallel Test execution using Selenium Grid (using multiple machines/Computer)
-------------------------------------------------
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 search(){
    System.out.println("Search Successful");
    }
    @Test (priority = 2)
    public void advancedSearch(){
    System.out.println("Advanced Search Successful");
    }
    @Test (priority = 3)
    public void buyProducts(){
    System.out.println("Buying Products Successful");
    }
    @Test (priority = 4)
    public void testCase(){
    System.out.println("Test Case in "+getClass().getSimpleName()
    + " With Thread id: " + Thread.currentThread().getId());
    }
    }
-----------------------------------------

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 accountSummary(){
    System.out.println("Account Summary Successful");
    }
    @Test (priority = 2)
    public void fundTransfer(){
    System.out.println("Fund Transfer Successful");
    }
    @Test (priority = 3)
    public void billPayment(){
    System.out.println("Bill Payment Successful");
    }
    @Test (priority = 4)
    public void testCase(){
    System.out.println("Test Case in "+getClass().getSimpleName()
    + " With Thread id: " + Thread.currentThread().getId());
    }
}
---------------------------------------
XML File
<suite name = "Parallel Test suite" parallel = "classes" thread-count = "2">
<test name = "Sanity Test">

<classes>
<class name = "seleniumTests.Class1"/>
<class name = "seleniumTests.Class2"/>
</classes>

</test>
</suite>
--------------------------------------------------------
parallel = "methods": TestNG will run all the methods in separate threads.
parallel = "classes" : TestNG will run all the methods in the same class in the same thread.
parallel = "tests" : TestNG will run all the methods in the same <test> tag in the same thread.
---------------------------------------
Class 1
3 Methods thread(1) 1
Class 2
3 methods thread(1) 2
----------------------
Class 1
3 Methods thread(3) 1
Class 2
3 methods thread(3) 2
-----------------------------
8) Grouping Test Cases
 

Class File
@BeforeTest (groups ={"sanity","regression"})
    public void login(){
    System.out.println("Login Successful");
    }
    @AfterTest (groups ={"sanity","regression"})
    public void logout(){
    System.out.println("Logout Successful");
    }
   
    @Test (groups ={"sanity"})
    public void search(){
    System.out.println("Search Successful");
    }
    @Test (groups ={"sanity","regression"})
    public void advancedSearch(){
    System.out.println("Advanced Search Successful");
    }
    @Test (groups ={"sanity", "regression"})
    public void buyProducts(){
    System.out.println("Buying Products Successful");
    }
    @Test (groups ={"regression"})
    public void abcd(){
    System.out.println("Abcd Successful");
    }
    @Test (groups ={"regression"})
    public void xyza(){
    System.out.println("Xyza Successful");
    }
    @Test (groups ={"regression"})
    public void asdf(){
    System.out.println("Asdf Successful");
    }
}
-----------------------------
XML File
<suite name ="suite">
<test name ="test">

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

<classes>
<class name = "seleniumTests.GroupTests"/>
<class name = "seleniumTests.GroupTests2"/>
</classes>

</test>
</suite>
----------------------------------------------------------------
9) Data driven testing using DataProvider Annotation
 

If you want work with excel, then download third party jar files

Ex: jxl

public class DataDriven {
    @Test (dataProvider="testdata")
    public void Addition(String val1, String val2, String val3){
        int a = Integer.parseInt(val1);
        int b = Integer.parseInt(val2);
        int c = Integer.parseInt(val3);
        int result = a + b + c;
        System.out.println(result);
    }
   
@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(0);
        int rows = s.getRows();
        int columns = s.getColumns();
        //System.out.println(rows);
        //System.out.println(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;
        }
}
----------------------------------------------------------
Assignment
--------------
Read Test Data (5 sets) from external Excel file and perform Data Driven Testing for Gmail Login Functionality.

Steps:

i) Prepare Test Data file (with 5 sets of email and Passwords)

ii) using DataProvider Annotation read the data.

iii) Use the data in TestNg test cases.
-------------------------------------------------
Selenium Project Work
 

Java Programming + Selenium WebDriver + TestNG Framework

Domain: Ecommerce

Sub-Domain: B2C

Application: Online Shopping

Development Environment: LAMP (Linux, Apache, MySQL and PHP)

Testing Environment: Intranet

Production Environment: Internet
------------------------------
Two interfaces of web Application

i) Admin Interface

ii) User Interface
---------------------------

Admin Interface URL: http://www.gcrit.com/build3/admin/

Username: admin

Password: admin@123

User Interface URL: http://www.gcrit.com/build3/

build3 -folder

http://telugu.greatandhra.com/

telugu -sub domain
------------------------------------------
Features to be tested in User Interface

i) create an account

ii) Login

iii) Buy Product
------------------------------
Features to be tested in Admin Interface

i) Login

ii) Create /Edit/Delete Products

iii) Insert / edit/delete vendors

iv) Create /Edit/Delete currencies
-------------------------------------------
Test Cases
Create an Account in User Interface

Steps:

i) Launch the Browser
ii) Navigate to http://www.gcrit.com/build3/
iii) click "create an account" link
iv) Enter all mandatory fields
v) Click "continue"
-------------
Verification Point:
Capture conformation message and compare with expected.
-----------------------------------------
Login in User Interface

Steps:

i) Launch the Browser
ii) Navigate to http://www.gcrit.com/build3/
iii) Click "login" link
iv) Enter "Email Address"
v) Enter "Password"
vi) Click "Sign in"

Verification point:

Capture Welcome message and find sub string first name
--------------------------------------------
Login in Admin Interface
i) Launch the Browser
ii) Navigate to http://www.gcrit.com/build3/admin/
iii) Enter "Username"
iv) Enter "Password"
v) Click "Login" button
------------------
Verification point:

Check the existence of "logoff" link
----------------------------------------------

1 comments:

Post a Comment