How To Read Data From Excel


In case of DataDriven Concept,We have to Read Data from Excel sheets.This post helps you to Read Data from Excel files.

This is the Sample Test Data . Please improvise.

Click Here to Download TestData

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

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.firefox.FirefoxDriver;

import jxl.Sheet;
import jxl.Workbook;
public class excelyahoo
{

public static void main(String[] args) throws Exception
{
FileInputStream fs= new FileInputStream(“d:\\sample.xls”);
Workbook wb=Workbook.getWorkbook(fs);
Sheet s=wb.getSheet(0);
for ( int r=1; r<s.getRows(); r++)
{
FirefoxDriver fd =new FirefoxDriver();
fd.get(“http://www.yahoomail.com&#8221;);
Thread.sleep(4000);
fd.findElementByName(“login”).sendKeys(s.getCell(0, r).getContents());
fd.findElementByName(“passwd”).sendKeys(s.getCell(1, r).getContents());
fd.findElementByXPath(“.//*[@id=’pLabelC’]”).click();
fd.findElementByName(“.save”).click();
Thread.sleep(5000);
try {
if (fd.findElementByLinkText(“Sign Out”).isDisplayed());
{
System.out.println(“login is success”);
fd.findElementByLinkText(“Sign Out”).click();
}
}
catch (Exception e)
{
String str= fd.findElementByXPath(“.//*[@id=’fsLogin’]/div/strong”).getText();
System.out.println(str);
File f=fd.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(f, new File(“d:\\yahooSS”+r+”.jpg”));
}
fd.close();

}
}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s