Showing posts with label Data driven. Show all posts
Showing posts with label Data driven. Show all posts

December 2, 2010

Write result into another excel sheet

December 02, 2010 3
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....

October 26, 2010

How to do Data Driven Testing using TestNG

October 26, 2010 14
There are many ways to do Data driven tests.I used Excel sheet for reading data and FileInputStream method.

Below script explains you how to Read data from excel sheet and use the data to search google.

I am using Eclipse, selenium RC, TestNG and Excel sheet for this script.


--->Create a Java project in eclipse.
--->Create a new class DatadriventestNG.
--->Paste the below code in Eclipse.
--->Chnage the path of excel file according to your requirement.








Please note that selenium will support only .xls format plese do not forget to change the excel file to .xls if you are using MS-office2007.

Below is the code

package test;

import com.thoughtworks.selenium.*;

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


public class DefaultTNG {
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, "*iexplore", "http://");
seleniumserver.start();
selenium.start();
}

@Test
public void testDefaultTNG()throws Exception {
FileInputStream fi=new FileInputStream("F:\\Framework\\testdata\\search.xls");
Workbook w=Workbook.getWorkbook(fi);
Sheet s=w.getSheet(0);
selenium.open("http://www.google.com");
selenium.windowMaximize();
for (int i = 1; i < s.getRows(); i++)
 { 
//Read data from excel sheet
 selenium.type("name=q",s.getCell(0,i).getContents());
 selenium.click("btnG");
 Thread.sleep(1000); } 
}
@AfterClass
 public void tearDown() throws InterruptedException{
selenium.stop();
seleniumserver.stop();
 
 }
 }



 Please let me know if you need more information regarding Data driven testing....


How to do Data Driven Testing using JUnit

October 26, 2010 37
There are many ways to do Data driven tests.I used Excel sheet for reading data and FileInputStream method.

Below script explains you how to Read data from excel sheet and use the data to search google.

I am using Eclipse, selenium RC, Junit and Excel sheet for this script.


--->Create a Java project in eclipse.
--->Create a new class DatadrivenJUnit.
--->Paste the below code in Eclipse.
--->Chnage the path of excel file according to your requirement.







Please note that selenium will support only .xls format plese do not forget to change the excel file to .xls if you are using MS-office2007.

Below is the code


import java.io.FileInputStream;
import jxl.Sheet;
import jxl.Workbook;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.*;


public class DatadrivenJUnit extends SeleneseTestCase{

public Selenium selenium;
public SeleniumServer seleniumserver;

@BeforeClass
public void setUp() throws Exception {

RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.setSingleWindow(true);
seleniumserver = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://");
seleniumserver.start();
selenium.start();

}

@Test
public void testDatadrivenJUnit() throws Exception
{
FileInputStream fi=new FileInputStream("F:\\Framework\\testdata\\search.xls");
Workbook w=Workbook.getWorkbook(fi);
Sheet s=w.getSheet(0);
selenium.open("http://www.google.com");
selenium.windowMaximize();
for (int i = 1; i < s.getRows(); i++)
 { //Read data from excel sheet
 selenium.type("name=q",s.getCell(0,i).getContents());
 selenium.click("btnG");
 Thread.sleep(1000); } }
@AfterClass
 public void tearDown() throws InterruptedException{
selenium.stop();
 seleniumserver.stop();
}

 }


 Please let me know if you need more information regarding Data driven testing....


An Introduction to Testing Web Applications with twill and Selenium