How to create data driven framework using selenium

Selenium Automated software tools on the market supports data driven testing, which allows software to automatically run a tests multiple times with multiple inputs and validations.Selenium is used to automate the software with data driven testing framework where we can use Excel sheets.



Below Script provide details about simple data driven testing using Excel sheet.



Steps:

Download apache POI jar files and add jars to project library in Eclipse

Selenium Script

Selenium Data Driven testing

package com.selenium.pack;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class DataDriven {

    Selenium sel=new DefaultSelenium("localhost",4444,"firefox","https://");
    public void test() throws BiffException, IOException, InterruptedException {
        sel.start();
        sel.open("www.rightstart.com/customer/account/create/");
        sel.windowMaximize();
        Thread.sleep(7000);
        for(int i=1;i<=6;i++) {
            textBox(excelRead(2,i),excelRead(3,i));
        }
        /*textBox(excelRead(2,1),excelRead(3, 1));
        textBox(excelRead(2,2),excelRead(3, 2));
        textBox(excelRead(2,3),excelRead(3, 3));
        textBox(excelRead(2,4),excelRead(3, 4));
        textBox(excelRead(2,5),excelRead(3, 5));
        
        textBox(excelRead(2,6),excelRead(3, 6));*/
    }
    public void textBox(String sprop,String sval){
        if(sprop.startsWith("//")) {
            sel.type(sprop,sval);

        }else{
            sel.type("id="+sprop,sval);
        }
    }
    public String excelRead(int icol,int irow) throws BiffException, IOException {
        FileInputStream fis=new FileInputStream("C:\\Documents and Settings\\righstart.xls");
        Workbook wb=Workbook.getWorkbook(fis);
        Sheet s=wb.getSheet(0);
        Cell a=s.getCell(icol,irow);
        String a1=a.getContents();
        return a1;
    }
    public static void main(String[] args) throws BiffException, IOException, InterruptedException {
        DataDriven dataDriven=new DataDriven();
        dataDriven.test();

    }

}

No comments:

Post a Comment