如何使用apache poi selenium webdriver java将excel中的两个或多个值与li中的值进行比较,并在同一个excel中报告pass或fail

时间:2014-12-08 10:41:38

标签: java excel selenium apache-poi

我需要从excel导入2个或更多值并比较我的li中的值,我使用的代码是

FileInputStream input=new FileInputStream(new File("TestCase.xls"));    
HSSFWorkbook workbook=new HSSFWorkbook(input);
HSSFSheet sheet=workbook.getSheet("KeywordFramework");
System.out.println("i am in"); 
int rowNum = sheet.getLastRowNum() + 1;
System.out.println(rowNum);
int colNum = sheet.getRow(0).getLastCellNum();
System.out.println(colNum);
String data [][] = new String[rowNum][colNum];
for(int i =1 ; i< rowNum;i++)       
{
    System.out.println("1");
    HSSFRow row = sheet.getRow(i);
    for(int j = 0; j<= colNum;j++)          
    {
    System.out.println("2");
    HSSFCell cell = row.getCell(j);
    String Cellvalue = cellToString(cell);
    System.out.println("cellvalue === "+Cellvalue);
    switch(j)
    {
   `case 1:
    case 2:
        ExpectedOP=Cellvalue;

        System.out.println("Expected output (value to be compared) = "+ExpectedOP);
        int LastRow = 1;
        List <WebElement> we =  driver.findElements(By.xpath(".//*[@id='stepList']/li"));//Getting list of elements in steplist ul
        for(int i1=2;i1<=we.size();i1++)                
        {
        String sr [] =new String [10];
        sr [i1]=driver.findElement(By.xpath(".//*[@id='stepList']/li["+i1+"]")).getText().trim();//Getting required li text data (example : abc)

        for (String j1:sr)
            {
            if(j1 != null)
            {
            String [] parts = sr [i1].split("/");//Spliting to get the required data
            String parta = parts [0].trim();//Triming the whitespaces
            System.out.println("Step value = "+parta);

            if(ExpectedOP.equals(parta))//Comparing value in Excel with value got in excel
            {
                System.out.println("compare pass");
                String status="PASS";
                [Write into excel];
                break;
            }
             else
            {
                System.out.println("compare pass");
                String status="Fail";
                [Write into excel];
                break;
            }//Close else
            }//Close if j1 null
            }//Close for j1
            }//Close i1 for loop
            }//Close Switch(j)
            }}}//Close the rest`

一旦我得到下一个值,Excel的值就不会增加,也就是说,如果excel的第一个值是abc并且abc与li1中的值(abc和pass)以及i1 for loop进行比较获取li2值作为xyz,但仍然是cellvalue将是abc,我希望单元格值为xyz,以便我可以将单元格的xyz值与li2中的值进行比较

1 个答案:

答案 0 :(得分:0)

请参考下面的示例程序,将excel的值与站点控制台输出进行比较:

    //CALL FIREFOX DRIVER TO OPEN IT
    WebDriver driver = new FirefoxDriver();


    driver.get("http://en.wikipedia.org/wiki/Software_testing");
    String title = driver.getTitle();
    System.out.println(title);

    FileInputStream input = new FileInputStream("D:\\sel.xls");

    int count=0;

    HSSFWorkbook wb = new HSSFWorkbook(input);
    HSSFSheet sh = wb.getSheet("sheet1");
    HSSFRow row = sh.getRow(count);
    String data = row.getCell(1).toString();
    System.out.println(data);


    FileOutputStream webdata = new FileOutputStream ("D:\\sel.xls");
    if(title.equals(data))

    {

        row.createCell(10).setCellValue("TRUE");
        wb.write(webdata);

    }
    else
    {

        row.createCell(11).setCellValue("FALSE");
        wb.write(webdata);

    }

    driver.close(); 
    wb.close();
    input.close();


}


}