As a software engineer, We all encounter some problems while designing a software. So what do we do when we face a problem? We google for solutions immediately. Don’t we? We google because we know that we would not be alone and someone would have already found the solution, for the problem we are facing now, which we could use to solve.
Design patternsare the solutions for the problems which you might face in your software design!!
Design Patternsare well optimized and reusable solutions to a given problem in software design. It helps us to show the relationship among the classes and the way in which they interact. Design pattern is a template which you have to carefully analyze and use it in appropriate places.
More information on design pattern can be found here .
Design Patterns in Test Automation:
As an automated test engineer, should we really care about design principles and patterns? Why do we need to learn/use Design Patterns in functional test automation?
After all, Test automation framework/automated test case is also a software which is used to test another software. So, we could apply the same design principles/patterns in our test automation design as well to come up with more elegant solution for any given design related problem in the test automation framework.
Remember that Design Patterns are NOT really mandatory. So you do not have to use them! But when you learn them, you would know exactly when to use! Basically these are all well tested solutions. So when you come across a problem while designing your framework/automated test cases, these design patterns could immediately help you by proving you a formula / template & saves us a lot of time and effort.
Note: Your aim should not be to implement a certain pattern in your framework. Instead, identify a problem in the framework and then recognize the pattern which could be used to solve the problem. Do not use any design pattern where it is not really required!!
In this article, We are going to see where we could use the Template Method Pattern which is one of the Behavioral Patterns .
Template Method Pattern:
Goal : Define the skeleton of an algorithm in an operation, deferring some steps to client sub classes. Template Method lets sub classes redefine certain steps of an algorithm without changing the algorithm’s structure.
If we consider this below example, all the workers have same routine. getup, eat breakfast and so on. But type of work could be different for each worker. [source: https://sourcemaking.com/design_patterns/template_method]
Lets see where we could use design pattern in Test automation.
Template Method Pattern in Test Automation:
Lets consider this – PHPTravels – application. Lets also assume that we need to automate this application’s Hotel and Car reservation workflow. As part of the business workflow, we need to below steps.
- enter the locaiton
- enter the dates
- click on the search
- sort the results by the price
- pick the lowest price
- get the confirmation number.
The above steps are applicable for both hotel and car reservations workflow. They have significant similarities. However screens are different, car reservation might require 2 locations one for pickup and one for dropoff which is different from just 1 location for Hotel.
Considering the workflow, We could create a template like this.
Hotel Page Object:
Car Page Object:
Hotel Reservation Workflow:
Car Reservation Workflow:
By using Template Method Pattern, we create a skeleton for the reservation functionality. If there is any common functionality, then it can be kept in the Template class itself. Hotel, Car, Flight etc any reservation can implement this template by overriding abstract methods of the Template. So, there is no code duplication.
Design Pattern is a very good tool for effective communication. How? As soon as we name a pattern, Others can immediately picture the high-level-design in their heads & get the solution for the given problem. Thus, Design Pattern enables us to explain the design in a much better way.
Happy Testing & Subscribe