如何使用java在selenium中输入excel文件中的结果

时间:2016-05-20 09:17:31

标签: java excel selenium selenium-webdriver

我已经完成了从excel中读取输入数据的代码,现在我想在excel中编写PASS,FAIL结果到特定的测试用例,但我不知道如何编写正确的代码。你也可以给我一些例子。

请帮帮我。  这是我的代码。

            public static void main(String[] args) throws IOException {
            WebDriver driver=null;
             Scanner scanner = new Scanner(System.in);
           //  prompt for the URL
            System.out.print("Enter your URL: ");
            // get their input as a String
            String URL = scanner.next();
            //System.out.println( URL );

            final FirefoxProfile firefoxProfile = new FirefoxProfile();
          driver = new FirefoxDriver(firefoxProfile);
           driver.get(URL);
          driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
          driver.manage().window().maximize();
            //read data from excel
                try {   
                  FileInputStream file = new FileInputStream(new File ("C:\\Documents and Settings\\Deepa\\Desktop\\AMC test cases1.xls")); 
                   HSSFWorkbook workbook = new HSSFWorkbook(file);
                   HSSFSheet sheet = workbook.getSheet("login page");

                   WebElement login=driver.findElement(By.cssSelector("a[data-target='#login-box']"));
                     login.click();
                     driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);

    //For Email ID 
       driver.findElement(By.id(sheet.getRow(1).getCell(1).getStringCellValue())).sendKeys(sheet.getRow(1).getCell(0).getStringCellValue());

    //FOR Password 

    driver.findElement(By.id(sheet.getRow(2).getCell(1).getStringCellValue())).sendKeys(sheet.getRow(2).getCell(0).getStringCellValue());

     //For Submit the form 
      driver.findElement(By.id(sheet.getRow(3).getCell(1).getStringCellValue())).click();

                   file.close();
//Else

             } catch (FileNotFoundException fnfe) {
                     fnfe.printStackTrace();
             } catch (IOException ioe) {
                      ioe.printStackTrace();
                    }

1 个答案:

答案 0 :(得分:0)

使用POI jar使用getXLcellValue函数获取excel中的值使用setXLCellValue函数在excel中设置值

参数: -

  • xlpath - > Excel文件的位置
  • sheetName - > Excel文件中的工作表名称
  • rowNum和cellNum - >数据所在的行号和列号 本

    public String getXLcellValue(String xlpath, String sheetName, int rowNum, int cellNum)
    
      {
         try{
                FileInputStream fis=new FileInputStream(xlpath);
            Workbook wb=WorkbookFactory.create(fis);
    
        Log.info("Get the value from the cell(getXLcellValue)");
    
            return wb.getSheet(sheetName).getRow(rowNum).getCell(cellNum).getStringCellValue();
    }
    catch(Exception ex)
    {
        Log.info("Error in getXLcellValue ="+ ex.getMessage());
    }
     return "";
    }
    
    
       //set the value of the cell present in specific sheet
    
        void setXLCellValue(String xlpath,String sheetName,int rowNum,int cellNum, String input)
       {
    
         try{
             FileInputStream fis=new FileInputStream(xlpath);
    
             Workbook wb=WorkbookFactory.create(fis);
    
        wb.getSheet(sheetName).getRow(rowNum).createCell(cellNum).setCellValue(input);
    
        FileOutputStream fos=new FileOutputStream(xlpath);
    
        wb.write(fos);
    
        fos.close();
    
        Log.info("Set the value in cell(setXLCellValue)");
    
    }
    catch(Exception ex)
    {
        Log.info("Error in setXLCellValue ="+ ex.getMessage());
    }
    }