0

我是UDF(用户定义的函数)中的新混合框架。我创建了“ExcelUtils”类。我在其中编写了读写Excel表格的代码。此外,我创建了“Utils”类,在此之后从excel中捕获测试用例名称。我创建了一个函数名称getRowContains在这些中我无法获得对象excel表单无法使用UDF和Selenium webdriver获取Excel表格

utils的类:

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/

+0

你得到任何错误的所有元素?如果是,请张贴,你想要什么?你想读写使用Excel? –

回答

0

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

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

在utils的类中创建ExcelUtils对象访问ExcelUtils

+0

由于我在“ExcelUtils”类中设置了excel路径,为什么需要在utils类中定义它? – SelenyanC2

+0

编辑我的答案,希望你会得到一个想法来解决你的问题 –

相关问题