10/17/15

TestNG Scripts

1)    TestNG Annotations
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class basicAnnotationsEx

            {
               @BeforeSuite
               public void suite()
               {
                   System.out.println("@Befor Suite - one time executed before any testNG function");
                         }
                         
               @AfterSuite
                public void afterSuite(){
             
                  System.out.println("@After Suite - one time executed after all testNg function");
             
               }
              
              
               @BeforeClass
                public void beforeClass() {
             
                    // one-time initialization code
             
                  System.out.println("@BeforeClass - one time executed before first mathod");
             
               }
             
               @AfterClass
                public void afterClass() {
             
                  // one-time cleanup code
             
                  System.out.println("@AfterClass - one time executed after last method");
             
               }
             
               @BeforeMethod
                public void setUp() { 

                   System.out.println("@BeforeMethod - executed before all test method");
             
               }
             
               @AfterMethod
                           public void tearDown() { 

                   System.out.println("@AfterMethod - executed after all test methods");
             
               }
             
              
               @Test
                           public void testFirstMethod() {   

                  System.out.println("@Test - executed first test method");
             
               }
             
               @Test
                public void testSecondMethod() {   

                  System.out.println("@Test - executed second test method");
             
               }
             
            }


2)   @BeforeMethod, @AfterMethod

import org.testng.annotations.*;

public class sample1 {

            @BeforeMethod
            public void Login()
            {
                        System.out.println("Login successful");
            }
           
            @Test(priority=1)
            public void search()
            {
                        System.out.println("search is  successful");
            }
           
            @Test(priority=2)
            public void Advancedsearch()
            {
                        System.out.println("Advanced search is  successful");
            }
           
            @Test(priority=3)
            public void BuyProducts()
            {
                        System.out.println("BuyProducts search is  successful");
            }
           
           
            @AfterMethod
            public void Logout()
            {
                        System.out.println("Logout successful");
            }
}


3)   @BeforeClass, @AfterClass
import org.testng.annotations.*;
import org.testng.annotations.Test;

public class sample2 {

            @BeforeClass
            public void Login()
            {
                        System.out.println("Login successful");
            }
           
            @Test(priority=1)
            public void search()
            {
                        System.out.println("search is  successful");
            }
           
            @Test(priority=2)
            public void Advancedsearch()
            {
                        System.out.println("Advanced search is  successful");
            }
           
            @Test(priority=3)
            public void BuyProducts()
            {
                        System.out.println("BuyProducts search is  successful");
            }
           
           
            @AfterClass
            public void Logout()
            {
                        System.out.println("Logout successful");
            }
}


4)   @BeforeTest, @AfterTest,@BeforeSuite,@AfterSuite
import org.testng.annotations.*;
import org.testng.annotations.Test;


public class sample3 {

@BeforeSuite
 public void method1()
 {
             System.out.println("This is test1 method1");
 }
@AfterSuite
 public void method2()
 {
             System.out.println("This is test1 method2");
 }

@BeforeTest
 public void method3()
 {
             System.out.println("This is test1 method2");
 }
           
 @AfterTest
 public void method4()
 {
             System.out.println("This is test1 method2");
 }

 @BeforeMethod
 public void method5()
 {
             System.out.println("This is test1 method2");
 }
           
 @AfterMethod
 public void method6()
 {
             System.out.println("This is test1 method2");
 }
           
@BeforeClass
 public void method7()
 {
             System.out.println("This is test1 method2");
 }

@AfterClass
 public void method8()
 {
             System.out.println("This is test1 method2");
 }

@Test(priority=1, enabled=false)
public void method9()
{
             System.out.println("This is test1 method2");
}

@Test(priority=2)
public void method10()
{
             System.out.println("This is test1 method2");
}

}


5)   @Executing multiple classes using XML file

Method1:
             
                          
                                         
                

           

           
           
                         
                                      
           

             


           
                         
                                      
           

             

             


Method2:

             
                          
                                        
                                                             
                                               
                

           

                         


6)   Ignoring Tests:
import org.testng.annotations.*;

public class ignoreTest {
           
            @BeforeTest
            public void login()
            {
                        System.out.println("Login Successful");
            }

            @AfterTest
            public void logout()
            {
                        System.out.println("Logout Successful");
            }
           
            @Test (priority=1)
            public void addemployee()
            {
                        System.out.println("Emplotee ADDED successfully");
            }

            @Test (priority=2, enabled=false)
            public void updateemployee()
            {
                        System.out.println("Employee UPDATED Successfully");
            }

           
            @Test (priority=3)
            public void displayemployee()
            {
                        System.out.println("Employee DISPLAYED Successfully");
            }

            @Test (priority=4)
            public void deleteemployee()
            {
                        System.out.println("Employee DELETED Successfully");
            }
           
           
}

7)   @Test annotation parameter ‘dependsOnMethods’
import org.testng.Assert;
import org.testng.annotations.*;

public class dependsonEx {
           
            @Test
            public void Login()
            {
                        Assert.assertTrue(true);
                       
            }
           
            @Test(dependsOnMethods={"Login"})
            public void search()
            {
                        Assert.assertEquals("abs","xyz");
            }
           
            @Test(dependsOnMethods={"search"}, alwaysRun=true)
            public void Logout()
            {
                        Assert.assertTrue(true);
                       
            }
           

8)   Grouping Tests:

import org.testng.annotations.*;

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("prepaidrecharge successful");
            }
           
            @Test(groups={"Regression"})
            public void BillPayments()
            {
                        System.out.println("BillPayments successful");
            } }
