Thursday, October 13, 2011

Data driven testing with Selenium

Data Driven Testing with Selenium:


Testing multiple data with same script.

the main intension of this testing is to find the behaviour 
of the application for different input values.

When i am executing my Test Cases which are same action up to some milestone. so I am are doing the same steps with different data... then I thought why should i do like this and i thought the data driven, how to do..first i thought for reusing the code.. then i created a class file then i am using the same class for all test cases in the class i am taking the values from xl file..

then i tried with java...then its working fine now..


package com.org.selected.datadriven;

import com.thoughtworks.selenium.*;
import java.io.File;
import java.sql.Time;
import java.util.regex.Pattern;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Blank;

import org.openqa.selenium.internal.seleniumemulation.Click;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;


public class Datadriventest_firefox36 extends SeleneseTestCase {


Selenium selenium;
public static final String MAX_WAIT_TIME_IN_MS="30000";
private SeleniumServer seleniumServer;



@BeforeTest
public void setUp() throws Exception {

File template = new File("C:\\Documents and Settings\\username\\Application Data\\Mozilla\\Firefox\\Profiles\\Selenium");
RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.setFirefoxProfileTemplate(template);
seleniumServer = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "URL");
seleniumServer.start();
selenium.start();
selenium.open("URL");
selenium.windowFocus();
selenium.windowMaximize();
}


@DataProvider(name = "Test")
// Test is hte xl file name, sheet1 is the sheet name and test1 is the table name.
public Object[][] createPayId() throws Exception{
    Object[][] retObjArr=getTableArray("D:\\workspace\\DemoProject\\datafiles\\Test.xls", "sheet1", "test1"); // what is these two ( sheet names )
    return(retObjArr);
}


@Test (dataProvider = "Test", description="Testing ")
public void Testpayments(String col1, String col2, String col3 )throws Exception {
selenium.setSpeed("500");
selenium.select("name=sn", xxx );
    pause(3000);
verifyTrue(selenium.isElementPresent("name=display"));
verifyTrue(selenium.isElementPresent("id=userid"));
verifyTrue(selenium.isElementPresent("name=rand_userid"));
selenium.select("name=aaa", col1);
selenium.select("name=livemode", col2);
selenium.type("id=amount", col3);
    selenium.click("id=Update");
pause(3000);

}

public String[][] getTableArray(String xlFilePath, String sheetName, String tableName) throws Exception{
    String[][] tabArray=null;
 
        Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));
        Sheet sheet = workbook.getSheet(sheetName);
        int startRow,startCol, endRow, endCol,ci,cj;
        Cell tableStart=sheet.findCell(tableName);
        startRow=tableStart.getRow();
        startCol=tableStart.getColumn();


Cell tableEnd= sheet.findCell(tableName, startCol+1,startRow+1, 100, 64000,  false);              
        endRow=tableEnd.getRow();
        endCol=tableEnd.getColumn();
        System.out.println("startRow="+startRow+", endRow="+endRow+", " +
                "startCol="+startCol+", endCol="+endCol);
        tabArray=new String[endRow-startRow-1][endCol-startCol-1];
        ci=0;








1 comment: