View and download Facebook_Registration.xls
package com.datadriven.test;
import java.io.FileInputStream;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import jxl.Sheet;
import jxl.Workbook;
import utility.Helper;
public class FBRegDataDrivenFramework {
WebDriver driver;
public List getElements(String locatorName, String locatorValue) {
List elements = null;
switch (locatorName) {
case "name":
elements = driver.findElements(By.name(locatorValue));
break;
case "linkText":
elements = driver.findElements(By.linkText(locatorValue));
break;
case "partialLinkText":
elements = driver.findElements(By.partialLinkText(locatorValue));
break;
case "className":
elements = driver.findElements(By.className(locatorValue));
break;
case "xpath":
elements = driver.findElements(By.xpath(locatorValue));
break;
case "css":
elements = driver.findElements(By.cssSelector(locatorValue));
break;
case "tagName":
elements = driver.findElements(By.tagName(locatorValue));
break;
}
return elements;
}
public WebElement getElement(String locatorName, String locatorValue) {
WebElement element = null;
switch (locatorName) {
case "id":
element = driver.findElement(By.id(locatorValue));
break;
case "name":
element = driver.findElement(By.name(locatorValue));
break;
case "linkText":
element = driver.findElement(By.linkText(locatorValue));
break;
case "partialLinkText":
element = driver.findElement(By.partialLinkText(locatorValue));
break;
case "className":
element = driver.findElement(By.className(locatorValue));
break;
case "xpath":
element = driver.findElement(By.xpath(locatorValue));
break;
case "css":
element = driver.findElement(By.cssSelector(locatorValue));
break;
case "tagName":
element = driver.findElement(By.tagName(locatorValue));
break;
case "text":
element = driver.findElement(By.xpath("//*[text()='" + locatorValue + "']"));
break;
}
return element;
}
public void performTask(WebElement element, String type, String... text) {
switch (type) {
case "text":
element.click();
element.clear();
element.sendKeys(text[0]);
break;
case "select":
Select select = new Select(element);
select.selectByVisibleText(text[0]);
break;
case "button":
element.click();
break;
case "link":
element.click();
break;
case "checkbox":
element.click();
break;
case "radiobutton":
element.click();
break;
}
}
@BeforeMethod
public void beforeRegistrationTest(){
driver = Helper.launchBrowser("chrome");
driver.get("http://facebook.com");
}
@Test(dataProvider = "regData")
public void facebookLogin(String fn, String sn, String email,
String re_enter, String pass, String day, String month,
String year, String gender) throws Exception {
String locatorName, locatorValue;
WebElement element;
FileInputStream fis = new FileInputStream("Facebook_Registration.xls");
Workbook wb = Workbook.getWorkbook(fis);
Sheet sht = wb.getSheet("Locators");
locatorName = sht.getCell(0, 0).getContents();
locatorValue = sht.getCell(0, 1).getContents();
element = getElement(locatorName, locatorValue);
performTask(element, "text", fn);
locatorName = sht.getCell(1, 0).getContents();
locatorValue = sht.getCell(1, 1).getContents();
element = getElement(locatorName, locatorValue);
performTask(element, "text", sn);
locatorName = sht.getCell(2, 0).getContents();
locatorValue = sht.getCell(2, 1).getContents();
element = getElement(locatorName, locatorValue);
performTask(element, "text", email);
locatorName = sht.getCell(3, 0).getContents();
locatorValue = sht.getCell(3, 1).getContents();
element = getElement(locatorName, locatorValue);
performTask(element, "text", re_enter);
locatorName = sht.getCell(4, 0).getContents();
locatorValue = sht.getCell(4, 1).getContents();
element = getElement(locatorName, locatorValue);
performTask(element, "text", pass);
locatorName = sht.getCell(5, 0).getContents();
locatorValue = sht.getCell(5, 1).getContents();
element = getElement(locatorName, locatorValue);
performTask(element, "select", day);
locatorName = sht.getCell(6, 0).getContents();
locatorValue = sht.getCell(6, 1).getContents();
element = getElement(locatorName, locatorValue);
performTask(element, "select", month);
locatorName = sht.getCell(7, 0).getContents();
locatorValue = sht.getCell(7, 1).getContents();
element = getElement(locatorName, locatorValue);
performTask(element, "select", year);
element = getElement("text", gender);
performTask(element, "button");
}
@AfterMethod
public void afterRegistrationTest() throws InterruptedException{
Thread.sleep(5000);
driver.close();
}
@DataProvider(name = "regData")
public Object[][] testData() throws Exception {
Object[][] data = null;
FileInputStream fis = new FileInputStream("Facebook_Registration.xls");
Workbook wb = Workbook.getWorkbook(fis);
Sheet sht = wb.getSheet("Registration");
int rows = sht.getRows(),
col = sht.getColumns();
System.out.println("No of Rows: " + rows + "\tNo of Columns: " + col);
data = new Object[rows-1][col];
for (int i = 0; i < rows - 1; i++) {
for (int j = 0; j < col; j++) {
data[i][j] = sht.getCell(j, i + 1).getContents();
System.out.print(data[i][j] + " ");
}
System.out.println();
}
return data;
}
}
import java.io.FileInputStream;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import jxl.Sheet;
import jxl.Workbook;
import utility.Helper;
public class FBRegDataDrivenFramework {
WebDriver driver;
public List
List
switch (locatorName) {
case "name":
elements = driver.findElements(By.name(locatorValue));
break;
case "linkText":
elements = driver.findElements(By.linkText(locatorValue));
break;
case "partialLinkText":
elements = driver.findElements(By.partialLinkText(locatorValue));
break;
case "className":
elements = driver.findElements(By.className(locatorValue));
break;
case "xpath":
elements = driver.findElements(By.xpath(locatorValue));
break;
case "css":
elements = driver.findElements(By.cssSelector(locatorValue));
break;
case "tagName":
elements = driver.findElements(By.tagName(locatorValue));
break;
}
return elements;
}
public WebElement getElement(String locatorName, String locatorValue) {
WebElement element = null;
switch (locatorName) {
case "id":
element = driver.findElement(By.id(locatorValue));
break;
case "name":
element = driver.findElement(By.name(locatorValue));
break;
case "linkText":
element = driver.findElement(By.linkText(locatorValue));
break;
case "partialLinkText":
element = driver.findElement(By.partialLinkText(locatorValue));
break;
case "className":
element = driver.findElement(By.className(locatorValue));
break;
case "xpath":
element = driver.findElement(By.xpath(locatorValue));
break;
case "css":
element = driver.findElement(By.cssSelector(locatorValue));
break;
case "tagName":
element = driver.findElement(By.tagName(locatorValue));
break;
case "text":
element = driver.findElement(By.xpath("//*[text()='" + locatorValue + "']"));
break;
}
return element;
}
public void performTask(WebElement element, String type, String... text) {
switch (type) {
case "text":
element.click();
element.clear();
element.sendKeys(text[0]);
break;
case "select":
Select select = new Select(element);
select.selectByVisibleText(text[0]);
break;
case "button":
element.click();
break;
case "link":
element.click();
break;
case "checkbox":
element.click();
break;
case "radiobutton":
element.click();
break;
}
}
@BeforeMethod
public void beforeRegistrationTest(){
driver = Helper.launchBrowser("chrome");
driver.get("http://facebook.com");
}
@Test(dataProvider = "regData")
public void facebookLogin(String fn, String sn, String email,
String re_enter, String pass, String day, String month,
String year, String gender) throws Exception {
String locatorName, locatorValue;
WebElement element;
FileInputStream fis = new FileInputStream("Facebook_Registration.xls");
Workbook wb = Workbook.getWorkbook(fis);
Sheet sht = wb.getSheet("Locators");
locatorName = sht.getCell(0, 0).getContents();
locatorValue = sht.getCell(0, 1).getContents();
element = getElement(locatorName, locatorValue);
performTask(element, "text", fn);
locatorName = sht.getCell(1, 0).getContents();
locatorValue = sht.getCell(1, 1).getContents();
element = getElement(locatorName, locatorValue);
performTask(element, "text", sn);
locatorName = sht.getCell(2, 0).getContents();
locatorValue = sht.getCell(2, 1).getContents();
element = getElement(locatorName, locatorValue);
performTask(element, "text", email);
locatorName = sht.getCell(3, 0).getContents();
locatorValue = sht.getCell(3, 1).getContents();
element = getElement(locatorName, locatorValue);
performTask(element, "text", re_enter);
locatorName = sht.getCell(4, 0).getContents();
locatorValue = sht.getCell(4, 1).getContents();
element = getElement(locatorName, locatorValue);
performTask(element, "text", pass);
locatorName = sht.getCell(5, 0).getContents();
locatorValue = sht.getCell(5, 1).getContents();
element = getElement(locatorName, locatorValue);
performTask(element, "select", day);
locatorName = sht.getCell(6, 0).getContents();
locatorValue = sht.getCell(6, 1).getContents();
element = getElement(locatorName, locatorValue);
performTask(element, "select", month);
locatorName = sht.getCell(7, 0).getContents();
locatorValue = sht.getCell(7, 1).getContents();
element = getElement(locatorName, locatorValue);
performTask(element, "select", year);
element = getElement("text", gender);
performTask(element, "button");
}
@AfterMethod
public void afterRegistrationTest() throws InterruptedException{
Thread.sleep(5000);
driver.close();
}
@DataProvider(name = "regData")
public Object[][] testData() throws Exception {
Object[][] data = null;
FileInputStream fis = new FileInputStream("Facebook_Registration.xls");
Workbook wb = Workbook.getWorkbook(fis);
Sheet sht = wb.getSheet("Registration");
int rows = sht.getRows(),
col = sht.getColumns();
System.out.println("No of Rows: " + rows + "\tNo of Columns: " + col);
data = new Object[rows-1][col];
for (int i = 0; i < rows - 1; i++) {
for (int j = 0; j < col; j++) {
data[i][j] = sht.getCell(j, i + 1).getContents();
System.out.print(data[i][j] + " ");
}
System.out.println();
}
return data;
}
}
No comments:
Post a Comment