Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Basic implementation of a factory pattern to create browser instances using Selenium WebDriver
public class DriverFactory {
public WebDriver createInstance(String browser) {
WebDriver driver;
BrowserList browserType = BrowserList.valueOf(browser.toUpperCase());
switch (browserType) {
case CHROME:
driver = new ChromeDriverManager().createDriver();
break;
case FIREFOX:
driver = new FirefoxDriverManager().createDriver();
break;
case EDGE:
driver = new EdgeDriverManager().createDriver();
break;
case SAFARI:
driver = new SafariDriverManager().createDriver();
break;
case OPERA:
driver = new OperaDriverManager().createDriver();
break;
case IE:
driver = new IEDriverManager().createDriver();
break;
default:
throw new BrowserNotSupportedException(browser);
}
return driver;
}
public enum BrowserList {
CHROME, FIREFOX, EDGE, SAFARI, OPERA, IE
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment