Driver Factory
by Santiago L. Schumacher
Factory Pattern for Selenium WebDriver
On my previous posts we initialized a new maven Project, and added the dependencies we will need for our Framework. Now we can get started with the main functionality of any Test Automation Framework, being able to create WebDriver instances to interact with your Applications.
Let’s get started with the basics, first, we need a way to easily choose between our favourite Browser (Chrome, Firefox, Edge …) and change the URL of our application depending on the environment.
We'll use
- Factory design pattern: A popular OOP pattern that allows for easier extension and decoupled code, where only the concrete classes need to know exactly how (logic) to create a new Instance. This helps create a really simple one-line-of-code way to create your WebDriver instance. This is our main focus on this post.
- Maven Profiles that will help us run test suites against different URL's in an easy way. We will cover this on subsequent posts
Implementing Factory Pattern
Start by creating new packages in our project, right click on src/main/java
, go to New -> Package and create:
- drivers
- testrunner
You can name them as you wish of course, but always keep in mind that the idea of packages are to group similar classes/functionality inside specific folders to keep everything neatly organized and easy to find.
We want the following classes inside the drivers
package:
Now, let’s go one by one.
DriverManager (Abstract Class)
And the DriverFactory, which is an enum, containing all the concrete implementations for every Browser.
We can use methods inside the enum. These will make calling the Factory extremely simple !
If we want to create an instance of Chrome Driver, we can just type DriverFactory.CHROME
and we’re good to go, sounds simple, right ?
The only caveat is that implementation (adding new Browsers) can get a little tricky.
Finally, the implementations. I’ll show ChromeDriver for now, since the rest of them are very similar:
Ok, looks like we have everything in place to create Selenium WebDriver instances, and now you might be wondering:
- My Client needs me to test with with Opera !! Your enum only mentions Chrome or Firefox ! To solve this you:
- Create a new concrete class that extends DriverManager. Call it OperaDriverManager. Replace
WebDriverManager.getInstance(CHROME)
withWebDriverManager.getInstance(OPERA)
-- remember to change the static import as well -- and the return line toreturn new OperaDriver()
- On the DriverFactory enum, add the following:
- Create a new concrete class that extends DriverManager. Call it OperaDriverManager. Replace
- "This looks cool but, how do I use it ?" - Well this one is easy. Before starting our tests we call
DriverFactory.valueOf(driverName).getDriverManager()
. Replace the "driverName" with "CHROME", or "FIREFOX", or any other implementation you might have coded yourself.
Thank you for sticking with me, and see you in the next post.
Subscribe via RSS