org.openqa.selenium.Proxy.ProxyType Java Examples

The following examples show how to use org.openqa.selenium.Proxy.ProxyType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProxyTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Test
public void manualProxyToJson() {
  Proxy proxy = new Proxy();
  proxy.setProxyType(ProxyType.MANUAL);
  proxy.setHttpProxy("http.proxy:1234");
  proxy.setFtpProxy("ftp.proxy");
  proxy.setSslProxy("ssl.proxy");
  proxy.setNoProxy("localhost,127.0.0.*");
  proxy.setSocksProxy("socks.proxy:65555");
  proxy.setSocksVersion(5);
  proxy.setSocksUsername("test1");
  proxy.setSocksPassword("test2");

  Map<String, Object> json = proxy.toJson();

  assertThat(json.get("proxyType")).isEqualTo("MANUAL");
  assertThat(json.get("ftpProxy")).isEqualTo("ftp.proxy");
  assertThat(json.get("httpProxy")).isEqualTo("http.proxy:1234");
  assertThat(json.get("sslProxy")).isEqualTo("ssl.proxy");
  assertThat(json.get("socksProxy")).isEqualTo("socks.proxy:65555");
  assertThat(json.get("socksVersion")).isEqualTo(5);
  assertThat(json.get("socksUsername")).isEqualTo("test1");
  assertThat(json.get("socksPassword")).isEqualTo("test2");
  assertThat(json.get("noProxy")).isEqualTo(Arrays.asList("localhost", "127.0.0.*"));
  assertThat(json.entrySet()).hasSize(9);
}
 
Example #2
Source File: DriverFactory.java    From NoraUi with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Generates an ie webdriver. Unable to use it with a proxy. Causes a crash.
 *
 * @return
 *         An ie webdriver
 * @throws TechnicalException
 *             if an error occured when Webdriver setExecutable to true.
 */
private WebDriver generateIEDriver() throws TechnicalException {
    final String pathWebdriver = DriverFactory.getPath(Driver.IE);
    if (!new File(pathWebdriver).setExecutable(true)) {
        throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_MESSAGE_WEBDRIVER_SET_EXECUTABLE));
    }
    log.info("Generating IE driver ({}) ...", pathWebdriver);

    System.setProperty(Driver.IE.getDriverName(), pathWebdriver);

    final InternetExplorerOptions internetExplorerOptions = new InternetExplorerOptions();
    internetExplorerOptions.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
    internetExplorerOptions.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
    internetExplorerOptions.setCapability(InternetExplorerDriver.REQUIRE_WINDOW_FOCUS, true);
    internetExplorerOptions.setCapability(InternetExplorerDriver.NATIVE_EVENTS, false);
    internetExplorerOptions.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
    internetExplorerOptions.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
    internetExplorerOptions.setCapability("disable-popup-blocking", true);

    setLoggingLevel(internetExplorerOptions);

    // Proxy configuration
    if (Context.getProxy().getProxyType() != ProxyType.UNSPECIFIED && Context.getProxy().getProxyType() != ProxyType.AUTODETECT) {
        internetExplorerOptions.setCapability(CapabilityType.PROXY, Context.getProxy());
    }

    return new InternetExplorerDriver(internetExplorerOptions);
}
 
Example #3
Source File: DriverFactory.java    From NoraUi with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Generates a firefox webdriver.
 *
 * @return
 *         A firefox webdriver
 * @throws TechnicalException
 *             if an error occured when Webdriver setExecutable to true.
 */
private WebDriver generateFirefoxDriver() throws TechnicalException {
    final String pathWebdriver = DriverFactory.getPath(Driver.FIREFOX);
    if (!new File(pathWebdriver).setExecutable(true)) {
        throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_MESSAGE_WEBDRIVER_SET_EXECUTABLE));
    }
    log.info("Generating Firefox driver ({}) ...", pathWebdriver);

    System.setProperty(Driver.FIREFOX.getDriverName(), pathWebdriver);

    final FirefoxOptions firefoxOptions = new FirefoxOptions();
    final FirefoxBinary firefoxBinary = new FirefoxBinary();

    firefoxOptions.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
    firefoxOptions.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);

    setLoggingLevel(firefoxOptions);

    // Proxy configuration
    if (Context.getProxy().getProxyType() != ProxyType.UNSPECIFIED && Context.getProxy().getProxyType() != ProxyType.AUTODETECT) {
        firefoxOptions.setCapability(CapabilityType.PROXY, Context.getProxy());
    }

    if (Context.isHeadless()) {
        firefoxBinary.addCommandLineOptions("--headless");
        firefoxOptions.setBinary(firefoxBinary);
    }
    firefoxOptions.setLogLevel(FirefoxDriverLogLevel.FATAL);

    return new FirefoxDriver(firefoxOptions);
}
 
Example #4
Source File: ProxyTest.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Test
public void pacProxyToJson() {
  Proxy proxy = new Proxy();
  proxy.setProxyType(ProxyType.PAC);
  proxy.setProxyAutoconfigUrl("http://aaa/bbb.pac");

  Map<String, Object> json = proxy.toJson();

  assertThat(json.get("proxyType")).isEqualTo("PAC");
  assertThat(json.get("proxyAutoconfigUrl")).isEqualTo("http://aaa/bbb.pac");
  assertThat(json.entrySet()).hasSize(2);
}
 
