Note that there are some explanatory texts on larger screens.

plurals
  1. POError calling a java class (Selenium WebDriver, TestNG)
    primarykey
    data
    text
    <p>I'm trying to create a java class for reading an excel file. I want to use it in Selenium WebDriver (using Eclipse IDE) to do test driven scripts.</p> <p>I have one line that stumps me:</p> <p>//error here---->>>>Object[][] retObjArr = data("C:\ExcelFiles\LoginData.xls");</p> <p>This line is generating an error "The method data(String) is undefined for the type Login" I'm fairly new to OOP but thought I could call the class ExcelRead like the above and pass in one arg string...but obviously not!</p> <p>I'm sure many of you know how TestNG DataProviders annotations work which is what I'm trying to do here. There are many examples of this but all of them pull the ExcelRead code into the main TestNG project (Login in this case) class. I thought I'd make the excelRead class a separate class file that I could just call from any test class I create in the future.</p> <pre><code>import org.openqa.selenium.WebElement; import org.openqa.selenium.safari.SafariDriver; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import java.io.File; import java.io.FileInputStream; import java.util.HashMap; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class Login { @DataProvider(name = "ExcelInput") public Object[][] createData() throws Exception{ ExcelRead data = new ExcelRead(); </code></pre> <p>//error here---->>>>Object[][] retObjArr = data("C:\ExcelFiles\LoginData.xls"); return(retObjArr); }</p> <pre><code> @Test(dataProvider = "ExcelInput") public void LoginTest(String Username, String Password) throws InterruptedException{ System.out.println("test"); } } // code in its own Class file import java.io.File; import java.io.FileInputStream; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class ExcelRead { public static Object[][] main( String[] args) throws Exception{ File excel = new File(args[1]); FileInputStream fis = new FileInputStream(excel); HSSFWorkbook wb = new HSSFWorkbook(fis); HSSFSheet ws = wb.getSheet("Input") ; int rowNum = ws.getLastRowNum() + 1 ; int colNum = ws.getRow(0).getLastCellNum() ; String[][] data = new String[rowNum][colNum] ; for ( int i = 0 ; i &lt; rowNum ; i++) { HSSFRow row = ws.getRow(i) ; for ( int j = 0 ; j &lt; colNum ; j++) { HSSFCell cell = row.getCell(j) ; String value = cellToString(cell); data[i][j] = value ; System.out.println("the value is " + value); } } return data; } public static String cellToString(HSSFCell cell) { int type ; Object result ; type = cell.getCellType() ; switch (type) { case 0 : // numeric value in Excel result = cell.getNumericCellValue() ; break ; case 1 : // String Value in Excel result = cell.getStringCellValue() ; break ; default : throw new RuntimeException("There are no support for this type of cell") ; } return result.toString() ; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload