使用带有Selenium webdriver的UDF无法获得excel表

时间:2017-07-06 09:41:09

标签: java excel selenium selenium-webdriver user-defined-functions

我是混合框架的UDF(用户定义函数)的新手。我创建了" ExcelUtils"类。我在其中编写了用于读取和写入excel表的代码。另外,我创建了" Utils" class,在从excel中捕获testcase名称之后我在这些中创建了一个函数名称 getRowContains 我无法获得对象 excelSheet

实用工具类:

public class Utils {

    private static FirefoxDriver driver;

    public static WebDriver openBrowser(int iTestCaseRow){
        String sBrowserName;
        try{
            sBrowserName= ExcelUtils.getCellData(iTestCaseRow, Constant.Col_Browser);

            if(sBrowserName.equals("Mozilla")){
                System.setProperty("webdriver.gecko.driver", "/Users/ileadsynapse/Desktop/CT/geckodriver");
                DesiredCapabilities capabilities= DesiredCapabilities.firefox();
                capabilities.setCapability("marionette", true);
                driver = new FirefoxDriver(capabilities);
                Log.info("New driver instantiated");
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                Log.info("Implicit wait applied on the driver for 10 seconds");
                driver.get(Constant.URL);
                Log.info("Web application get launched");
            }
        }catch(Exception e){
            Log.error("Class Utils | Method OpenBrowser | Exception Desc:"+e.getMessage());
        }

        return driver;

    }

    public static String getTestCaseName(String sTestCase){
        String value= sTestCase;

        try {
            int posi= value.indexOf("@");
            value = value.substring(0, posi);
            posi= value.lastIndexOf(".");
            value= value.substring(posi+1);

            return value;

        } catch (Exception e) {
            // TODO: handle exception
             Log.error("Class Utils | Method getTestCaseName | Exception desc : "+e.getMessage());
             throw(e);
        }

    }

    public static int getRowContains(String sTestCaseName, int colNum){
                int i;      
        try{
            int rowCount= excelSheet.


        }catch(Exception e){

        }
    }

}

ExcelUtils:

public class ExcelUtils {

    private static XSSFWorkbook excelBook;
    private static XSSFSheet excelSheet;
    private static XSSFCell cell;
    private static XSSFRow row;

    //This method is to set the File path and to open the Excel file, Pass Excel Path and Sheetname as Arguments to this method
    public static void setExcelFile(String Path, String SheetName) throws Exception{
        try{
            //Open excel file
            FileInputStream excelFile = new FileInputStream(Path);
            // Access the required test data sheet
            excelBook = new XSSFWorkbook(excelFile);
            excelSheet = excelBook.getSheet("Sheet1");
        }catch(Exception e){
            throw(e);
        }
    }

    //This method is to read the test data from the Excel cell, in this we are passing parameters as Row num and Col num
    public static String getCellData(int RowNum, int ColNum){
        try {
            cell = excelSheet.getRow(RowNum).getCell(ColNum);
            String cellData = cell.getStringCellValue();
            return cellData;
        } catch (Exception e) {
            return "";
        }
    }

    //This method is to write in the Excel cell, Row num and Col num are the parameters
    public static void setCellData(String Result, int RowNum, int ColNum) throws Exception{
        try{
            row = excelSheet.getRow(RowNum);
            cell = row.getCell(ColNum, row.RETURN_BLANK_AS_NULL);
            if(cell==null){
                cell = row.createCell(ColNum);
                cell.setCellValue(Result);
            }else{
                cell.setCellValue(Result);
            }

            // Constant variables Test Data path and Test Data file name
            FileOutputStream fileOut = new FileOutputStream(Constant.Path_TestData+ Constant.File_TestData);
            excelBook.write(fileOut);
            fileOut.flush();
            fileOut.close();

        }catch(Exception e){
            throw(e);
        }
    }

}

任何人都可以帮助解决它。我已经参考了这个教程:http://toolsqa.com/selenium-webdriver/user-defined-functions/

1 个答案:

答案 0 :(得分:0)

您还没有创建excel对象来访问Utils类中的excelsheet。

在utils类中声明private static XSSFSheet excelSheet;以获取excelsheet对象。

在utils类中创建ExcelUtils对象以访问ExcelUtils的所有元素