December 2, 2010

Write result into another excel sheet

Today i tried some interesting script...with help my colleague(developer- Sricharan).

The aim of this script is to read login credentials from excel sheet and write the credentials and the result in another excel sheet...

Here is the script....

First prepare a excel sheet...with 2 columns usernmae and pwd...




import java.io.FileInputStream;
import java.io.FileOutputStream;

import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.thoughtworks.selenium.*;

import org.openqa.selenium.server.*;
import org.testng.annotations.*;

public class Import_ExportTestNG {
public Selenium selenium;
public SeleniumServer seleniumserver;

@BeforeClass
public void setUp() throws Exception {
RemoteControlConfiguration rc = new RemoteControlConfiguration();
seleniumserver = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://");
seleniumserver.start();
selenium.start();
}

@Test
public void testImport_ExportTestNG() throws Exception {

// Read data from excel sheet
FileInputStream fi = new FileInputStream(
"F:\\Selenium all\\Framework\\testdata\\Login_Credentials1.xls");
Workbook w = Workbook.getWorkbook(fi);
Sheet s = w.getSheet(0);
String a[][] = new String[s.getRows()][s.getColumns()];
// Write the input data into another excel file
FileOutputStream fo = new FileOutputStream(
"F:\\Selenium all\\Framework\\Results\\LoginResult1.xls");
WritableWorkbook wwb = Workbook.createWorkbook(fo);
WritableSheet ws = wwb.createSheet("loginresult1", 0);
selenium.open("http://www.gmail.com");
selenium.windowMaximize();
System.out.println("s.getRows() = " + s.getRows());

for (int i = 0; i < s.getRows(); i++) {
System.out.println("s.getColumns = " + s.getColumns());
for (int j = 0; j < s.getColumns(); j++) {
a[i][j] = s.getCell(j, i).getContents();
Label l = new Label(j, i, a[i][j]);
Label l1 = new Label(2, 0, "Result");
ws.addCell(l);
ws.addCell(l1);
}
}

for (int i = 1; i < s.getRows(); i++)
{
selenium.type("Email", s.getCell(0, i).getContents());
selenium.type("Passwd", s.getCell(1, i).getContents());
selenium.click("signIn");
selenium.waitForPageToLoad("30000");
//if username or pwd is wrong it will execute if block
if (selenium.isTextPresent("The username or password you entered is incorrect. [?]"))
{
Label l3 = new Label(2, i, "fail");
System.out.println("The value of l2 is ::" + l3);
ws.addCell(l3);
System.out.println("Login Failure");
Thread.sleep(10000);

}
//valid username and pwd it willl execute else block
else
{

Label l2 = new Label(2, i, "pass");
System.out.println("The value of l2 is ::" + l2);
ws.addCell(l2);
selenium.click("link=Sign out");
Thread.sleep(10000);
  }
   }
wwb.write();
wwb.close();
  }

@AfterClass
public void tearDown() throws Exception {
selenium.stop();
seleniumserver.stop();

}
}



Hope this will help you while writing results to excel sheet....

3 comments:

  1. Hi...
    I am new to selenium so I don't know where to put this code and how to run it successfully.
    Please help me... How can I run this code?

    Thanks in advance.

    ReplyDelete
  2. can u tell me how to write result in Excel shet using IDE not rc or websdriver . is there any way ?

    ReplyDelete
  3. i have tried to run in eclipse but it is showing an error
    public void testImportexport1() throws Exception
    can you pls. help me out for executing the code

    ReplyDelete