Singleton Design Pattern in Automation Testing
Introduction
The Singleton Design Pattern can be used in the automation tests to build up easier access to page objects and facades. Its usage can speed up the tests writing process dramatically. In this article, I am going to show you the best and most straightforward approaches to integrating the Singleton Design Pattern in your test automation framework
Definition
Ensure a class has only one instance and provide a global point of access to it.
- Instance control – prevents other objects from instantiating their copies of the Singleton object, ensuring that all objects access the single instance.
- Flexibility – since the class controls the instantiation process, the class has the flexibility to change the instantiation process.
- Lazy initialization – defers the object’s creation until it is first used.
UML Class Diagram
Participants
The classes and objects participating in this pattern are:
- Page Objects (
BingMainPage)- Holds the actions that can be performed on the page like Search and Navigate. Exposes an easy access to the Page Validator through theValidate()method. The best implementations of the pattern hide the usage of the Element Map, wrapping it through all action methods. BasePage<S, M>– Gives access to the child’s page element map class and defines a standard navigation operation.BasePage<S, M, V>– Adds an instance to the child page’s validator class through theValidatemethod.BaseSingleton– This is anabstractclass that contains astaticproperty of its child instance
Singleton for obtaining
WebDriver instance.::Singleton class for obtaining single instance of webdriver instance
public class WebDriverSingleTon{
public static webdriver driver;
public static Webdriver getInstance(){
if (driver==Null){
driver = New FirefoxDriver();
}
return driver;
}
}
Abstract factory for creating
WebDriver instance.::public class WebDriverFactory (
public static WebDriver createWebDriverForFirefox(){
return new FirefoxWebDriver();
}
public static WebDriver createWebDriverForChrome(){
return New ChromeWebDriver();
}
public static WebDriver create WebDRiverForOpera{
return New OperaWebDriver();
}
}
Comments
Post a Comment