Selenium WebDriver - Part 1 - Architecture to run tests with different browsers

Selenium WebDriver has pretty much been the de-facto standard for web automation. While there is a lot of documentation out there, I haven’t found an article explaining an architecture for running scripts with different browsers.

This is part 1 of a 2 part series to explain the steps we took at my current workplace to tackle this problem. Part 1 (this part) will explain how to setup a project, and the architecture for handling different browsers. Part 2 will explain using Ant so we can run our tests using command line and generate an XML report. Let’s begin!

For our purposes, we used IntelliJ as our Java IDE. So start by creating a new project, give it a name, and complete the rest of the setup (there are many tutorials on this so I wont go into detail). We used the Page Object Model for our overall architecture. Here’s our folder structure.

To handle running tests with different browsers, we used the factory pattern. We created a class called DriverFactory. Its responsibility is to create the driver of whatever type (browser) we’d like the tests to run with, and return it to the tests.

Here’s a test class:

public class SignInTests {
    private static WebDriver driver = DriverFactory.constructDriver();

    public static void beforeClass() {}

    public void registerAccountSuccessfully() {}

Here’s the DriverFactory class:

public class DriverFactory {
    public static WebDriver constructDriver() {
        if(System.getProperty("browsername") != null) {
            String browser = System.getProperty("browsername");
            if (browser.toLowerCase().equals("firefox")) {
                return new Firefox().setDriver();
            else if(browser.toLowerCase().equals("chrome")) {
                return new Chrome().setDriver();
            // Add more browsers here
        return new Firefox().setDriver();

Finally, here’s one of the browser classes:

public class Firefox {
    public Firefox() {}

    public WebDriver setDriver() {
        // Using new Firefox WebDriver based off of marionette
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setCapability("marionette", true);
        WebDriver driver = new FirefoxDriver(capabilities);

        return driver;

I’ll go into more detail about the DriverFactory class in part 2 but basically the idea is:

  • Test uses DriverFactory to create a WebDriver
  • DriverFactory determines what browser we want to generate from system property ‘browsername’ (passed in — will show in part 2)
  • DriverFactory returns an instance of a driver of type Firefox, Chrome, etc

In the next part, we will discuss how to setup Ant so we can run our tests via command line, pass in required parameters, and generate XML results that can be parsed by Jenkins.