Wednesday 27 February 2013

Write data to excel sheet using selenium

Dudes,

In my previous post i was explained about how to read the data from excel sheet and use those in our selenium script. In this post i will explain about how to write the data to a excel sheet through selenium script.

As known by everyone selenium does n't support read/write data from/to external resources like excel, etc. JXL jar file helps to overcome that issue. Jxl jar helps selenium to read/write the data from/to external sources like excel, etc. I would be explaining how could implement this in this post.

Example: (Google search and write the pass fail status to excel sheet)

package excel;

import com.thoughtworks.selenium.*;
import org.testng.annotations.*;
import java.io.FileOutputStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import jxl.*;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;


public class write2excel{
private Selenium selenium;

@Before
public void startSelenium() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.co.in");
selenium.start();
selenium.windowMaximize();
}


@Test
public void testGoogle1() throws Exception{
String s;
selenium.open("/");
Thread.sleep(3000);
System.out.println("Opened");
selenium.type("id=gbqfq", "Software Testing");
Thread.sleep(3000);
System.out.println("Typed");
selenium.click("id=gbqfb");
Thread.sleep(3000);
System.out.println("Clicked");
if(selenium.isTextPresent("en.wikipedia.org/wiki/Software_testing"))
{
s= "Pass";
}
else
{
s="Fail";
}
write(s);
}

@After
public void tearDown() throws Exception {
//selenium.stop();
}
public void write(String t) {
try
{
FileOutputStream f = new FileOutputStream("src\\Resources\\Data\\data2.xls",true);
      WritableWorkbook book = Workbook.createWorkbook(f);
      WritableSheet sheet = book.createSheet("output", 0);
      Label l = new Label(0, 0, t);
      sheet.addCell(l);
      book.write();
      book.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}

}

In the above example following code used to create new excel file and write the data into excel sheet. And isText Present function helps to verify whether the mentioned text or word or sentence present in loaded page. Using that we can find whether the test case is pass or fail.


FileOutputStream f = new FileOutputStream("src\\Resources\\Data\\data2.xls",true);
       WritableWorkbook book = Workbook.createWorkbook(f);//Creating the excel sheet
       WritableSheet sheet = book.createSheet("output", 0);//Creating work sheet for excel
       Label l = new Label(0, 0, t);
       sheet.addCell(l);//Adding data to cell.
       book.write();
       book.close();

Happy Testing guys.


6 comments:

  1. i would like add multiple data into excel.
    i have added this for loop ,
    like:
    for (i=0;i<=10;i++)
    {
    for (j=0;j<=i;j++)
    {
    Label l = new Label(i,j,t);
    }}
    this is just an example code.
    here the loop is working fine but
    Label l = new Label(i,j,t);

    is failing to add data into exce.
    can someone help me.


    ReplyDelete
    Replies
    1. addCell(l);
      this statement should be added after Label l = new Label(i,j,t);
      and after completing both loops , call write() function using worksheet object
      example :
      Label l1=new Label(0,0,"Appium");
      Label l2=new Label(0,1,"Selenium");
      /*Label l3=new Label(0,2,"QTP");
      ws.addCell(l3);*/
      ws.addCell(l1);
      ws.addCell(l2);
      wwb.write();

      Delete
  2. How to read and write in same excel, instead of creating a new excel for writing. Please help me with java code for selenium wedriver

    ReplyDelete
    Replies
    1. Brother, I am looking for the same code. Kindly email me , if you get the response, i will be visiting. ( Email : samuel.mandala@gmail.com)

      Delete
  3. hi i want to write multiple data in excel sheet from http://www.justdail.com/restaurants/mumbai/ .I want to get all the restaurant name,address,phone number using poi .i tried but i am not getting.can u help?

    ReplyDelete
  4. hii everyone
    how to read the data and write data into the same excel file instead of creating new excel sheet every time...if anyone knows please help me...
    thnaks in advance

    ReplyDelete