0

I am trying to develop a data driven Selenium automation framework in Java. I have written code to read input test data from excel sheet. The excel sheet contains two columns - Username and Password. I read the excel data using the following code.

String testData;
for(int j=1;j<currentRow.getPhysicalNumberOfCells();j++){
    if(currentRow.getCell(j)!==null)
        {
         if(currentRow.getCell(j).getCellType()==Cell.CELL_TYPE_BOOLEAN){
          testData=Boolean.toString(currentRow.getCell(j).getBooleanCellValue());
            }
            if(currentRow.getCell(j).getCellType()==Cell.CELL_TYPE_NUMERIC){
                testData=Double.toString(currentRow.getCell(j).getNumericCellValue());                      
            }
            if(currentRow.getCell(j).getCellType()==Cell.CELL_TYPE_STRING){
                testData=currentRow.getCell(j).getStringValue();
            }
        }

    }

The problem is that if the password is 123, the above code will return the value 123.0 and hence the test case fails. I cannot remove the decimal point since if the actual password is 123.0, it would return the result 123. How can I read the excel data as it is given in the cell?

stackoverflow
  • 2,134
  • 3
  • 19
  • 35
  • This excel contains only username and password ?? then just use `cell.getStringCellValue()` no need of these 3 if statements I guess. – Arun Xavier Feb 23 '16 at 05:14
  • Username and password can be just numbers, so if I avoid other statements, it throws 'IllegalStateException: Cannot get a error value from a numeric cell'-@ᴊᴀᴠʏ – stackoverflow Feb 23 '16 at 05:23
  • check my answer or refer this question [How can I read numeric strings in Excel cells as string (not numbers) with Apache POI?](http://stackoverflow.com/questions/1072561/how-can-i-read-numeric-strings-in-excel-cells-as-string-not-numbers-with-apach) – Arun Xavier Feb 23 '16 at 05:26

2 Answers2

2

add cell.setCellType(Cell.CELL_TYPE_STRING); before starting to read.

EDIT : POI API Documentation says,

If what you want to do is get a String value for your numeric cell, stop!. This is not the way to do it. Instead, for fetching the string value of a numeric or boolean or date cell, use DataFormatter instead.

refer This answer for a better solution!

Community
  • 1
  • 1
Arun Xavier
  • 763
  • 8
  • 47
0

Read everything in string format and then compare

Ashish
  • 101
  • 4