简体   繁体   中英

Error calling a java class (Selenium WebDriver, TestNG)

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.

I have one line that stumps me:

//error here---->>>>Object[][] retObjArr = data("C:\\ExcelFiles\\LoginData.xls");

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!

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.

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();

//error here---->>>>Object[][] retObjArr = data("C:\\ExcelFiles\\LoginData.xls"); return(retObjArr); }

    @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 < rowNum ; i++) {
        HSSFRow row = ws.getRow(i) ;
            for ( int j = 0 ; j < 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() ;
        }
}

Try this:

Object[][] retObjArr = data.fileRead("C:\ExcelFiles\LoginData.xls");

Modify main as follows: Change main to fileRead

public static Object[][] fileRead( String args) throws Exception{
    File excel = new File(args);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM