mardi 2 août 2016

Using Factory Method pattern with Singleton via Java and WebDriver

I'm writting test automation framework and need some help. I have this enum class:

enum DriverType {

FIREFOX("Firefox"),
IE("Internet Explorer");

private String name;

DriverType(String name) {
    this.name = name;
}

public String getDriverName() {
    return name;
}}

And I have class called Driver, which creates instance of webdriver by type:

public class Driver {

private static final String DEFAULT_WEB_DRIVER = "default";
private static DriverType defaultType = DriverType.FIREFOX;

private static HashMap<String, WebDriver> instances;

static {
    instances = new HashMap<>();
}

public static WebDriver getInstance(String name, DriverType type) {

    WebDriver driver;

    if (!instances.containsKey(name)) {
        switch (type) {
            case FIREFOX:
                driver = new FirefoxDriver();
                break;
            case IE:
                driver = new InternetExplorerDriver();
                break;
            default:
                throw new UnknownDriverTypeException(
                        String.format("Unknown WebDriver type: %s.",
                                type.getDriverName()));
        }
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        instances.put(name, driver);
    }

    driver = instances.get(name);
    return driver;
}

public static WebDriver getInstance(String name) {
    return getInstance(name, defaultType);
}

public static WebDriver getInstance() {
    return getInstance(DEFAULT_WEB_DRIVER, defaultType);
}}

But now I want to use design patterns for my framework and have to use Singleton with Factory Method or Abstract Factory for this Driver class. Does somebody have idea? I haven't worked with patterns in Java yet. Tahnks.

Aucun commentaire:

Enregistrer un commentaire