·
Perquisites for automation frame work.
·
What are all needed for designing automation
framework?
Folder Structure for Automation frame
work.
1.
Create a Java Project and
add the library files into JRE System Library.
The folder structure will be.
a.
Test Util.Java
b.
Test Data
c.
OR. Properties.
d.
Tests(or Classes).java
e.
Driver Script. Java
f.
Library files
g.
testing.xml
h.
Screen shots.
i.
Test output
Now we will discuss about the frame work how to design
and how to work with framework.
Before that we will discuss about each and
every packages (Each script file).
a.
Library Files which need to
add...
Note: No need to download each and every jar.
Just download the below jars which contains all from the below given link.
Link : Seleniumhq.org/Download/
b.
Test Util.java:
--- This package
is mainly deals with utilities. like..
·
Initialize.
·
get Object.
·
waitForElementPopulate
·
WaitForElementPresent
·
waitFor5Seconds
·
typeData
·
getExcelData
·
take Screenshot
·
test Login
·
selectFromDropdown
·
Initialize. In initialization we need to read the property
file data. Like below
public static void intialize() throws IOException {
try {
props = new Properties();
fis = new
FileInputStream(System.getProperty("user.dir")+"\\src\\OR.properties");
System.out.println(fis);
props.load(fis);
}
catch
(FileNotFoundException e) {
e.printStackTrace();
}
}
****** here we have to create an instance for
properties and read through file input stream and load the props file .
·
get Object.
Get object concept will use in the test
scripts. Like
public WebElement getObject(String xpathkey)
throws IOException {
try {
return driver.findElement(By.xpath(props.getProperty(xpathkey)));
}
catch (Throwable t) {
System.out.println("error:\t" + xpathkey);
return null;
}
}
Here we have to use this method as
getObject(“Xpath given in the property
file”).click();
getObject(“Xpath”).getTitle();
getObject(“Xpath”).getText();...etc...in
the test scripts
·
waitForElementPopulate : this method will use
in case of any
public void
waitForElementPopulate(String xpathkey) throws Exception {
int i = 0;
String
expdata = null;
while (i <= 20) {
System.out.println(expdata + "\t" + i);
Thread.sleep(2000L);
new Actions(driver).moveToElement(getObject(xpathkey)).click().perform();
getObject(xpathkey).click();
Thread.sleep(2000L);
expdata
= getObject(xpathkey).getText().trim();
System.out.println(expdata);
if (expdata != null) {
System.out.println(expdata);
break;
}
else {
Thread.sleep(500L);
i++;
}
}
}
Ex:
waitForElementPopulate(“xpathkey”);
·
WaitForElementPresent: It
will wait for Element present in the Web page. Until the web element present in
the web page it will wait.
public WebElement WaitForElementPresent(String xpathkey) throws Exception {
WebElement
present = null;
int i = 0;
while (i <= 10) {
Thread.sleep(2000L);
present
= getObject(xpathkey);
if (present != null) {
break;
}
else {
System.out.println("i is: "+i);
i++;
}
}
return present;
}
Ex: WaitForElementPresent(“propertyfilexpath”);
·
WaitFor5Seconds: it will wait for 5 second to execute a single line or
command.
public void waitFor5Seconds() throws Exception {
int i = 0;
while (i <= 5) {
Thread.sleep(1000L);
System.out.println("i is: "+i);
i++;
}
}
Ex: waitFor5Seconds();
·
TypeData : this will help you to enter the data in Text
field.
public void typeData(String
xpathkey, String value) throws Exception {
try {
getObject(xpathkey).sendKeys(value);
}
catch (Exception t) {
t.printStackTrace();
}
}
Ex:
TypeData(“Xpath from propertyfile”,”Value you want to enter into text field”);
·
GetExcelData(“Xpath”,”SheetName”,”TableName1”,”Tablename2”);
This method will read the excel data and
send the data into text fields in the application.
public String[][]
getExcelData(String xlPath, String shtName, String tbName, String tbName1) throws Exception{
String[][] tabArray=null;
Workbook workbk = Workbook.getWorkbook(new File(xlPath));
Sheet sht = workbk.getSheet(shtName);
int sRow,sCol, eRow, eCol,ci,cj;
Cell tableStart=sht.findCell(tbName);
sRow=tableStart.getRow();
sCol=tableStart.getColumn();
Cell tableEnd= sht.findCell(tbName1);
eRow=tableEnd.getRow();
eCol=tableEnd.getColumn();
System.out.println("startRow="+sRow+",
endRow="+eRow+", " + "startCol="+sCol+", endCol="+eCol);
tabArray=new
String[eRow-sRow-1][eCol-sCol-1];
ci=0;
for (int i=sRow+1;i<eRow;i++,ci++){
cj=0;
/*System.out.println("Row"+i);
System.out.println("Column"+sCol);*/
for (int j=sCol+1;j<eCol;j++,cj++){
/*System.out.println("Row1"+i);
System.out.println("Column1"+j);*/
tabArray[ci][cj]=sht.getCell(j,i).getContents();
}
}
return(tabArray);
}
·
takeScreenShot
: This will take the screen shot where
you mentioned the below line of code.
public void takeScreenShot(String fileName,String
folderName,String Modname ) throws IOException
{
File
scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(System.getProperty("user.dir")+"//screenshots//"+folderName+"//"+Modname+"//"+fileName+".png"));
}
Ex:
takeScreenShot(“Filename”,”folder”,”Modname”);
·
TestLogin: this method useful to direct login to the
application.
public void testLogin(String
UserName, String Password) throws Exception {
if (getObject("login.username.label") != null && getObject("login.pwd.label") != null) {
typeData("en.distributor.UName", UserName);
typeData("en.distributor.Pwd", Password);
getObject("en.distributor.LgBtn").submit();
System.out.println("Clicked on
Login button");
waitFor5Seconds();
}
}
Ex: testLogin(UserName, Password); or
testLogin(“Admin”, “111”);
·
SelectfromDropDown: this will select an option from drop down list.
public void
selectFromDropdown(String xpathkey, String value) throws Exception{
/*waitFor5Seconds();*/
WebElement
table6 = getObject(xpathkey);
List<WebElement>
tds6 = table6.findElements(By.tagName("option"));
for (WebElement option :
tds6) {
if
(option.getText().equals(value)) {
option.click();
}
}
So far we discussed about TestUtil file....
Now we are
going to discuss about the test data.
2.
Test Data :
This is an excel file which contains
Test data to test the application. Please find the sample test data file.
·
GetExcelData(“Location”,”SheetName”,”TableName1”,”Tablename2”);
3.
OR. Properties.
This is very crucial concept need to concentrate . because the
properties are stored in the Property file. I will show you the sample.
4.
Tests(.Java files)
We will check with sample script
for Gmail Login.
package Gmail;
import
org.openqa.selenium.firefox.FirefoxDriver;
import
org.testng.annotations.BeforeClass;
import
org.testng.annotations.DataProvider;
import
org.testng.annotations.Test;
import TestUtil.TestUtil;
public class GmailLogin extends TestUtil
{
public String location = "path of Xls
file";
public String sheetname1 = "GmailLogin";
public String Baseurl = "http://mail.google.com";
@BeforeClass
public void setUp(){
driver= new FirefoxDriver();
driver.manage().window().maximize();
driver.get(Baseurl);
}
@Test(dataProvider="dp")
public void GLogin(String
UserName,String Password) throws Exception{
// enter the username
and password.
testLogin(UserName,
Password);
waitFor5Seconds();
if(getObject("login.login_Btn")!=null){
WaitForElementPresent("login.login_Btn");
//Click on login
button
getObject("login.login_Btn").click();
}else{
takeScreenShot("GmailLogin","Gmailtest", "Gmail");
}
}
@DataProvider(name = "dp")
public Object[][] createData() throws Exception {
Object[][]
retObjArr = getExcelData(location, sheetname1,
"GmailLogin", "GmailLogout");
return (retObjArr);
}
}
5.
TestNg.xml
Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing
ReplyDeleteSelenium with Java Online Training
Selenium Online Training India