Sunday, 18 November 2018

Selenium Frameworks - TestNG Framwork


TestNG Framework
TestNG stands for Test New Generation which is inspired from JUnit and NUnit. It is also same as Junit but it has more functionalities than Junit
Advantages of TestNG:
·       Contains more Annotations
·       Test cases can be grouped more easily
·       Dependency testing is possible
·       Parallel testing is possible using Grid2


Downloading & Configuring TestNG into eclipse:
TestNG Installation:
 “Help” menuà Eclipse Marketplace à enter  TestNG in Search Edit box à click on Goàclick on install


**TestNg annotations:
**TestNG is a built-in framework tool, which we can use in Selenium Webdriver to execute our test scripts without main () method in classes using annotations
Annotations are used to specify sequence of methods execution without using main method
1. @Test: it is used to specify method under this annotation  is a test case(i.e. validation part)
In one class we can maintain multiple @Test  annotation methods
By default those methods will execute in alphabetical order
Based on requirement we can set execution sequence for @Test annotation methods using ‘priority’
Syntax:
@Test(priority=1)
2. @BeforeMethod:     methods under this annotation will be executed before each @Test annotation method (i.e. @Test)
Note:
@BeforeMethod  annotation method will execute depends on number @Test annotation methods in a class

3. @AfterMethod: method under this annotation will be executed after each @Test annotation method (i.e. @Test)
Note:
@AfterMethod  annotation method will execute depends on number @Test annotation methods in a class

4. @BeforeClass: method under this annotation will execute prior to any method in that class
5. @AfterClass: method under this annotation will execute after all the methods are executed in a class
6. @Parameters: to get data from TestNg xml file

sequence of annotations
@BeforeClass
@BeforeMethod
@Test
@AfterMethod
@AfterClass

Ex: create script to perform execution by taking each scenario into individual methods
Scenarios:
This program to launch browser
This program to refresh the page after each transaction
This program is to validate user Registration operation
This program is written to validate login functionality
This script to set home page of application
To close browser

ex:
public class TestNgEx02 {
   
    @BeforeClass
    public void setUp(){
         System.out.println("To initialize browser and to open URL");
    }
   
    @AfterMethod
    public void refreshPg(){
         System.out.println("To refresh the page after each validation");
    }
   
    @Test (priority=1)
    public void userReg(){
         System.out.println("Validate user Registration");
    }
    @Test (priority=2)
    public void userLogin(){
         System.out.println("Login validation");
    }
   
    @BeforeMethod
    public void homePg(){
         System.out.println("To set Home page before each validation");
    }
   
    @AfterClass
    public void tearDown(){
         System.out.println("to close application");
    }

}
O/p:


To initialize browser and to open URL
To set Home page before each validation
Validate user Registration
To refresh the page after each validation
To set Home page before each validation
Login validation
To refresh the page after each validation
to close application




Ex: TestNg program for validation
package testng.framework;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestNgValidation {
                                       
                                        WebDriver driver;
                                        @BeforeClass
                                        public void setUp(){
                                                driver= new FirefoxDriver();
                                                driver.get("http://demo.actitime.com/login.do");
                                                driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
                                        }
                                       
                                        @Test (priority=0)
                                        public void LoginTest(){
                                                WebElement objUserName = driver.findElement(By.name("username"));
                                                if (objUserName.isDisplayed()) {
                                                          Reporter.log("Username element found in Application and Script executing ....",true);
                                                                   objUserName.sendKeys("user");
                                                                                                  driver.findElement(By.name("pwd")).sendKeys("user");
                                                                                      driver.findElement(By.xpath("//a[@id='loginButton']/div")).click();
                                                                            
                                                } else {
                                                          Reporter.log("Username element not found in Application",true);
                                                }
                                        }

                                        @Test (priority=1)
                                        public void LoginValidation(){
                                                String str1= driver.findElement(By.linkText("Logout")).getText();
                                                Assert.assertEquals(str1, "Logout1");
                                        }
                                        @AfterClass
                                        public void tearDown() {
                                                driver.close();
                                        }

}




TestNg Xml file:
Using TestNg xml file we can execute multiple classes from different packages, and also we can include/exclude some methods from class
Creating TestNg xml file:
Navigation:
Select Java project
   Right click on mouse
     Select “File”             
                                        Enter file name with “.xml” extension
                                                àclick on “Finish”
                                                          àwrite following code in “Source view” of testing xml file


================================================





















=====================================================

Note:
One suite can have multiple tests
One test can have multiple classes
One class can have multiple methods



Ex: Create TestNg Xml file to execute tests from different classes from different packages
Procedure:
Step 1: Create new package with name as “sampleone”
Step 2: create 2 classes in “sampleone” package with 2 @Test annotation methods
Class-1:
package sampleone;

import org.testng.annotations.Test;

