how to change the number into string from excel sheet using selenium webdriver Im passing the value as 10 in excel sheet but it enter as 10.0 in web application. thanks in advance
-
Are you reading into a string the value from excel?niharika_neo– niharika_neo2014-10-29 12:19:09 +00:00Commented Oct 29, 2014 at 12:19
-
It sounds like passing into. What method are you using to set the value in the cell? Some example code would be very helpful herebcar– bcar2014-10-29 12:35:23 +00:00Commented Oct 29, 2014 at 12:35
-
im reading the text value as 10 from excel sheet.but when it enter into the webelement it type the value as 10.0. also i changed the category as text in excel.Vids– Vids2014-10-29 12:45:45 +00:00Commented Oct 29, 2014 at 12:45
2 Answers
Solution 1
I hope you are using apache poi for reading from Excel. In that case, this sample can help you
CellReference cr = new CellReference("A1");
XSSFRow row = sheet.getRow(cr.getRow());
XSSFCell cell = row.getCell(cr.getCol());
switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_NUMERIC:
System.out.println(cell.getRawValue());
break;
case XSSFCell.CELL_TYPE_BOOLEAN:
System.out.println(cell.getBooleanCellValue());
break;
case XSSFCell.CELL_TYPE_STRING:
System.out.println(cell.getStringCellValue());
break;
default:
System.out.println(cell.getRawValue());
}
For value 1234 in excel cell.getNumericCellValue() will return you 1234.0 and cell.getRawValue() will return 1234
Solution 2
And in case you cant modify anything in the place where you read the Excel, format the String before entering the value using Webdriver
public static void main(String[] args) {
String input = "1234.0";
try {
double parseDouble = Double.parseDouble(input);
DecimalFormat df = new DecimalFormat("0");
String formatNumber = df.format(parseDouble);
System.out.println(formatNumber);
} catch (Exception e) {
System.out.println("Input is not a numeric");
}
}
4 Comments
this input value should be in string,from the excel sheet im getting the integer value, in this need to change the inputvalue into string.if i change inputvalue as integer in this method, sendkeys(inbuild java method)doesnt accept the integer inputvalue.i no need to oddcode in this method.i need to read the value from the excelsheet, and send to this method.
public static void inttypeIn(String objLocator, String inputValue){
try
{
findWebElement(objLocator);
webElement.sendKeys(inputValue);
webElement.sendKeys(Keys.TAB);
APP_LOGS.debug("Typing '" + inputValue + "' in : " + locatorDescription);
}
catch (Exception e)
{
APP_LOGS.debug("FAIL : The locator "+objLocator+" of description '"+locatorDescription+"': does not exists in webpage:");
Reporting.fail("FAIL : The locator '"+objLocator+"' of description '"+locatorDescription+"': does not exists in webpage:");
}
}