Last active
August 11, 2019 11:20
-
-
Save temyers/e3246d666a27c59db04a to your computer and use it in GitHub Desktop.
Selenium FirefoxDriver with proxy configured profile
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
| import static java.lang.String.format; | |
| import java.net.URL; | |
| import org.openqa.selenium.WebDriver; | |
| import org.openqa.selenium.firefox.FirefoxDriver; | |
| import org.openqa.selenium.firefox.FirefoxProfile; | |
| import org.openqa.selenium.remote.DesiredCapabilities; | |
| import org.openqa.selenium.remote.RemoteWebDriver; | |
| public class DriverConfig { | |
| // Change these for your values. | |
| private static String proxyHost = ""; | |
| private static int proxyPort = 0; | |
| private static String ignoreProxy = "localhost, 127.0.0.1"; | |
| private static String seleniumGridHubHost = "localhost"; | |
| /** | |
| * Firefox preference value for specifying Manual Proxy configuration. | |
| */ | |
| private static final int MANUAL_PROXY_CONFIG = 1; | |
| public static WebDriver localFirefox() { | |
| FirefoxProfile profile = createProfile(); | |
| return new FirefoxDriver(profile); | |
| } | |
| private static FirefoxProfile createProfile() { | |
| FirefoxProfile profile = new FirefoxProfile(); | |
| // Use native events on all platforms | |
| profile.setEnableNativeEvents(true); | |
| boolean useProxy = !proxyHost.isEmpty(); | |
| if (useProxy) { | |
| // Configures the same proxy for all variants | |
| profile.setPreference("network.proxy.type", MANUAL_PROXY_CONFIG); | |
| profile.setPreference("network.proxy.http", proxyHost); | |
| profile.setPreference("network.proxy.http_port", proxyPort); | |
| profile.setPreference("network.proxy.ssl", proxyHost); | |
| profile.setPreference("network.proxy.ssl_port", proxyPort); | |
| profile.setPreference("network.proxy.socks", proxyHost); | |
| profile.setPreference("network.proxy.socks_port", proxyPort); | |
| profile.setPreference("network.proxy.ftp", proxyHost); | |
| profile.setPreference("network.proxy.ftp_port", proxyPort); | |
| profile.setPreference("network.proxy.no_proxies_on", ignoreProxy); | |
| } | |
| return profile; | |
| } | |
| public static WebDriver remoteFirefox() throws Exception { | |
| DesiredCapabilities capabilities = DesiredCapabilities.firefox(); | |
| capabilities.setCapability(FirefoxDriver.PROFILE, createProfile()); | |
| URL remoteAddress = new URL(format("http://%s:4444/wd/hub/", seleniumGridHubHost)); | |
| // Remote Driver, e.g. on a VM | |
| RemoteWebDriver driver = new RemoteWebDriver(remoteAddress, capabilities); | |
| return driver; | |
| } | |
| } |
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
| import org.junit.After; | |
| import org.junit.Before; | |
| import org.junit.Test; | |
| import org.openqa.selenium.By; | |
| import org.openqa.selenium.WebDriver; | |
| public class DriverConfigTest { | |
| private WebDriver firefox; | |
| @Before | |
| public void createDriver() { | |
| firefox = DriverConfig.localFirefox(); | |
| } | |
| @After | |
| public void destroyDriver() { | |
| firefox.quit(); | |
| } | |
| @Test | |
| public void test() { | |
| firefox.navigate().to("http://www.google.com.au"); | |
| // Find the search box | |
| firefox.findElement(By.tagName("input")); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment