Friday, April 10, 2015

Keyword driven Framework for Selenium Webdriver



1. Test Scenario Class:

package com.testScript.case1;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class ZooPageheadingtest {


WebDriver driver;
ExcelFileHandler excelFilehandler;

@BeforeSuite
public void setUp(){

driver = new FirefoxDriver();
excelFilehandler = new ExcelFileHandler();

}
@AfterSuite
public void tearDown(){
driver.quit();
}
@Test
public void TestRun(){

List<List<String>> data = excelFilehandler.readFromFile("C:\\NewBatch\\TestXls.xls");
List<String> results = new ArrayList<String>();
results.add("Results");
for (int i = 1; i < data.size(); i++) {

for (int j = 0; j < data.get(i).size(); j++) {

driver.navigate().to("http://www.thetestroom.com/webapp/");
driver.findElement(By.id(data.get(i).get(j).toLowerCase()+ "_link")).click();

if (driver.getTitle().contains(data.get(i).get(++j))) {
System.out.println(data.get(i).get(j));
results.add("PASS");
}else{
results.add("FAIL");
}

j++;
}
}
excelFilehandler.writeToFile("C:\\NewBatch\\TestXls.xls", results);
System.out.println("Now printing fomr results array");
for (int i = 0; i < results.size(); i++) {
System.out.println(results.get(i));

}
}
}





2.Excel Reader Class
package com.testScript.case1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
public class ExcelFileHandler {

public List<List<String>> readFromFile(String fileName) {

List<List<String>> table = new ArrayList<List<String>>();
try {
FileInputStream file = new FileInputStream(fileName);

HSSFWorkbook workbook = new HSSFWorkbook(file);

HSSFSheet sh = workbook.getSheetAt(0);

Iterator<Row> rowiterator = sh.iterator();

while (rowiterator.hasNext()) {

List<String> rowData = new ArrayList<String>();
Row row = rowiterator.next();
Iterator<Cell> celliterator = row
.cellIterator();
             while(celliterator.hasNext()){
            Cell cell = celliterator.next();
            switch(cell.getCellType()){
             
            case Cell.CELL_TYPE_BOOLEAN:
            rowData.add(String.valueOf(cell.getBooleanCellValue()));
            break;

            case Cell.CELL_TYPE_NUMERIC:
            rowData.add(String.valueOf(cell.getNumericCellValue()));

            case Cell.CELL_TYPE_STRING:
            rowData.add(String.valueOf(cell.getStringCellValue()));
             
            }
             
             }
             table.add(rowData);
}

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return table;
}
public void writeToFile(String fileName,List<String> results){
try {
FileInputStream file = new FileInputStream(fileName);
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt(0);
Cell cell =null;
for (int i = 0; i < results.size(); i++) {
cell = sheet.getRow(i).getCell(2);
cell.setCellValue(results.get(i));
}
file.close();
FileOutputStream updatefile = new FileOutputStream(fileName);
workbook.write(updatefile);
updatefile.close();
} catch (Exception e) {
// TODO: handle exception
}
}

}




Excel File Data:




No comments:

Post a Comment

TestNG - Can i use the 2 different data providers to same @test methods in TestNG?

public Object [][] dp1 () { return new Object [][] { new Object [] { "a" , "b" }, new Obje...