1

The line marked in star is giving error when the test is run in TestNG
Guys tell me what am I doing wrong in this.
I have attached the excel with 3 rows and 3 columns in 1st sheet.

Data lib code-

package Datalibrery;

import java.io.File;
import java.io.FileInputStream;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

   public class Dataconfig {

      XSSFWorkbook WB;
      XSSFSheet sheet1;

    public Dataconfig(String Excelpath){



    try {
        File src=new File(Excelpath);

        FileInputStream fis=new FileInputStream(src);

        WB=new XSSFWorkbook(fis);

         sheet1=WB.getSheetAt(0);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        System.out.println(e.getMessage());
  }

}



public String getData(int  sheetno,int row,int col){

    sheet1=WB.getSheetAt(sheetno);

    **String data=sheet1.getRow(row).getCell(col).getStringCellValue();**

    return data;
}

public int getRowCount(int sheetindex){

    int row=WB.getSheetAt(sheetindex).getLastRowNum();
    row=row+1;
    return row;
}


}

Script Code-

  package SampleTest;

  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.BeforeTest;
  import org.testng.annotations.DataProvider;
  import org.testng.annotations.Test;

  import Datalibrery.Dataconfig;


    public class ContactusDDT {

       WebDriver driver;

      @BeforeTest

      public void beforeTest() {

      // If the browser is Firefox, then do this



          driver = new FirefoxDriver();


      // Doesn't the browser type, lauch the Website

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

      }
@Test
public void login(){

    driver.get("Websit URL here");
    driver.findElement(By.xpath("//*[@id='u157-4']/p")).click();// contact
}

@Test(dataProvider = "Contactdata")
  public void testLogIn(String Harshal1, String harshalb ,String Hi) throws Exception {





    driver.findElement(By.xpath("//*[@id='widgetu798_input']")).clear();
    driver.findElement(By.xpath("//*[@id='widgetu798_input']")).sendKeys(Harshal1);
    driver.findElement(By.xpath("//*[@id='widgetu783_input']")).clear();
    driver.findElement(By.xpath("//*[@id='widgetu783_input']")).sendKeys(harshalb);
    driver.findElement(By.xpath("//*[@id='widgetu791_input']")).clear();


  }

@DataProvider(name = "Contactdata")
public Object[][] Testdata()

{
    Dataconfig conf=new Dataconfig("C:\\Users\\harshadab\\workspace\\Sampledatadiven\\excelData\\Managemygrp.xlsx");
    int rows=conf.getRowCount(0);

    Object[][] data = new Object[rows][3];

    for(int i=0;i<rows;i++)
    {
        ***data[i][0]=conf.getData(1, i, 0);***
        data[i][1]=conf.getData(1, i, 1);
        data[i][2]=conf.getData(1, i, 2);


    }

    return data;
}

}

  • 1
    Harsha- It would be better if you- provide the error/exception stack trace in the post. – demouser123 Jan 14 '16 at 7:28
  • Could you edit in some information about the getData method? Siraj has a potential solution for you, but it would be better to validate that against the parameters he's changing. – corsiKa Jan 18 '16 at 17:08
3

In excel sheet always start with 0-column and 0-row and index 0 as well.

Change

 data[i][0]=conf.getData(1, i, 0); 
 data[i][1]=conf.getData(1, i, 1);
 data[i][2]=conf.getData(1, i, 2);

to

 data[i][0]=conf.getData(0, i, 0);
 data[i][1]=conf.getData(0, i, 1);
 data[i][2]=conf.getData(0, i, 2);
  • Could you add some explanation for the change? When someone comes to this questions later, they'll want to know why it's necessary to change the values as you've suggested. – Kate Paulk Jan 18 '16 at 12:52
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. - From Review – Kate Paulk Jan 18 '16 at 12:52
  • @KatePaulk Given siraj's edit, I think this is a valid answer now. (Even if not correct... the two are different...) Would you agree? – corsiKa Jan 18 '16 at 17:04
  • @siraj It still could be more clear now, though. Are you suggesting that the sheet be zero indexed as well? – corsiKa Jan 18 '16 at 17:07
  • I'd agree it's a valid answer now although I would like to see more explanation. – Kate Paulk Jan 18 '16 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.