public class Class01 {
   
    @Test
    public void methodOne(){
         System.out.println("Method-1 from Class01");
    }
   
    @Test
    public void methodTwo(){
         System.out.println("Method-2 from Class01");
    }

}
Class-2:
package sampleone;

import org.testng.annotations.Test;

public class Class02 {
    @Test
    public void methodOne(){
         System.out.println("Method-1 from Class02");
    }
   
    @Test
    public void methodTwo(){
         System.out.println("Method-2 from Class02");
    }
}

Step 3: Create new package with name as “sampletwo”
Step 4: Create class in “sampletwo” package with 2 @Test annotation methods
Class03:
package sampletwo;

import org.testng.annotations.Test;

public class Class03 {
   

         @Test
         public void methodOne(){
             System.out.println("Method-1 from Class03");
         }
        
         @Test
         public void methodTwo(){
             System.out.println("Method-2 from Class03");
         }
   

}

Step 5: create TestNg xml file to execute all the methods from different classes (i.e. Class01, Class02 and Class03 classes)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TestNg xml file
<suite name="my Demo testNg suite" >
<test name="Sample tests">
    <classes>
         <class name="sampleone.Class01" />
         <class name="sampleone.Class02" />
         <class name="sampletwo.Class03" />
    </classes>
</test>

</suite>
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Create TestNg xml file to execute some of the methods only
TestNg xml file code
<suite name="my Demo testNg suite" >
<test name="Sample tests">
    <classes>
         <class name="sampleone.Class01" />
             <methods>
                 <include name="methodOne" />
                 <exclude name="methodTwo" />
             </methods>
         <class name="sampleone.Class02" />
         <class name="sampletwo.Class03" />
    </classes>
</test>

</suite>



EX:
Create TestNg class to validate login functionality in mercury tours application by passing the data from Testng xml file
TestNg class:
package testng.concepts;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class MtLoginTest {
   
    WebDriver driver;
    @BeforeClass
    public void setUp(){
         driver= new FirefoxDriver();
         driver.get("http://newtours.demoaut.com");
         driver.manage().window().maximize();
    }

    @Test
    @Parameters({"uid","pwd"})    //to read data from TestNg xml file
    public void userLogin(String userid, String passwd) throws InterruptedException{
         //to perform login operation
        driver.findElement(By.name("userName")).sendKeys(userid);
        driver.findElement(By.name("password")).sendKeys(passwd);
         driver.findElement(By.name("login")).click();
         Thread.sleep(5000);
        
    //TestNg Assert class to validate page title
         Assert.assertEquals(driver.getTitle(), "Find a Flight: Mercury Tours:");
         //to send user msg into XML reports and log section
         Reporter.log("Successful Login operation", true);
        
    }
    @AfterClass
    public void tearDown(){
         driver.close();
    }
}
======================================================
TestNg xml file
<suite name="Suite">
  <test name="Test">
  <parameter name="uid" value="mercury" />
  <parameter name="pwd" value="mercury" />
    <classes>
      <class name="testng.concepts.MtLoginTest"/>
    </classes>
  </test>
</suite>




Ex: create script to perform login operation in mercury tours application on different browsers based on keyword given from testing xml file
Procedure:
Step 1: create class with required methods to perform login operation in mercury tours
Step 2: Create TestNg xml file by passing keyword to initialize browser
Procedure:
Step 1: create class with required methods:
public class Google {
    
     WebDriver driver;
     @BeforeMethod
     @Parameters({"browser"})
     public void setUp(String myBrowser){
         
          if(myBrowser.equalsIgnoreCase("firefox")){
              driver=new FirefoxDriver();
          }
          else if(myBrowser.equalsIgnoreCase("chrome")){
              System.setProperty("webdriver.chrome.driver","chromedriver.exe");
             driver=new ChromeDriver();
          }
          else if(myBrowser.equalsIgnoreCase("ie")){
             System.setProperty("webdriver.ie.driver","IEDriverServer_32.exe");
              driver=new InternetExplorerDriver();
          }
         
     }
     @Test
     public void f() throws InterruptedException {
      
       Thread.sleep(4000);
       driver.get("http://google.com/");
       driver.findElement(By.name("q")).sendKeys("selenium");
       driver.findElement(By.name("btnG")).click();
      
  }
}

Create TestNg xml file with following code
xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="tests">
  <test name="firefoxTest">
    <parameter name="browser" value="firefox"/>
    <classes>   
      <class name="demo1.Google"/>
    </classes>
  </test>

  <test name="chromeTest">
    <parameter name="browser" value="chrome"/>
    <classes>
      <class name="demo1.Google"/>
    </classes>
   
  </test>
 
  <test name="ieTest">
  <parameter name="browser" value="ie"/>
    <classes>
      <class name="demo1.Google"/>
    </classes>
  </test>
  </suite>