White Box Testing
Test your code
   Home      selenium2WebDriver-DataDrivenTesting
 
Selenium WebDriver Data Driven Testing with TestNG DataProviders
 
 
1) Call class method setup, and instanciate webdriver interface reference with FirefoxDriver
 
            @BeforeClass
            public void setUp() {
                        driver = new FirefoxDriver();
                        driver.get("http://www.whiteboxtest.com/selenium-test2.php");
            }
 
 
2) Call class method tearDown, and close firefox driver instance
 
            @AfterClass
            public void tearDown() {
                        driver.quit();
            }
 
           
3) Create Data Provider for test method
 
            @DataProvider(name = "data-provider", parallel = false)
            public Object[][] data() {
                       
                        return new Object[][] { { "hello", 2}, { "hiiiiiii", 1}, {"whiteboxtest.com", 2}};
                       
            }
 
 
4) Call the test method with Data Provider
 
            @Test(dataProvider="data-provider")
            public final void testClick(String textinput, int option) throws InterruptedException {
                        // Find the text-box by ID and input data
                        driver.findElement(By.id("q2")).clear();
                        driver.findElement(By.id("q2")).sendKeys(textinput);
                       
                        // Find the combo-box and select value
                        Select comboBox = new Select(driver.findElement(By.id("q11")));
                        comboBox.selectByIndex(option);
                       
                        // Find the page splitter and click
                        driver.findElement(By.id("td_q23")).click();
                       
                        Thread.sleep(10000);
                       
                        // Find the button and click/submit
                        driver.findElement(By.className("btn")).submit();
                       
            }
 
 
 
 
 
 
Selenium WebDriver Data Driven Testing with xls and TestNG DataProviders
 

1) Create data provider with reading xls file. Pass xls file name, sheet name, start and end cell values
 
            @DataProvider(name = "data-provider", parallel = false)
            public Object[][] data() throws Exception {
                        Object[][] retObjArr=getTableArray("TestInputData.xls","TestDataSheet", "Start", "End");
                        return(retObjArr);
            }
 
 
2) Read xls file with jxl classes and return data to data provider  
      
     public String[][] getTableArray(String xlFilePath, String sheetName, String tableStartName, String tableEndName) throws Exception{
            String[][] tabArray=null;
       
            Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));
            Sheet sheet = workbook.getSheet(sheetName);
            int startRow,startCol, endRow, endCol,ci,cj;
            Cell tableStart=sheet.findCell(tableStartName);
            startRow=tableStart.getRow();
            startCol=tableStart.getColumn();
 
            Cell tableEnd= sheet.findCell(tableEndName, startCol+1,startRow+1, 100, 100,  false);               
 
            endRow=tableEnd.getRow();
            endCol=tableEnd.getColumn();
            tabArray=new String[endRow-startRow-1][endCol-startCol-1];
            ci=0;
 
            for (int i=startRow+1;i<endRow;i++,ci++){
                cj=0;
                for (int j=startCol+1;j<endCol;j++,cj++){
                    tabArray[ci][cj]=sheet.getCell(j,i).getContents();
                }
            }
 
        return(tabArray);
    }
 
 
3) Call the test method with Data Provider
 
            @Test(dataProvider="data-provider")
            public final void testClick(String textinput, String option) throws InterruptedException {
                        // Find the text-box by ID and input data
                        driver.findElement(By.id("q2")).clear();
                        driver.findElement(By.id("q2")).sendKeys(textinput);
                       
                        // Find the combo-box and select value
                        Select comboBox = new Select(driver.findElement(By.id("q11")));
                        comboBox.selectByIndex(Integer.parseInt(option));
                       
                        // Find the page splitter and click
                        driver.findElement(By.id("td_q23")).click();
                       
                        Thread.sleep(10000);
                       
                        // Find the button and click/submit
                        driver.findElement(By.className("btn")).submit();
                        //driver.findElement(By.className("shape")).click(); alternatively can be clicked
            }