0

These are the three most popular design patterns in JAVA. Most Selenium WebDriver projects are build on these.

  1. What is the difference between these three, with examples?
  2. Does Choosing a pattern also depends on kind of project you want to automate? Example if it is shopping, social networking etc.
  3. Structural(Skeleton) difference?

I have tried:

https://stackoverflow.com/questions/2094211/difference-between-singleton-and-factory-pattern

http://www.guru99.com/page-object-model-pom-page-factory-in-selenium-ultimate-guide.html

but couldn't understand and came out with these questions.

  • Design patterns are usually language-independent. Singleton, POM, abstract factory can be used in Java, C#, python, i.,e., all object-oriented languages – dzieciou Mar 6 '16 at 7:25
2

all in all these 3 design patterns, we are instantiating WebDriver object only once and using across the project (we have one baseclass, where we have setup and teardown methods, and instantiating .properties file, webdriver object, taking screenshots etc.). So how these 3 are different from other.

First, singleton and abstract factory do need to be used for creating only instances of WebDriver, but you can have a factory for creating e.g., an instance of a page object class or instance of, say, Person class.

But let's stick to creating of WebDriver instance in different patterns.

Abstract factory is called abstract, because it serves for creating different implementations of WebDriver interface, e.g., FirefoxWebDriver, ChromeWebDriver, etc. A singleton class creates only one particular implementation of WebDriver interface.

Next, Only in singleton pattern you're creating WebDriver instance once for all tests and it is globally accessible for all tests. In abstract factory everytime you call a factory method it creates a new instance of WebDriver class. It's only up to you when you call this method, i.e., how many instances you will create. You can create WebDriver instance even multiple times in a single test method.

In Page Object Model (POM), an instance of any page object class wraps instance of WebDriver. POM does not define how WebDriver instances are created. However, since Web Driver instance is stateful, i.e., it contains a state in which Web browser is (current page URL, stored cookies, cache, etc.), I recommend to:

  1. Create a new instance for each test method and use it for all page objects in a single test method and
  2. Do not reuse same instance of WebDriver across multiple independent test methods.

In practice, @BeforeMethod-annotated method in test class is a common place to create instance of WebDriver, and @AfterMethod-annotated method to close it.

Does Choosing a pattern also depends on kind of project you want to automate? Example if it is shopping, social networking etc.

No. Those are generic design patterns that can be use in completely different domains and what's more, not only for test automation.

Structural(Skeleton) difference?

I guess you mean how those patterns can be implemented.

Singleton for obtaining WebDriver instance.

public class WebDriverSingleton {

   public static WebDriver driver;

   public static WebDriver getInstance() {
     if (driver == null) {
       driver = new FirefoxWebDriver();
     }
     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();
   }

}
1

What is the difference between these three, with examples?

Look at the wiki pages for each; Singleton, POM, and Abstract.

  • In software engineering, the singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects. The term comes from the mathematical concept of a singleton.
  • A Project Object Model (POM) provides all the configuration for a single project. General configuration covers the project's name, its owner and its dependencies on other projects. One can also configure individual phases of the build process, which are implemented as plugins. For example, one can configure the compiler-plugin to use Java version 1.5 for compilation, or specify packaging the project even if some unit tests fail.
  • The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes. In normal usage, the client software creates a concrete implementation of the abstract factory and then uses the generic interface of the factory to create the concrete objects that are part of the theme. The client doesn't know (or care) which concrete objects it gets from each of these internal factories, since it uses only the generic interfaces of their products. This pattern separates the details of implementation of a set of objects from their general usage and relies on object composition, as object creation is implemented in methods exposed in the factory interface.

Does Choosing a pattern also depends on kind of project you want to automate? Example if it is shopping, social networking etc.

Once you read the wikis it should be clear depending on your project, it's not necessarily the project type in the sense that you asked. It more a choice of how you need to instantiate and use the object(s).

Structural(Skeleton) difference?

Again, this ought to be made clear by looking over the definitions and examples of the wiki articles.

  • 1
    I think by POM the asker means the page object model since that relates to selenium design patterns a little more directly – Julian Nov 7 '15 at 1:18
  • In that case, code.google.com/p/selenium/wiki/PageObjects – kirbycope Nov 7 '15 at 1:34
  • @all in all these 3 design patterns, we are instantiating WebDriver object only once and using across the project (we have one baseclass, where we have setup and teardown methods, and instantiating .properties file, webdriver object, taking screenshots etc.). So how these 3 are different from other. – paul Nov 8 '15 at 8:52
  • 1
    @paul, that's not true. I explained that in my answer. – dzieciou Mar 6 '16 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.