RE: Hii frnds….i need info about getting data from excel…how to get numeric data from excel???

Answered

I m unable to get numeric data example mobile number from excel n pass that value to some field…I used getnumericcellvalue() method to fetch the data…as send keys(char sequence) accepts char sequence I m unable to get neric data…m getting exception….pls do help me to sort this prblm…

Regards
Pradeep

Pradeep Train Asked on December 10, 2014 in Selenium WebDriver.
Add Comment
5 Answers

Hi Pradeeep,
Here is the complete solution for your problem.


Util Class to read data from excel file

package com.syner.poi;
import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.ss.usermodel.Sheet;

import org.apache.poi.ss.usermodel.Workbook;

import org.apache.poi.ss.usermodel.WorkbookFactory;
public class Util {

public static Object[][] readExcelData() throws InvalidFormatException, IOException

{

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

InputStream f=new FileInputStream("GmailTestData.xlsx");

Workbook workbook=WorkbookFactory.create(f);

Sheet sheet=workbook.getSheetAt(0);

int rc=sheet.getLastRowNum();

for(int i=0;i<rc;i++)

{

Row r=sheet.getRow(i+1);

int cc=r.getLastCellNum();

for(int j=0;j<cc;j++)

{

Cell c=r.getCell(j);

if(c.getCellType()==c.CELL_TYPE_STRING)

{

data[i][j]=c.getStringCellValue();

}

else if(c.getCellType()==c.CELL_TYPE_NUMERIC)

{

//data[i][j]=c.getNumericCellValue();

data[i][j]=new Long((long) c.getNumericCellValue());

}

System.out.println("Data: "+data[i][j]);
}

}
return data;
}
}
ExcelFile class to get data from Util class and execute your test cases.

package com.syner.poi;
import java.io.IOException;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.DataProvider;

import org.testng.annotations.Test;
public class ExcelFile {

WebDriver driver;
@DataProvider(name="excelData")

public Object[][] readData() throws InvalidFormatException, IOException

{

return Util.readExcelData();

}
@BeforeMethod

public void setUp()

{

driver=new FirefoxDriver();

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

driver.get("https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ltmpl=default");

}
@Test(dataProvider="excelData")

public void testApp(String firstName,long mobileNumber)

{

driver.findElement(By.xpath(".//*[@id='FirstName']")).sendKeys(firstName);

driver.findElement(By.xpath(".//*[@id='RecoveryPhoneNumber']")).sendKeys(""+mobileNumber);

}
@AfterMethod

public void tearDown() throws InterruptedException

{

Thread.sleep(3000);

driver.close();

}
}

In this example I used google singup page.
I hope this is useful for you, If you have any doubts regarding the problem, please let me know.
Thnaks & Regards
Samba.

Train Answered on December 14, 2014.

Hi samba..
Very gud morning..
Hey samba m bit unfamiliar with the @dataprovider..vl u give me how it works n all….
u know m using POM(Page Object Model) Framework….hope u used some other Framework itseems….

any how thank u very much for ur valuable time….n thanks fr helping me…..m bigginner in selenium so ur help vl b valuable….Thanks alot

on December 15, 2014.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.