Created
February 26, 2015 16:58
-
-
Save sayems/7c695a8e85d129a7e905 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.sayem.browser; | |
| import java.net.URL; | |
| import java.util.Map; | |
| import org.openqa.selenium.WebDriverException; | |
| import org.openqa.selenium.chrome.ChromeDriver; | |
| import org.openqa.selenium.firefox.FirefoxDriver; | |
| import org.openqa.selenium.firefox.FirefoxProfile; | |
| import org.openqa.selenium.ie.InternetExplorerDriver; | |
| import org.openqa.selenium.remote.DesiredCapabilities; | |
| import org.openqa.selenium.remote.RemoteWebDriver; | |
| import org.openqa.selenium.safari.SafariDriver; | |
| import com.google.common.base.Strings; | |
| import com.google.common.collect.ImmutableMap; | |
| public class BrowserRepository { | |
| private static String FIREFOX = "ff"; | |
| private static String CHROME = "chrome"; | |
| private static String SAFARI = "safari"; | |
| private static String IEXPLORER = "ie"; | |
| //J- | |
| private static Map<String, Initializable> repository = new ImmutableMap.Builder<String, Initializable>() | |
| .put(IEXPLORER, new IExplorerBrowser()) | |
| .put(FIREFOX, new FirefoxBrowser()) | |
| .put(CHROME, new ChromeBrowser()) | |
| .put(SAFARI, new SafariBrowser()) | |
| .build(); | |
| //J+ | |
| public static RemoteWebDriver getBrowserForName(final String driverName, final String remoteUrl) throws Exception { | |
| if (!repository.containsKey(driverName)) { | |
| throw new WebDriverException(String.format("Browser for name {} does not exist", driverName)); | |
| } | |
| return repository.get(driverName).initialize(remoteUrl); | |
| } | |
| private interface Initializable { | |
| RemoteWebDriver initialize(String url) throws Exception; | |
| } | |
| private static class FirefoxBrowser implements Initializable { | |
| @Override | |
| public RemoteWebDriver initialize(final String url) throws Exception { | |
| DesiredCapabilities capabilities = DesiredCapabilities.firefox(); | |
| capabilities.setCapability(FirefoxDriver.PROFILE, new FirefoxProfile()); | |
| capabilities.setJavascriptEnabled(true); | |
| return Strings.isNullOrEmpty(url) ? new FirefoxDriver() : new RemoteWebDriver(new URL(url), capabilities); | |
| } | |
| } | |
| private static class ChromeBrowser implements Initializable { | |
| @Override | |
| public RemoteWebDriver initialize(final String url) throws Exception { | |
| //System.setProperty("webdriver.chrome.driver", "C:/selenium/jar/chromedriver.exe"); | |
| DesiredCapabilities capabilities = DesiredCapabilities.chrome(); | |
| capabilities.setBrowserName("chrome"); | |
| capabilities.setJavascriptEnabled(true); | |
| return Strings.isNullOrEmpty(url) ? new ChromeDriver() : new RemoteWebDriver(new URL(url), capabilities); | |
| } | |
| } | |
| private static class IExplorerBrowser implements Initializable { | |
| @Override | |
| public RemoteWebDriver initialize(final String url) throws Exception { | |
| System.setProperty("webdriver.ie.driver", "C:/selenium/jar/IEDriverServer.exe"); | |
| DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer(); | |
| capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); | |
| capabilities.setCapability("ignoreZoomSetting", true); | |
| capabilities.setBrowserName("*iexplore"); | |
| capabilities.setJavascriptEnabled(true); | |
| return Strings.isNullOrEmpty(url) ? new InternetExplorerDriver() | |
| : new RemoteWebDriver(new URL(url), capabilities); | |
| } | |
| } | |
| private static class SafariBrowser implements Initializable { | |
| @Override | |
| public RemoteWebDriver initialize(final String url) throws Exception { | |
| DesiredCapabilities capabilities = DesiredCapabilities.safari(); | |
| capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); | |
| capabilities.setBrowserName("safari"); | |
| capabilities.setJavascriptEnabled(true); | |
| return Strings.isNullOrEmpty(url) ? new SafariDriver() : new RemoteWebDriver(new URL(url), capabilities); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment