Wednesday 8 February 2017

SELENIUM: @DataProvider In TESTNG

SELENIUM: @DataProvider In TESTNG:



1.By Using @DataProvider,we can pass the multiple sets of data to the Testmethod based on our requirement (OR) Marks a method as supplying data for a test method.
2.The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test method.
3.The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.
4.The name of this data provider. If it's not supplied, the name of this data provider will automatically be set to the name of the method.
5. In @dataProvider by default parallel=false.
If we write "parallel=true" then the @Test method  will run parallelly i,e this method runs parallelly  on multiple threads.

syntax:

@DataProvider(name="dataProviderName",parallel=true)
          public  Object[][]  methodName()
          {
                   Object[][] obj=new Object[rows][columns];
                   ;;;;
                   ;;;;;
                   ;;;;

                   return obj;
          }

@DataProvider is always going to return Two-Dimensional array.

In DataProvider Two dimensional arrays,'rows' indicates how many times you want to repeat the testcase and 'columns' indicates number of inputs(for how many webelements you want to pass the data).we need to pass rows and columns in the form of integer values.

DataProvider name can be anything,but it should be meaningful.

Example:

package com.rameshsoft.rameshselenium;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataProviderDemo {
         
          static String uname;
          static String pwd;
                  
         
          @BeforeClass
          public void beforeClass()
          {
                   System.out.println("Before class executing");
          }
          @BeforeTest
          public void beforeTest()
          {
                   System.out.println("Before test executing");
          }
          @BeforeMethod
          public void beforeMethod()
          {
                   System.out.println("Before @test executing");
          }
         
          @Test(dataProvider="dataProvider1")
          public void execute(String username,String password) throws InterruptedException
          {
          uname=username;
          pwd=password;
                   System.out.println(uname);
                   System.out.println(pwd);

                   System.setProperty("webdriver.gecko.driver", "C:\\Users\\ramesh\\Desktop\\java\\desk\\geckodriver-v0.11.1-win64\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.manage().timeouts().implicitlyWait(50,TimeUnit.SECONDS);

driver.get("https://www.gmail.com");

driver.findElement(By.id("Email")).sendKeys(uname);

driver.findElement(By.id("next")).click();

Thread.sleep(2000);

driver.findElement(By.id("Passwd")).sendKeys(pwd);

driver.findElement(By.id("signIn")).click();

driver.quit();
}

@DataProvider(name="dataProvider1",parallel=true)
          public Object[][] values()
          {
                   Object[][] obj=new Object[2][2];
                   obj[0][0]="rameshsoft.selenium@gmail.com";
                   obj[0][1]="123456";
                   obj[1][0]="rameshatbtech@gmail.com";
                   obj[1][1]="123456899";
                  
                   return obj;
          }
}


OUTPUT:
Before test executing
Before class executing

Before @test executing

Before @test executing

rameshatbtech@gmail.com
123456899

rameshsoft.selenium@gmail.com
123456
if  parallel=false  in @DataProvider then output as follows
Before test executing
Before class executing
Before @test executing
rameshsoft.selenium@gmail.com
123456

Before @test executing
rameshatbtech@gmail.com
123456899

No comments:

Post a Comment