Tuesday, July 14, 2015

Selenium Webdriver - Keyword driven framework Skelton

                Framework made easy – KeyWord driven                               
                       Frameworks help to structure our code and make maintenance easy. Without frameworks we will place all our code and data in same place which is neither re-usable nor readable. Using Frameworks, produce beneficial outcomes like increase code re-usage , higher portability , reduced script maintenance cost etc
There are mainly three type of frameworks created by Selenium WebDriver to automate manual testcases
·         Data Driven Test Framework
·         Keyword Driven Test Framework
·         Hybrid Test Framework
Keyword Driven Test Framework:

What is Keyword Driven Framework?

Keyword Driven Framework is a type of Functional Automation Testing Framework which is also known as Table-Driven testing or Action Word based testing. The basic working of the Keyword Driven Framework is to divide the Test Case in to four different parts. First is called as Keyword, second is Object of Test Step, third is objectType on Test Object and fourth is Value for Test Object.

The above categorization can be done and maintained with the help of Excel spread sheet:

Keyword: It is a scenario which we need to set text or Click link or button.
Ex: if my scenario is navigate to certain URL then my keyword will be like: GOTOURL
If my Scenario is enter some data in text box then Keyword is: SETTEXT.
 The keyword is our own imagination, it’s not mandatory to choose the particular one, but whatever we choose the Keyword we need the same in Entire framework.

Object: it is Object repository name
Ex: username text box properties is id=UserName.
Here, username=UserName
Username is nothing but we are specifying own name for Object name given.
UserName is a property type

ObjectType: It is the Object name whether it is Id,name,xpath,CSS,etc…

Value: Data can be any value which is needed by the Object to perform any action, like Username value for Username field.
        
                               In keyword driven test framework, all the operations and instructions are written in some external file like excel worksheet. Here is how the complete framework looks like
As you can see it's a 5 step framework. Let's study it stepwise in detail
Step 1)
·         The driver script Execute.java will call ReadExcelFile.java
·         ReadExcelFile.java has POI script to read data from an Excel

Step 2)
·         ReadExcelFile.java will read data from TestCase.xlsx
·         Here is how the sheet looks like-
·         According to the keywords written in excel file, the framework will perform the operation on UI.
·         For example, we need to click a button 'Login'. Correspondingly, our excel will have a keyword 'Click'. Now the AUT can have hundreds of button on a page, to identify a Login button, in excel we will input Object Name as loginButton & object type as name (see highlighted row in above image). The Object Type could be xpath,name css or any other value

Step 3) ReadExcelFile.java will pass this data to the driver script Execute.java
Step 4)
·         For all of our UI web elements we need to create an object repository where we will place their element locator (like xpath, name, css path ,class name etc.)
·          

·         Execute.java (our driver script) will read the entire Object Repository and store it in a variable
·         To read this object repository we need a ReadObject class which has a getObjectRepository method to read it.

NOTE: You may think why do we need to create an object repository. The answer it helps in code maintenance. For example, we are using the a button with name = btnlogin in 10 different test cases. In future , the developer decides to change the name from btnlogin to submit. You will have to make change in all the 10 test cases. In case of an object repository, you will make change just once in the repository.

Step 5)
·         The driver will pass the data from Excel & Object Repository to UIOperation class
·         UIOperation class has functions to perfom actions corresponding to keywords like CLICK, SETTEXT etc… mentioned in the excel
·         UIOperation class is a java class which has the actual implementation of the code to perform operations on web elements


The complete project will looks like-
Let's look into an example :
Test Scenario
·         We are executing 1 test cases
·         Test Case 1:
  • Goto : Your application URL
·          Enter User ID
·         Enter Password
·         Click Reset
object.properties
url= Your application URL
username=UserName
password=Password
loginButton=btnLogin

ReadExcelFile.java
package com.excelExportAndFileIO;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcelFile {
           
            public Sheet readExcel(String filePath,String fileName,String sheetName) throws IOException{
            //Create a object of File class to open xlsx file
            File file =       new File(filePath+"\\"+fileName);
            //Create an object of FileInputStream class to read excel file
            FileInputStream inputStream = new FileInputStream(file);
            Workbook Workbook = null;
            //Find the file extension by spliting file name in substing and getting only extension name
            String fileExtensionName = fileName.substring(fileName.indexOf("."));
            //Check condition if the file is xlsx file
            if(fileExtensionName.equals(".xlsx")){
            //If it is xlsx file then create object of XSSFWorkbook class
            Workbook = new XSSFWorkbook(inputStream);
            }
            //Check condition if the file is xls file
            else if(fileExtensionName.equals(".xls")){
                        //If it is xls file then create object of XSSFWorkbook class
                        Workbook = new HSSFWorkbook(inputStream);
            }
            //Read sheet inside the workbook by its name
            Sheet  Sheet = Workbook.getSheet(sheetName);
             return Sheet;           
            }
}


ReadObject.java
package com.operation;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ReadObject {

            Properties p = new Properties();

            public Properties getObjectRepository() throws IOException{
                        //Read object repository file
                        InputStream stream = new FileInputStream(new File("C:\\DatadrivenTest\\Training\\KeywordFW\\src\\com\\objects\\object.properties"));
                        //load all objects
                        p.load(stream);
                         return p;
            }
           
}

