TestNG Tutorial


TestNG Tutorial for Selenium
 

TestNG Introduction and create Test cases using TestNG Annotations.

> In Selenium using Java there are two Automation frameworks available:

i) JUnit

ii) TestNG
---------------------------------
TestNG Introduction
> TestNG is more powerful than JUnit framework.

> TestNG is an open source test automation framework, where NG stands for Next Generation

> TestNG inspired from JUNit but introduced some new functionality.

Benefits of TestNG:-------------------
i) It generates HTML test reports

ii) TestNG Annotations are easy to create Test cases.

iii) Test cases can be grouped and prioritized more easily.

iv) Parallel Testing is possible

v) Parameterization is possible.
---------------------------------------
Using TestNG we can create Test cases, execute Test cases and generate reports using HTML format.
----------------------------------------------
Manual Test Case:
------------------
Test Case Id: TC_001

Test Case Name: Verify title of the Page

Test Steps:
---------------
i) Launch the Browser        WebDriver driver = new FirefoxDriver();   

ii) Navigate to gmail.com    driver.get("htttp:\\www.gmail.com");

iii) check the page title      String Actual = driver.getTitle();
                                       Assert.assertEquals(Actual, "Gmail");
Expected:

Actual:

Status:
-------------------------
@Test
public void verifyTitle()
{
WebDriver driver = new FirefoxDriver();
driver.get("htttp:\\www.gmail.com");
String Actual = driver.getTitle();
Assert.assertEquals(Actual, "Gmail");
}

Note: main method is not required for TestNG programs.

TestNG programs contain only methods that contain @Test Annotations.

If don't write @Test Annotation then the methods are not going to be executed.
--------------------------------------------------------------
Install TestNG:
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 the Agreement -> Finish
------------------------------------------
TestNG Test case example:
package seleniumTests;

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

public class SampleTest {
    @Test
    public void verifyTitle()
    {
    WebDriver driver = new FirefoxDriver();
    driver.get("http:/www.gmail.com");
    String Actual = driver.getTitle();
    Assert.assertEquals(Actual, "Gmail");
    }
}
-------------------------------------------------
Create Multiple Test cases
TestNG test cases can be executed in alphabetical order, if you want to control the
Test execution then use Attributes.

Ex 1:
public class SampleTest {
    @Test
    public void testA()
    {
    Assert.assertEquals("Google", "Google");
    }
    @Test
    public void testC()
    {
    Assert.assertEquals("Gmail", "Google");
    }
    @Test
    public void testB()
    {
    Assert.assertEquals("Yahoo", "Yahoo");
    }
}
----------------------------------
public class SampleTest {
    @Test (priority = 1, description ="Verify Login", enabled =false)
    public void Login()
    {
    System.out.println("Login Successful");
    }
    @Test (priority = 2)
    public void Search()
    {
    System.out.println("Search operation successful");
    }
    @Test (priority = 3)
    public void Logout()
    {
    System.out.println("Logout Successful");
    }
}
------------------------------------
In TestNG Test we use Java programming features, Webdriver methods and TestNG Annotations.
------------------------------
@Test -TestNG Annotation

@Test (priority=1) - Test method execution priority

@Test (description="**************") - description of the Test case

@Test (enabled =false) - Ignores the Test case

@Test(dependsOnMethods={"method name"})
---------------------------------------------
priority Versus dependsOnMethods

Test cases 10
----------------------------------------------
public class SampleTest {
    @Test
    public void Login() // This is a Test
    {
    System.out.println("Login Successful");
    }
    @Test (dependsOnMethods ={"Login"})
    public void Search()
    {
    Assert.assertEquals("abc", "xyz");
    }
    @Test (dependsOnMethods ={"Search"}, alwaysRun=true)
    public void Logout()
    {
    System.out.println("Logout Successful");
    }
}
------------------------------------------------
Hard dependency:

@Test (dependsOnMethods={"Search"})
---------------------------------
Soft dependency:

@Test (dependsOnMethods={"Search"}, alwaysRun=true)
-----------------------------------------------------------
TestNG Annotations
@Test

Login -precondition for every test case
Logout -Post condition for every test case
----------------
Search
Advanced Search
Buy products
--------------------
Execution Flow:

Login
Search
Logout
Login
Advanced Search
Logout
Login
Buy products
Logout
----------------------------------
public class Sample2 {
    @BeforeMethod
    public void Login()
    {
    System.out.println("Login 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("Buy Products Successful");
    }
    @AfterMethod
    public void Logout()
    {
    System.out.println("Logout Successful");
    }
}
-------------------------------------
Login -precondition for every test case
Logout -Post condition for every test case
----------------
Search
Advanced Search
Buy products
------------------
Execution Flow:

Login
Search
AdvancedSearch
BuyProducts
Logout
---------------------
@BeforeClass
@AfterClass
------------------------
Example:

public class Sample2 {
    @BeforeClass
    public void Login()
    {
    System.out.println("Login 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("Buy Products Successful");
    }
    @AfterClass
    public void Logout()
    {
    System.out.println("Logout Successful");
    }
}
------------------------------------------------
Executing one or more programs using XML file
<suite name = "EcomerceSuite">
    <test name ="UserTest">
        <classes>
        <class name = "package.Class1Name"/>
        <class name = "package.Class2Name"/>
        </classes>
    </test>
</suite>
----------------------------------------------------------
suite tag
test tag
classes tag
class tag
---------------------------------
Create XML file in Eclipse
Select Java Project -> Right click -> New -> Other
-> Enter testng and Select TestNG Class
-> Enter source and package names
-> Enter xml file name as "testng.xml"
------------------------------------------
Executing multiple classes using XML file.
@BeforeMethod - executes before every test case in a class
@AfterMethod -executes after every test case in class
---------------
@BeforeClass -executes before all test cases in a class
@AfterClass - executes after all test cases in a class
--------------------------------
@BeforeTest - executes before all test cases from all classes in XML file
@AfterTest -executes after all test cases from all classes in XML file
------------------------------------------------
XML file for executing multiple classes:

