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?

3
  • This excel contains only username and password ?? then just use cell.getStringCellValue() no need of these 3 if statements I guess. Commented Feb 23, 2016 at 5: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'-@ᴊᴀᴠʏ Commented Feb 23, 2016 at 5:23
  • check my answer or refer this question How can I read numeric strings in Excel cells as string (not numbers) with Apache POI? Commented Feb 23, 2016 at 5:26

2 Answers 2

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!

Sign up to request clarification or add additional context in comments.

Comments

0

Read everything in string format and then compare

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.