Showing posts with label Design Patterns. Show all posts
Showing posts with label Design Patterns. Show all posts

Saturday, July 16, 2016

Data Driven Approach - Using HashMap/Dictionary for Selenium automation

In this article, we will discuss how to create data dictionary or hash map in Java for data driven tests and read the data from the excel/csv file similar to dictionary object. Please go through below code example to read data from the external source using hashmap/dictionary or data driven approach. Please suggest or comment for better approach for data driven testing in java for Selenium.


package testingtest;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

class testclass {

 static String[][] getexceldatainArray;
 static HashMap<String, String> dictionary;

 public static void main(String[] args) throws IOException, BiffException {
  // Create an array based on data in excel file
  excelarrayclass tc = new excelarrayclass();
  getexceldatainArray = tc.CreateArrayfromExcel("D:\\Testing.xls",
    "testing");
  for (int i = 0; i < getexceldatainArray.length; i++) {
   dictionary = DataDictionaryFromArray(getexceldatainArray, i);
   // the keys will be value in the header column
   String username = dictionary.get("UserName");
   /*
    * Perform the required operation using the dictionary values
    */
   System.out.println("UserName " + username);
  }
 }

 public String[][] CreateArrayfromExcel(String WorkbookName, String SheetName)
   throws BiffException, IOException {
  String[][] arrTestDta;

  Workbook workbk = Workbook.getWorkbook(new File(WorkbookName));
  Sheet wrkSheet = workbk.getSheet(SheetName);
  arrTestDta = new String[wrkSheet.getRows()][wrkSheet.getColumns()];

  // Loop to get data from excel file into an array

  for (int i = 0; i < wrkSheet.getRows(); i++) {
   for (int j = 0; j < wrkSheet.getColumns(); j++) {
    Cell cell = wrkSheet.getCell(j, i);
    if (cell.getContents() != null) {
     arrTestDta[i][j] = cell.getContents();
    }
   }
  }
  return arrTestDta;
 }

 public static HashMap<String, String> DataDictionaryFromArray(
  String[][] arrayforDictionary, int iRow) {
  // Create a hashmap to store the data as dictionary in key value form
  HashMap<String, String> dictionary = new HashMap<String, String>();
  // get the size of the array, assuming being used in data driven test,
  // get the number of columns in the data file
  int iColCnt = arrayforDictionary[0].length;
  for (int i = 0; i < iColCnt; i++) {
   // mark the header column values as key and current column value as
   // key value pair
   dictionary.put(arrayforDictionary[0][i],
     arrayforDictionary[iRow][i]);
  }
  return dictionary;
 }
}

Saturday, March 12, 2016

Page Object Model pattern in Selenium?

Page Object Model in selenium is a design Pattern (Design Pattern is a general reusable solution(best practices) to a commonly occurring problem within the context in software design) implemented using selenium Web Driver. Page Object Model is used to create object repository for elements in the web application used in test flows.


Page Object Model Pattern serves following purposes:


  • Segregating objects definition from test scripts in separate classes making test scripts independent of page elements and locators, thus reducing the maintenance cost in case of changes in elements locator in the Application under test.
  • Defining Page elements and page methods in Page classes help in re-usability of the page elements and methods in different tests reduces the line of code and makes it more maintainable as changes in application needs to be implemented at Page classes only.
  • Make code more readable in the test scripts.
  • Easier for testers with less knowledge of selenium/Java to create test scripts once the Page classes are prepared.

Page Object Model - Do not see too seriously

Understanding how to automate a web application using selenium Web Driver using Page Object Model design pattern


Let us try to understand how Page object model pattern is implemented with an example. 


Suppose there are number of pages in an Web Application and within each page there are multiple elements in each page. Let us assume linear approach for creating automated test cases. Suppose there are ~100 test scripts(workflows) to test the application. Each of the workflow starts with login into application. In each of the test cases, we would have defined the locators for input fields for username and password and login button elements and interact(perform action) with the element. 


Creating test using above approach will result in following problems:
  • In case of changes in an element locator, changes needs to be implemented at multiple tests
  • Readability of test will be poor due to locators defined at test level. 
  • Lines of code will be higher and approach for creating test scripts will vary from one test developer to another.
  • Requires script developer to have good selenium knowledge for test scripts creation. 

Object repository structure in QTPA better approach would be to define elements locators at a different location from test scripts. Also methods for working with elements should be defined, separate of the test scripts.


If you had worked with QTP before, the object repository used to be maintained in tree structure. In Page Object model design pattern,Web Pages are represented by classes with elements in the Page defined as members of the class. Actions on the elements are implemented as methods in the class very much similar to QTP approach.



Implementing Page Object Model:


  • Create a class for login Page (similarly create class for different pages).
  • Define the objects locator in the Page as member of the class.
  • In the constructor method for the Page class, load or intialise the elements defined in the Page using Page Object Factory. 
  • Create user defined methods in the class to input data in the input box, clicking the login button, and methods for successful login or failure. 
  • An object of the page class will be created in the test scripts to interact with the objects and methods for Page class.
    Page Object model pattern implementation

I have not added any code snippet in this article, but below reference article have nice examples explaining the code and concept of Page Object model pattern in selenium.


Useful References for code and concept of Page Object Model

Hope you find this post informative or atleast the references useful.