Xml file:


           
                       
           



           




9)   Parameter Annotation with xml file:

import org.testng.annotations.*;

public class sampletestparam {
           
            @Test
            @Parameters("myName")   // Annotation
           
            public void m1(String s)
            {
                        System.out.println(s);
            }
           
}


Xml file:







10)                     Parallel Test Execution on multiple browsers
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class MultiBrowser

{

  public WebDriver driver;

  @Parameters("browser")

   @BeforeTest

  // Passing Browser parameter from TestNG xml

  public void beforeTest(String browser)
  {

  // If the browser is Firefox, then do this

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

  // If browser is IE, then do this         

  } else if (browser.equalsIgnoreCase("ie"))
 
    {
              // Here I am setting up the path for my IEDriver

              System.setProperty("webdriver.ie.driver", "C://Program Files/eclipse/IEDriverServer.exe");
              driver = new InternetExplorerDriver();

  } else if (browser.equalsIgnoreCase("chrome"))
  {
              System.setProperty("webdriver.chrome.driver", "C://Program Files//eclipse//chromedriver.exe");
      driver=new ChromeDriver();
             }

  // Launch the Website
   
  driver.get("http://newtours.demoaut.com");

  }

  // Once Before method is completed, Test method will start

  @Test
  public void login() throws InterruptedException
  {

            driver.findElement(By.name("userName")).sendKeys("mercury");
            driver.findElement(By.name("password")).sendKeys("mercury");
            driver.findElement(By.name("password")).submit();

            } 

  @AfterTest
  public void afterTest() {

                        //driver.quit();

            }

}

Xml file:


           
                       
                                   
                                               
                                   

           


           
           
                         
                                     
                                                 
                                     

           


           
                       
                                   
                                                 
                                     

           




11)                     Dataprovider Annotation for parameterization
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.*;
             
            public class dataproviderex {
             
                        WebDriver driver;
                                     
             @BeforeTest
             public void setup()
             {
                         driver= new FirefoxDriver();
             }
             
             @DataProvider(name = "Authentication")
             
              public Object[][] credentials()
              {
                         Object data[][]=new Object[][] { { "mercury", "mercury" }, { "xyz", "xyz" }};
                       
                         return data;
              }
             
             
              // Here we are calling the Data Provider object with its Name
             
              @Test(dataProvider = "Authentication1")
              public void logintest(String uname, String passwrd)
              {
                                   
                                    driver.get("http://newtours.demoaut.com/");
                    driver.findElement(By.name("userName")).sendKeys(uname);
                                    driver.findElement(By.name("password")).sendKeys(passwrd);
                                    driver.findElement(By.name("login")).click();
                                   
                                    String actualtitle=driver.getTitle();               // Get the title of the page
                                   
                                    if (actualtitle.contentEquals("Find a Flight: Mercury Tours:"))
                                                {
                                                Assert.assertTrue(true);
                                               
                                                }
                                    else
                                    {
                                                Assert.fail();
                                    }
                       
                         }
             
              @AfterTest
              public void closebrowser()
              {
                        driver.quit();
                       
              }
             
             
}

12)                     Dataprovider Annotation with Excel sheet


import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.*;

public class Execelwithdatadriver {
           
            public WebDriver driver;
           
            @BeforeTest
            public void setup()
            {
                        driver= new FirefoxDriver();
            }

            @DataProvider(name="Authentication")
            public Object[][] credentials() throws IOException
            {
                        Object[][] arrayObject = getExcelData("C://Login1.xlsx","Sheet1");   //calling getExcelData method for reading data
                        return arrayObject;
            }
           
           
            @Test(dataProvider="Authentication")
            public void logintest(String uname, String passwrd)
            {
                       
                        driver.get("http://newtours.demoaut.com/");
        driver.findElement(By.name("userName")).sendKeys(uname);
                        driver.findElement(By.name("password")).sendKeys(passwrd);
                        driver.findElement(By.name("login")).click();
                       
                        String actualtitle=driver.getTitle();               // Get the title of the page
                       
                        if (actualtitle.contentEquals("Find a Flight: Mercury Tours:"))
                                    {
                                    Assert.assertTrue(true);
                                   
                                    }
                        else
                        {
                                    Assert.fail();
                        }
           
            }
           
           
public String[][] getExcelData(String fileName, String sheetName) throws IOException
                        {
                                    String[][] arrayExcelData = null;
                       
                                    FileInputStream fs = new FileInputStream(fileName);
                                    XSSFWorkbook  wb = new XSSFWorkbook(fs);
                                    XSSFSheet sh = wb.getSheet(sheetName);

                                    int totalNoOfRows = sh.getLastRowNum();
                                   
                                    XSSFRow row=sh.getRow(0);
                                    int totalNoOfCols=row.getLastCellNum();
                                   
                                    //System.out.println("total cols:"+totalNoOfCols);
                                    //System.out.println("total rows:"+totalNoOfRows);
                                   
                                    arrayExcelData = new String[totalNoOfRows][totalNoOfCols];
                                   
                                    for(int i=1;i<=totalNoOfRows;i++)
                                    {
                                                XSSFRow r=sh.getRow(i);
                                               
                                                for(int j=0;j<totalNoOfCols;j++)
                                                {
                                                            XSSFCell col=r.getCell(j);
                                                            arrayExcelData[i-1][j] = col.getStringCellValue();         
                                                }

                                    }
                       
                        return arrayExcelData;
            }



@AfterTest
public void closebrowser()
{
            driver.quit();
           
}

}


0 comments:

Post a Comment