UIOperation.java
package com.operation;

import java.util.Properties;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class UIOperation {

            WebDriver driver;
            public UIOperation(WebDriver driver){
                        this.driver = driver;
            }
            public void perform(Properties p,String operation,String objectName,String objectType,String value) throws Exception{
                        System.out.println("");
                        switch (operation.toUpperCase()) {
                        case "CLICK":
                                    //Perform click
                                    driver.findElement(this.getObject(p,objectName,objectType)).click();
                                    break;
                        case "SETTEXT":
                                    //Set text on control
                                    driver.findElement(this.getObject(p,objectName,objectType)).sendKeys(value);
                                    break;
                                   
                        case "GOTOURL":
                                    //Get url of application
                                    driver.get(p.getProperty(value));
                                    break;
                        case "GETTEXT":
                                    //Get text of an element
                                    driver.findElement(this.getObject(p,objectName,objectType)).getText();
                                    break;

                        default:
                                    break;
                        }
            }
           
            /**
             * Find element BY using object type and value
             * @param p
             * @param objectName
             * @param objectType
             * @return
             * @throws Exception
             */
            private By getObject(Properties p,String objectName,String objectType) throws Exception{
                        //Find by xpath
                        if(objectType.equalsIgnoreCase("XPATH")){
                                   
                                    return By.xpath(p.getProperty(objectName));
                        }
                        //find by class
                        else if(objectType.equalsIgnoreCase("CLASSNAME")){
                                   
                                    return By.className(p.getProperty(objectName));
                                   
                        }
        else if(objectType.equalsIgnoreCase("ID")){
                                   
                                    return By.className(p.getProperty(objectName));
                                   
                        }
                        //find by name
                        else if(objectType.equalsIgnoreCase("NAME")){
                                   
                                    return By.name(p.getProperty(objectName));
                                   
                        }
                        //Find by css
                        else if(objectType.equalsIgnoreCase("CSS")){
                                   
                                    return By.cssSelector(p.getProperty(objectName));
                                   
                        }
                        //find by link
                        else if(objectType.equalsIgnoreCase("LINK")){
                                   
                                    return By.linkText(p.getProperty(objectName));
                                   
                        }
                        //find by partial link
                        else if(objectType.equalsIgnoreCase("PARTIALLINK")){
                                   
                                    return By.partialLinkText(p.getProperty(objectName));
                                   
                        }else
                        {
                                    throw new Exception("Wrong object type");
                        }
            }

}



LoginTest.java
package com.testCases;

import java.util.Properties;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

import com.excelExportAndFileIO.ReadExcelFile;
import com.operation.ReadObject;
import com.operation.UIOperation;

/**
 * THIS IS THE EXAMPLE OF KEYWORD DRIVEN TEST CASE
 *
 */
public class LoginTest {
           
    @Test
            public void testLogin() throws Exception {
                        // TODO Auto-generated method stub
       WebDriver webdriver = new FirefoxDriver();
       ReadExcelFile file = new ReadExcelFile();
        ReadObject object = new ReadObject();
        Properties allObjects =  object.getObjectRepository();
        UIOperation operation = new UIOperation(webdriver);
        //Read keyword sheet
        Sheet Sheet = file.readExcel("C:\\DatadrivenTest\\Training\\KeywordFW\\src\\com\\testData\\","TestCase.xlsx" , "KeywordFramework");
      //Find number of rows in excel file
       
            int rowCount = Sheet.getLastRowNum()-Sheet.getFirstRowNum();
            //Create a loop over all the rows of excel file to read it
            for (int i = 1; i < rowCount+1; i++) {
                        //Loop over all the rows
                       
                        Row row = Sheet.getRow(i);
                       
                        //Check if the first cell contain a value, if yes, That means it is the new testcase name
                        if(row.getCell(0).toString().length()==0)
                                   
                        {
                                   
                        //Print testcase detail on console
                                    System.out.println(row.getCell(1).toString()+"----"+ row.getCell(2).toString()+"----"+
                                    row.getCell(3).toString()+"----"+ row.getCell(4).toString());
                        //Call perform function to perform operation on UI
                                   
                                    operation.perform(allObjects, row.getCell(1).toString(), row.getCell(2).toString(),
                                                row.getCell(3).toString(), row.getCell(4).toString());
                }
                       
                        else{
                                   
                                    //Print the new  testcase name when it started
                                   
                                                System.out.println("New Testcase->"+row.getCell(0).toString() +" Started");
                                    }
                        }
            }

}


After execution, output will look like -



What are the Libraries need to add to the under Project?
1.      Selenium Jar Files.
a.      Selenium Standalone server
b.      Selenium Client Driver

2.      Excel reader jar files.
a.      POI jar files.


3.      Then need to place all these libraries in one Folder named as library.
4.      Once we create a project in Eclipse right click on JRE system Library -> configure build path-> Add External Library files-> Navigate to Library folder where all libraries copied. Select all the libraries and click on OK button and Click on OK button.
5.      Now your project is ready to develop the Test scripts.
6.      Now start writing the scripts one by one as mentioned above.


Thanks You…………

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...