Example #5
Source File: ProxyTest.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Test
public void autodetectProxyToJson() {
  Proxy proxy = new Proxy();
  proxy.setProxyType(ProxyType.AUTODETECT);
  proxy.setAutodetect(true);

  Map<String, ?> json = proxy.toJson();

  assertThat(json.get("proxyType")).isEqualTo("AUTODETECT");
  assertThat((Boolean) json.get("autodetect")).isTrue();
  assertThat(json.entrySet()).hasSize(2);
}
 
Example #6
Source File: ProxyTest.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Test
public void systemProxyToJson() {
  Proxy proxy = new Proxy();
  proxy.setProxyType(ProxyType.SYSTEM);

  Map<String, Object> json = proxy.toJson();

  assertThat(json.get("proxyType")).isEqualTo("SYSTEM");
  assertThat(json.entrySet()).hasSize(1);
}
 
Example #7
Source File: ProxyTest.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Test
public void directProxyToJson() {
  Proxy proxy = new Proxy();
  proxy.setProxyType(ProxyType.DIRECT);

  Map<String, Object> json = proxy.toJson();

  assertThat(json.get("proxyType")).isEqualTo("DIRECT");
  assertThat(json.entrySet()).hasSize(1);
}
 
Example #8
Source File: DriverFactory.java    From NoraUi with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Generates a chrome webdriver.
 *
 * @return
 *         A chrome webdriver
 * @throws TechnicalException
 *             if an error occured when Webdriver setExecutable to true.
 */
private WebDriver generateGoogleChromeDriver() throws TechnicalException {
    final String pathWebdriver = DriverFactory.getPath(Driver.CHROME);
    if (!new File(pathWebdriver).setExecutable(true)) {
        throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_MESSAGE_WEBDRIVER_SET_EXECUTABLE));
    }
    log.info("Generating Chrome driver ({}) ...", pathWebdriver);

    System.setProperty(Driver.CHROME.getDriverName(), pathWebdriver);

    final ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
    chromeOptions.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
    chromeOptions.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    chromeOptions.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);

    setLoggingLevel(chromeOptions);
    chromeOptions.addArguments("--ignore-certificate-errors");

    if (Context.isHeadless()) {
        chromeOptions.addArguments("--headless");
    }

    // Proxy configuration
    if (Context.getProxy().getProxyType() != ProxyType.UNSPECIFIED && Context.getProxy().getProxyType() != ProxyType.AUTODETECT) {
        chromeOptions.setCapability(CapabilityType.PROXY, Context.getProxy());
    }

    // add Modifyheader Extensions to Chrome
    if (Context.getWebdriversProperties(MODIFYHEADER_PATH) != null && !"".equals(Context.getWebdriversProperties(MODIFYHEADER_PATH))) {
        chromeOptions.addExtensions(new File(Context.getWebdriversProperties(MODIFYHEADER_PATH)));
    }

    // Set custom downloaded file path. When you check content of downloaded file by robot.
    final HashMap<String, Object> chromePrefs = new HashMap<>();
    chromePrefs.put("download.default_directory", System.getProperty(USER_DIR) + File.separator + DOWNLOADED_FILES_FOLDER);
    chromeOptions.setExperimentalOption("prefs", chromePrefs);

    // Set custom chromium (if you not use default chromium on your target device)
    final String targetBrowserBinaryPath = Context.getWebdriversProperties(TARGET_BROWSER_BINARY_PATH);
    if (targetBrowserBinaryPath != null && !"".equals(targetBrowserBinaryPath)) {
        chromeOptions.setBinary(targetBrowserBinaryPath);
    }

    log.info("addArguments [{}] to webdriver.", Context.getWebdriversProperties(WEBDRIVER_OPTIONS_ADDITIONAL_ARGS));
    for (String additionalArgument : Context.getWebdriversProperties(WEBDRIVER_OPTIONS_ADDITIONAL_ARGS).split(",")) {
        log.info("addArgument [{}] to webdriver.", additionalArgument);
        chromeOptions.addArguments(additionalArgument);
    }

    if (Context.getWebdriversProperties(REMOTE_WEBDRIVER_URL) != null && !"".equals(Context.getWebdriversProperties(REMOTE_WEBDRIVER_URL))
            && Context.getWebdriversProperties(REMOTE_WEBDRIVER_BROWSER_VERSION) != null && !"".equals(Context.getWebdriversProperties(REMOTE_WEBDRIVER_BROWSER_VERSION))
            && Context.getWebdriversProperties(REMOTE_WEBDRIVER_PLATFORM_NAME) != null && !"".equals(Context.getWebdriversProperties(REMOTE_WEBDRIVER_PLATFORM_NAME))) {
        chromeOptions.setCapability("browserVersion", Context.getWebdriversProperties(REMOTE_WEBDRIVER_BROWSER_VERSION));
        chromeOptions.setCapability("platformName", Context.getWebdriversProperties(REMOTE_WEBDRIVER_PLATFORM_NAME));
        try {
            return new RemoteWebDriver(new URL(Context.getWebdriversProperties(REMOTE_WEBDRIVER_URL)), chromeOptions);
        } catch (MalformedURLException e) {
            throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_MESSAGE_REMOTE_WEBDRIVER_URL));
        }
    } else {
        final String withWhitelistedIps = Context.getWebdriversProperties(WITH_WHITE_LISTED_IPS);
        if (withWhitelistedIps != null && !"".equals(withWhitelistedIps)) {
            final ChromeDriverService service = new ChromeDriverService.Builder().withWhitelistedIps(withWhitelistedIps).withVerbose(false).build();
            return new ChromeDriver(service, chromeOptions);
        } else {
            return new ChromeDriver(chromeOptions);
        }
    }
}