 -----------------------------------------
<suite name="Suite" >
  <test name="Test">
    <classes>
      <class name="seleniumTests.Abcd1"/>
      <class name="seleniumTests.Abcd2"/>
     
    </classes>
  </test>
</suite>
-----------------------
Class 1-----------------------------------------------
public class Abcd1 {
  @BeforeTest
  public void Login(){
  System.out.println("Login Successful");
  }
  @AfterTest
  public void Logout(){
  System.out.println("Logout Successful");
  }
  @Test (priority = 1)
  public void FundTransfer (){
    System.out.println("Fund Transfer Successful");
  }
  @Test (priority=3)
  public void PrepaidRecharge(){
  System.out.println("Prepaid Recharge Successful");
  }
  @Test (priority=2)
  public void BillPayments(){
  System.out.println("Bill Payments Successful");
  }
}
-----------------------------------------------
Class 2----------------------------------------------
public class Abcd2 {
    /*@BeforeMethod
    public void Login() {
        System.out.println("Login Successful");
    }
    @AfterMethod
    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");
    }
}
-----------------------------------------
Grouping Test Cases
XML file
<suite name="Suite">
 <test name="Test">
 <groups>
 <run>
 <include name ="regression"/>
 </run>
 </groups>
    <classes>
      <class name="seleniumTests.GroupingTests"/>
    </classes>
  </test>
</suite>
----------------------------
Class File
public class GroupingTests {
    @BeforeGroups (groups ="regression")
      public void Login(){
      System.out.println("Login Successful");
      }
      @AfterGroups (groups ="regression")
      public void Logout(){
      System.out.println("Logout Successful");
      }
      @Test (groups ={"sanity"})
      public void FundTransfer (){
        System.out.println("Fund Transfer Successful");
      }
      @Test (groups ={"sanity", "regression"})
      public void Search (){
            System.out.println("Search Successful");
          }
      @Test (groups ={"regression"})
      public void PrepaidRecharge(){
      System.out.println("Prepaid Recharge Successful");
      }
      @Test (groups ={"regression"})
      public void BillPayments(){
      System.out.println("Bill Payments Successful");
      }
    }
-----------------------------------------------
Data driven Testing using DataProvider annotation
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;
}
}
-----------------------------------------------------
Parallel Test execution
Parallel test execution using TestNG (using single machine/computer)

Parallel test execution Using Selenium Grid (using multiple computers)
----------------------------------------
Take tests and execute using different browsers (IE, Firefox, and Chrome)
---------------------------------------------
Steps:

Before Class - Launch Browsers (Firefox, Internet Explorer and Chrome)

Execute Tests on all Browsers

After Class - Close all browsers
--------------------------------
Class File--------------------------
public class ParallelTests {
    public WebDriver driver;
   
    @Parameters("browser")
    @BeforeClass
       
    public void LunchBrowser(String browser) {

    if(browser.equalsIgnoreCase("FF")) {
    driver = new FirefoxDriver();
    }

    else if (browser.equalsIgnoreCase("IE")) {
    System.setProperty("webdriver.ie.driver", "E:/IEDriverServer.exe");
    driver = new InternetExplorerDriver();
    }

    else if (browser.equalsIgnoreCase("CH")) {
    System.setProperty("webdriver.chrome.driver", "E:/chromedriver.exe");
    driver = new ChromeDriver();
    }
    driver.get("http://www.google.com");
    }
    @Test
    public void VerifyPageTitle() throws InterruptedException {
    Assert.assertEquals(driver.getTitle(), "Google");
    }
    @AfterClass
    public void CloseBrowsers() {
    driver.quit();
    }
    }
-------------------------------
XML file--------------------------
<suite name="Suite" parallel="tests" thread-count ="3">
  <test name="FireFoxTests">
  <parameter name = "browser" value = "FF"/>
     <classes>
      <class name="seleniumTests.ParallelTests"/>
    </classes>
  </test>
 
  <test name="IETests">
  <parameter name = "browser" value = "CH"/>
      <classes>
      <class name="seleniumTests.ParallelTests"/>
          </classes>
  </test>
 
  <test name="ChromeTests">
  <parameter name = "browser" value = "IE"/>
     <classes>
     <class name="seleniumTests.ParallelTests"/>
    </classes>
  </test>
</suite>
----------------------------------------------------

0 comments:

Post a Comment