Selenium / WebDriver code stops running after reading worksheet with Apache Poi

1

My test code using Selenium / WebDriver stops running after reading Apache Poi data.

Can anyone tell me why?

//poi
WebElement searchbox = driver.findElement(By.name("simcard"));

try {

    FileInputStream file = new FileInputStream(new File("C:\paulo.xls")); 
    HSSFWorkbook workbook = new HSSFWorkbook(file);

    HSSFSheet sheet = workbook.getSheetAt(0);

    for (int i=1; i <= sheet.getLastRowNum(); i++){
        String simcard = sheet.getRow(i).getCell(0).getStringCellValue();
        searchbox.sendKeys(simcard);                
        searchbox.submit();
        driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);
    }

    workbook.close();
    file.close();

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

The next step:

driver.findElement(By.xpath("/html/body/table/tbody/tr[4]/td/div/form/table/tbody/tr[2]/td/center/table/tbody/tr[19]/td/a[1]/img")).click();
    
asked by anonymous 08.05.2015 / 16:25

1 answer

2

Based on my understanding of what you want to do and some assumptions, this would be the code that accomplishes the goal:

//referência ao campo de busca
WebElement searchbox = driver.findElement(By.name("simcard"));

try {

    //carrega arquivo com planilha
    FileInputStream file = new FileInputStream(new File("C:\paulo.xls")); 
    HSSFWorkbook workbook = new HSSFWorkbook(file);

    //recupera primeira planilha (aba)
    HSSFSheet sheet = workbook.getSheetAt(0);
    //pega o valor da primeira célula (A1)
    String simcard = sheet.getRow(0).getCell(0).getStringCellValue();

    //fecha planilha e arquivo (não precisa fechar os dois)
    workbook.close();

    //envia o valor da célula para o campo
    searchbox.sendKeys(simcard);

    //envia o formulário (precisa ser um form normal, se a tela usar JavaScript pode não funcionar)
    searchbox.submit();

    //aguarda 10 segundos (esperar carregar os dados, talvez)
    driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);

} catch (Exception e) {
    e.printStackTrace();
}

//próximo passo aqui

Comments:

  • You do not need to close both workbook and file. When you close the first, the second is closed automatically.
  • You do not need a loop to get value. And the first line starts with 0 and not with 1 .
  • You can close the spreadsheet shortly after reading the value.
  • If it still does not work, check the console or log to see if an exception is occurring. In this case, edit your question and add the error stack.
  • If the program crashes it can be a problem with the WebDriver with your browser. In this case, update the Selenium / WebDriver version, try another browser, check if there is a JavaScript running something on the page that can cause the lock.
08.05.2015 / 17:52