简体   繁体   English

调用Java类(Selenium WebDriver,TestNG)时出错

[英]Error calling a java class (Selenium WebDriver, TestNG)

I'm trying to create a java class for reading an excel file. 我正在尝试创建一个用于读取Excel文件的Java类。 I want to use it in Selenium WebDriver (using Eclipse IDE) to do test driven scripts. 我想在Selenium WebDriver(使用Eclipse IDE)中使用它来测试驱动脚本。

I have one line that stumps me: 我有一条让我难过的地方:

//error here---->>>>Object[][] retObjArr = data("C:\\ExcelFiles\\LoginData.xls"); //这里出现错误---- >>>> 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! 该行生成错误“对于登录类型未定义方法data(String)”,我对OOP还是很陌生,但我认为我可以像上面那样调用类ExcelRead并传入一个arg字符串...但显然不是!

I'm sure many of you know how TestNG DataProviders annotations work which is what I'm trying to do here. 我敢肯定,你们中的许多人都知道TestNG DataProviders批注的工作方式,这就是我在这里要做的。 There are many examples of this but all of them pull the ExcelRead code into the main TestNG project (Login in this case) class. 有许多示例,但所有示例都将ExcelRead代码拉入主要TestNG项目(在本例中为Login)类。 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. 我以为可以将excelRead类作为一个单独的类文件,以后可以从我创建的任何测试类中调用它。

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"); //这里出现错误---- >>>> Object [] [] retObjArr = data(“ C:\\ ExcelFiles \\ LoginData.xls”); return(retObjArr); 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 如下修改main :将main更改为fileRead

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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