Handling Excel Sheet in Selenium(WebDriver) - Bug Reaper

                  Bug Reaper

Lean about Automation Testing,Selenium WebDriver,RestAssured,Appium,Jenkins,JAVA,API Automation,TestNG,Maven, Rest API, SOAP API,Linux,Maven,Security Testing,Interview Questions

Monday 23 September 2013

Handling Excel Sheet in Selenium(WebDriver)



Download jxl.jar file

import java.io.FileInputStream;
import jxl.Sheet;
import jxl.Workbook;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class WebDriverExcel
{
private WebDriver driver;
@BeforeClass
public void Startup()
{
driver = new FirefoxDriver();
}
@Test 
public void Login() throws Exception
{
FileInputStream fi=new FileInputStream("C:\\Users\\neeraj\\Desktop\\test.xls");
Workbook w=Workbook.getWorkbook(fi);
Sheet s=w.getSheet(0);
{
for (int i = 1; i < s.getRows(); i++)
{
for (int j = 0; j < s.getColumns(); j++)
{
//Read data from excel sheet
String s1 = s.getCell(j,i).getContents();
System.out.println("Usernam"+s1);
String s2 = s.getCell(++j,i).getContents();
driver.findElement(By.id("Email")).sendKeys(s1);
driver.findElement(By.id("Email")).clear();
System.out.println("Password"+s2);
driver.findElement(By.id("Passwd")).sendKeys(s2);
driver.findElement(By.id("Passwd")).clear();
driver.findElement(By.id("signIn")).click();


}
}
}}
}

5 comments:

  1. Good one Neeraj!!
    Keep up the good work!!

    ReplyDelete
    Replies
    1. Thanks Rakesh..!!
      I hope you ppl like the blog and help in Contributing to Tester's Community...:)

      Delete
  2. Thanks Madiraju :)
    Its all for testing Community, join this blog and share the content among friends and testers.

    ReplyDelete

  3. 1.Write Excel + JXL API [Web Driver]
    I want added at time multiple rows and columns.


    EMPID
    EMPNAME
    EMPADDRESS
    10
    WEB
    HYD
    20
    DRIVER
    BANG
    30
    SELENIUM
    CHENNAI








    public static void main(String[] args) throws Exception {



    File f = new File("src/pkg/xyz.xls"); //new Xl sheet create
    FileOutputStream ff = new FileOutputStream(f);
    WritableWorkbook book = Workbook.createWorkbook(ff);
    WritableSheet sheet = book.createSheet("Sheet1", 0); //CreateSheet("SheetName","index")



    Label description = new Label(0, 0, "EMPID"); //Label(ColNumber-1,RowNumber-1,Data)
    sheet.addCell(description);

    Label orderNo = new Label(1, 0, "EMPNAME");
    sheet.addCell(orderNo);

    Label amount = new Label(2, 0, "ADDRESS");
    sheet.addCell(amount);


    book.write();
    book.close();


    }}


    NOTE: I have using addCell one-by-one but,added at time multiple rows and columns.

    ReplyDelete