Join us on :

Factory Pattern in Selenium

Let's understand that This articles is about Factory Pattern with selenium, donot get confused with Page factory pattern.

Define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate

Let's Undestand Issue first :

  • Your friend needs a haircut
  • You have tried all the barbarshops in the city, so every one thinks you will direct them to good shop
  • The friend could be Male or Female, based on the gender you have to take the decision (it becomes tough take decisions when you are a women bacuse of lot of options, so lets consider you are a man)
  • You directed the Friend to the shop and he got nice haircut

In Simple Words :

Providing simple choice to the user and letting the backend people to handle the difficulties, In barbershop example, we just want to get the haircut but we donot cut our hairs. Cutting the hair is the problem of barbar.

factory-pattern-selenium-barbar-shop

Let's Understand the solution

Based on the given input, we have to provide the output object from list of sub classes

Below are few things that you should know while directing / suggesting a barbarshop

  • By default you will think of a criteria that a shop which cuts the hair (must)
  • You will decide shop based on whether a man or women is asking of suggestion from you
  • You will provide the suggestion to the person
  • Hair cut is perfomed there

Create same modal in Code

First you will create a Interface which contains a abstract class called haircut, as you are aware that all the classes which implement the interface should provide implementation for all the abstract methods present in the Interface. (this is eulaent to criteria of haircut in a shop)

I do understand, what you are thinking, Interface contains 100% abstract methods, then why karthiq (author) is explaining like there are other methods.

Now with recent updates Interfaces can have private methods and static blocks inside interface

Now we have to create sub classes for the Interface ( these sub classes are nothing but barbar shops), So we have to provide implementation to the abstract methods.

Now we have to create a class which does not inherit the Interface but decides which object to invoke. It is nothing but you who does not have barbar shop but know all the barbar shops.

The object in the classes will be returned based one criteria / value, like which gender is asking.

Handle dropdowns in selenium

Code solution for above problem

Create a interface called HairCut_Criteria, this interface has a abstract method called haircut() .


interface HairCut_Criteria{
	boolean haircut();
}		


Whoever implements the HairCut_Criteria, they have to provide implementation to the haircut(). ForMale and ForFemale both classes has implemented the haircut() because they are sub-classes of HairCut_Criteria Interface.


class ForMale implements HairCut_Criteria{
	@Override
	public boolean haircut() {
		System.out.println("Hair cut is finished for man & price is INR 120");
		return true;
	}
}
class ForFemale implements HairCut_Criteria{
	@Override
	public boolean haircut() {
		System.out.println("Hair cut is finished for Woman & price is INR 700");
		return true;
	}
}		


Create class which represents your position (who know better barber shops), For this I have created a class called You. You class will decide barber shop or return the object of required class based on a decision like Man or Woman.


class You {
	public static HairCut_Criteria suggestAShop(String criteria) {
		
		if(criteria.equals("man")) {
			return new ForMale();
		}else {
			return new ForFemale();
		}
	}
}	


It is time for class which represents your friend, who will ask you which barbershop is better.


public class TheFriend{
	public static void main(String[] args) {
		// suggest for man
		You.suggestAShop("man").haircut();
		// suggest for woman
		You.suggestAShop("woman").haircut();
	}
}		


Complete code for factory Pattern


interface HairCut_Criteria{
	boolean haircut();
}
class ForMale implements HairCut_Criteria{
	@Override
	public boolean haircut() {
		System.out.println("Hair cut is finished for man & price is INR 120");
		return true;
	}
}
class ForFemale implements HairCut_Criteria{
	@Override
	public boolean haircut() {
		System.out.println("Hair cut is finished for Woman & price is INR 700");
		return true;
	}
}
class You {
	public static HairCut_Criteria suggestAShop(String criteria) {
		
		if(criteria.equals("man")) {
			return new ForMale();
		}else {
			return new ForFemale();
		}
	}
}
public class TheFriend{
	public static void main(String[] args) {
		// suggest for man
		You.suggestAShop("man").haircut();
		// suggest for woman
		You.suggestAShop("woman").haircut();
	}
}
		


factory-pattern-selenium

So far we have seen what is factory pattern, lets see how pattern will be used with selenium

Cucumber With Selenium

Factory Pattern with Selenium

Yes, I wanted to explain it simple way that why i got to write above one. Now let's see how can we implements same thing with Selenium.

Redefine the problem with selenium

  • Now you have to run your execution in different browser
  • Based on a value (read from user input, or from excel, properties file) You should decide the browser

Factory Pattern solution with selenium

  • Read the value(browser name) and pass it to a method which makes the decision
  • Webdriver is an Interafce which provides a list of abstract methods
  • ChromeDriver, FireFoxDriver and other Browser classes are sub classes of Webdriver.
  • All sub classes of Webdriver Implements abstract methods present in the Webdriver
  • The decision taking method should return object or driver of a browser based on the value(browser name)
  • Now use the browser to run selenium commands

Open different browser with selenium

Complete program for Factory Pattern with webdriver.

Browser classes takes care of the overriding/iMplementing the abstract methods of Webdriver

class DriverDecider {
	public static WebDriver suggestAShop(String criteria) {
		
		if(criteria.equals("chrome")) {
			return new ChromeDriver();
		}else if(criteria.equals("firefox")) {
			return new FirefoxDriver();
		}else if(criteria.equals("ie")) {
			return new InternetExplorerDriver();
		}
		
		// if none of the above browser matches
		return null;
	}
}
public class TheFriend{
	public static void main(String[] args) {
		// create chrome driver
		DriverDecider.suggestAShop("chrome");
	}
}		


Compare Screenshots in selenium

Factory Pattern with Switch Case

I can hear you guys are screming like Why donot you use Switch case, that is right.

It is true that when we have more than one if else block then we should try to use the switch cases. So let me rewite the above example using swicth cases


class DriverDecider {
	public static WebDriver getDriver(String criteria) {

		switch (criteria) {
			case "chrome":
				return new ChromeDriver();
			case "firefox":
				return new FirefoxDriver();
			case "ie":
					return new InternetExplorerDriver();
			default:
				// if none of the above browser matches
				return null;
		}
	}
}
public class TheFriend{
	public static void main(String[] args) {
		// create chrome driver
		WebDriver driver = DriverDecider.getDriver("chrome");
		driver.get("https://chercher.tech");
	}
}		


I know, that i have not added a break statements, can you guess why ?

Leave your answer below on comments section

When to use Factory Pattern in Selenium

  • When a class doesn't know what sub-classes will be required to create
  • When a class wants that its sub-classes specify the objects to be created.
  • When the parent classes choose the creation of objects to its sub-classes.

About Author

Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know. Knowledge is the only thing that doubles when you spend it.

I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
  • latha
    You have used return statement then how will the break be used. The break statement will never be executed
    Reply