Search code examples
javaseleniumselenium-webdriverapache-poitestng

Cannot get a STRING value from a NUMERIC cell?


I Was Executing Test Scripts Though Excel Sheet Using Keyword Driven Data Framework.

While Executing Am Geting Error as :

java.lang.RuntimeException: java.lang.IllegalStateException: Cannot get a STRING value from a NUMERIC cell

My Selenium Code is

    package DemoData;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import DataConfig.ExcelDataConfig;

public class ReadArrayData 
{

    WebDriver driver;


  @Test(dataProvider="Tipreports")
  public void Dataprovider(String username, String password) throws InterruptedException
  {
      System.setProperty("webdriver.chrome.driver", "F:\\New folder\\chromedriver.exe");

      driver = new ChromeDriver();

      driver.get("http://xxxxxxxxxxxxxxxx/");

      driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

      driver.manage().window().maximize();

      driver.findElement(By.id("txt_username")).sendKeys(username);

      driver.findElement(By.id("txt_pin")).sendKeys(password);

      driver.findElement(By.xpath("//*[@id='btn_click']")).click();

      Thread.sleep(5000);

      //driver.getCurrentUrl();

      Assert.assertTrue(driver.getCurrentUrl().contains("http://xxxxxxxxxxxxx/Algorithm_Run.aspx"),"User Not able to Login - Invalid Credentials");   

  }

  @AfterMethod
  public void Close()
  {
      driver.quit();
  }

  @DataProvider(name="Tipreports")
  public Object[][] passdata()
  {

      ExcelDataConfig config = new ExcelDataConfig("E:\\Workspace\\KeywordFramework\\TestData\\Dataprovider.xlsx");
         int rows = config.getRowCount(0);

         Object[][] data = new Object[rows][2];

         for(int i=0;i<rows;i++)
         {
             data[i][0]=config.getData(0, i, 0);
             data[i][1]=config.getData(0, i, 1);

         }
        return data;

  }

}

Here is My Excel Data Config Code :

package DataConfig;

import java.io.File;
import java.io.FileInputStream;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelDataConfig 
{
    XSSFWorkbook wb;
    XSSFSheet Sheet1;

    public ExcelDataConfig(String excelpath)
    {
        try
        {
            File src = new File(excelpath);
            FileInputStream fis = new FileInputStream(src);
            wb= new XSSFWorkbook(fis);
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }

    public String getData(int sheetnumber, int row, int column)
    {
        Sheet1 = wb.getSheetAt(sheetnumber);

        String data =Sheet1.getRow(row).getCell(column).getStringCellValue();

        return data;

    }

    public int getRowCount(int sheetindex)
    {
        int row = wb.getSheetAt(sheetindex).getLastRowNum();

        row = row+1;

        return row;
    }

}

Can you please help me to sort out this, Why am getting this error? I have declared 2 Columns and 4 Rows using for loop. But am getting cannot Get The error as cannot get the string value from Numeic Cell.


Solution

  • Try this:

    String data;
    
    if(cell.getCellType()==CellType.STRING) 
        data = cell.getStringCellValue(); 
    else if(cell.getCellType()==CellType.NUMERIC) 
        data = String.valueOf(cell.getNumericCellValue());
    else ...
    

    It is a better way to retrieve the cell content as text.

    Thanks to @AxelRichter, the manual show a more complete example to retrieve cell content: https://poi.apache.org/components/spreadsheet/quick-guide.html#CellContents