Thursday, May 8, 2014

How to execute the same test case with multiple set of input data using @DataProvider.

How @DataProvider work-
1) The number of columns become the number of arguments of the @Test method with their respective order as they have been added while storing in Object array.
2) Once the @Test annotation found with 'dataProvider' keyword, it will call the @DataProvider method.
3) This @DataProvider method will start iterating till the time @Test method has not been executed for all the rows of excel sheet.
4) In simple word, one could say, @DataProvider call the @Test method for each iteration.


Here the @Test method will execute the number of times rows are there in the sheet. For example here is the scenario of login in Yahoo.
Step1- Open the browser and open yahoo.com
Step2- Get the 1st row data from sheet and pass that as input (userName, pass, and expTitle) and click on login button.
Step3- Get the title of the page after login and verify with expTitle.
Step4- close the browser wether testcase passed or failed.
Step4. Then again go back to step2 and get the 2nd row data likewise it will execute the test case for all the rows.


import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataProvider1
{
    @Test(dataProvider="DataInput")
    public void login(String userName, String pass, String expTitle) throws InterruptedException{
        //open browser and Yahoo
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://in.yahoo.com/");
        Actions act = new Actions(driver);
        //login
        act.moveToElement(driver.findElement(By.xpath("//em[text()='Sign In']"))).perform();
        driver.findElement(By.xpath("//span//a[@class='login-svc ylogin login-btn-purple rapid-noclick-resp login-btn-small']")).click();
        driver.findElement(By.id("username")).sendKeys(userName);
        driver.findElement(By.id("passwd")).sendKeys(pass);
        driver.findElement(By.xpath("//button[contains(text(),'Sign in')]")).click();
       
        Thread.sleep(2000);
        String actTitle = driver.getTitle();
        try{
            Assert.assertEquals(actTitle, expTitle);
            driver.close();
        }catch(Exception e){
            driver.close();
        }
       
    }
    @DataProvider(name="DataInput")
    public static Iterator fetchData() throws InvalidFormatException, IOException{
        ArrayList myData = new ArrayList();
        FileInputStream fis = new FileInputStream("./InputData.xlsx");
        Workbook wb = WorkbookFactory.create(fis);
        Sheet sh = wb.getSheet("Sheet4");
        int numOfRows = sh.getLastRowNum();
        String userName, pass, expTitle;
        for(int i=0; i<numOfRows; i++){
            userName = sh.getRow(i).getCell(0).getStringCellValue();
            pass = sh.getRow(i).getCell(1).getStringCellValue();
            expTitle = sh.getRow(i).getCell(2).getStringCellValue();
            myData.add(new Object[]{userName,pass,expTitle});
        }
        return myData.iterator();
    }
}

7 comments:

  1. hello Sanjay
    i am working as Automation Engineer in a concern,today i stuck up with a problem,i am unable to select a item from drop down list actually developer has not used normal drop down,they used dropkick(javascript) i order to design UI.So how to select item from dropkick drop down.
    pls help me since i am new to Automation field ,your help is highly anticipated.

    ReplyDelete
  2. Select class can not be used always to select the option from dropdown. Select class can be used only when the dropdown menu has been created using select tag.
    So for other scenario, 1st you can click on dropdown menu and then you can click on the particular option, either after moving the cursor over option then click or directly click on the option.

    ReplyDelete
  3. hello sanjay
    How to execute the same test case with multiple set of input data using apache poi. I am new so please help me and give to send me any program with all step. My mail id is kapilkumarvarshney.it@gmail.com.

    ReplyDelete
  4. use @DataProvider.
    Please find the code here -
    http://selenium-makeiteasy.blogspot.in/search/label/%40DataProvider

    ReplyDelete
  5. Hello Sanjay,
    I have a User Form having so many fields including Text Fields, dropdown and so on, I want to create different users with different inputs. What should I do??

    ReplyDelete
  6. i need some clear explanation regarding to Verify and Assert....
    can anybody helps me

    ReplyDelete
  7. Hello Sanjay,
    Can I use 2 data provider in single method? or is there any way to user data from 2 different excel file in single method?
    Please help me,I will be very thankful if you do.

    ReplyDelete