Data Driven Framework:
As the data driven says getting a
input data from the outside here I am getting Username and Password values from
excel sheet and using those values in my main script. The below is the script
for fetching the values from excel sheet.
package
com.Google.util;
import java.io.File;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class ExcelReader {
//
* This Method is used to read the data from XSL File.
public static 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;
for (int i = startRow +
1; i < endRow; i++, ci++) {
cj = 0;
for (int j = startCol +
1; j < endCol; j++, cj++) {
tabArray[ci][cj]
= sheet.getCell(j, i).getContents();
}
}
return (tabArray);
}
}
And the data in the excel looks
like this
LoginGoogle
|
UserName
|
Password
|
|
xxxxx
|
|||
LoginGoogle
|
The description of the parameters
used in the “ExcelReader” function is as below
xlFilePath: The excel file name
along with the location of the file.
sheetName: Name of the excel sheet
tableName: Name of the table, it is the first and last
cell names and both should be same.
Ex: Here in above data table name
is LoginGoogle
The above “ExcelReader”
function with startRow, startCol, endRow and endCol is little bit confusing
hence we can use normal one as shown below for the normal excel table without
table name
package com.Google.util;
import java.io.File;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class ExcelReader
{
//
* This Method is used to read the data from XSL File.
public static String[][]
getTableArray(String xlFilePath, String sheetName) throws Exception
{
String[][]
tabArray = null;
Workbook
workbook = Workbook.getWorkbook(new File(xlFilePath));
Sheet
sheet = workbook.getSheet(sheetName);
int
numRows, numCols, ci, cj;
numRows
= sheet.getRows();
numCols
= sheet.getColumns();
ci
= 0;
for (int i = 0; i <
numRows; i++, ci++)
{
cj
= 0;
for (int j = 0 + 1; j
< numCols; j++, cj++)
{
tabArray[ci][cj]
= sheet.getCell(j, i).getContents();
System.out.print(tabArray[ci][cj]);
}
}
return (tabArray);
}
}
And the data in the excel looks
like this
UserName
|
Password
|
xxxxx
|
|
Create folder as “resource” and
copy the excel sheet and paste under the created folder. After this download
the “jxl.jar” file and copy and paste under the “lib” folder and Set path for
the copied jar file. Right click on Project select Build Path >>
Configure Build Path Select the Add External Jars.. button and navigate to the
location where the jar file is kept and select the file and click on Open
button.
These methods getWorkbook(), getSheet(),
getRows(), getColumns() are part of “jxl.jar” hence we need to use it.
Note: Here the 1st and
last cell concept is not there it’s a normal excel table with column heading
without table name.
As we all know selenium best
suites in agile process and as it is agile there are chances of changing
objects name as project progress. It’s tough to handle the changing objects
because we cannot change each and every time in all the places where and all we
have used these objects.
To resolve this we can even use
external flat files to save our objects or else the .properties file option in
java to save our objects. Using .properties files it’s very easy to handle the
changing objects by updating the changes only once in .properties file.
To create property file right
click on “resource” folder and select file and give the file name as “locators.properties”
and add the objects in to it.
Like
#----------------------------------------------------------------------------
# OBJECT REPOSITORY
#----------------------------------------------------------------------------
#Keyword = value
UN_ID=Email
PW_ID=Passwd
SIGNIN_ID=signIn
Now see the sign in functionality
separated as below
Package name: com.Google.Common
Class name: Signin.Java This class will
enter the values in User name and Password textboxes
package
com.Google.Common;
import org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
com.Google.util.PropertyFileReader;
public class Signin
{
PropertyFileReader
propObj=new
PropertyFileReader();
public void LoginToApp(WebDriver driver, String UserName, String Password)
{
driver.findElement(By.id(propObj.getValue("UN_ID"))).
clear();
driver.findElement(By.id(propObj.getValue("UN_ID"))).
sendKeys(UserName);
driver.findElement(By.id(propObj.getValue("PW_ID"))).
clear();
driver.findElement(By.id(propObj.getValue("PW_ID"))).
sendKeys(Password);
}
}
The main method is as below which
calls all base, signin and signout functions
package
com.Google.Main;
import java.io.File;
import
java.util.regex.Pattern;
import
java.util.concurrent.TimeUnit;
import static
org.hamcrest.CoreMatchers.*;
import
org.openqa.selenium.*;
import
org.openqa.selenium.firefox.FirefoxDriver;
import
org.openqa.selenium.support.ui.Select;
import
org.testng.Assert;
import
org.testng.annotations.AfterSuite;
import
org.testng.annotations.BeforeSuite;
import org.testng.annotations.DataProvider;
import
org.testng.annotations.Test;
import
com.Google.util.PropertyFileReader;
import
com.Google.util.ExcelReader;
import
com.Google.Common.BaseTest;
import
com.Google.Common.SignOut;
import
com.Google.Common.Signin;
public class GoogleLogin extends BaseTest {
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
PropertyFileReader
propObj = new
PropertyFileReader();
@DataProvider(name
= "DP1")
public Object[][] createData()
throws Exception {
/*File dir =
new File("");
String folderPath = dir.getAbsolutePath();*/
Object[][]
retObjArr = ExcelReader.getTableArray
("C:/SeleniumWorkspace/GoogleMailProject/resource/
TestData.xls", "Data", "LoginGoogle");
return (retObjArr);
}
@Test(dataProvider
= "DP1")
public void
testGoogleLogin(String UserName, String Password)
throws Exception {
String
baseUrl = "https://www.google.co.in/";
driver.get(baseUrl);
Assert.assertEquals("Google",
driver.getTitle());
driver.findElement(By.id("gb_70")).click();
Assert.assertEquals("Google
Accounts", driver.getTitle());
// Call common function Signin for
entering User
name and Password
Signin
login = new Signin();
login.LoginToApp(driver, UserName,
Password);
// Click on
Sign in button and validate title
driver.findElement(By.id(propObj.getValue("SIGNIN_ID"))).click();
// Call common
function SignOut for loging out
SignOut
logout = new SignOut();
logout.LoginToApp(driver);
}
}
The overall structure of
automation looks like this
Hybrid Framework: As per the definition of Hybrid Framework it’s
a combination of any two frameworks hence here we have achieved the Hybrid
Framework which is a combination of Modular and Data driven.
No comments:
Post a Comment