通过 marionette 驱动程序在 Firefox 浏览器中设置代理 [英] Setup proxy in Firefox browser via marionette driver

查看:142
本文介绍了通过 marionette 驱动程序在 Firefox 浏览器中设置代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firefox 47 及以上版本不支持 Selenium Webdriver.我尝试使用 Marionette 驱动程序通过 Firefox 开始我的测试.

Firefox 47 and above do not support Selenium Webdriver. I tried to use a Marionette driver to start my tests via Firefox.

但我在 firefox-profile 中的设置(代理必须设置为 network.proxy.type = 4,自动检测)不再适用于 Firefox 配置(Firefox 打开但所有设置默认设置)) 并且如果没有正确的 PROXY 配置,我的测试将无法运行.

But my settings in firefox-profile (proxy must set to network.proxy.type = 4, auto-detect) is no longer applied to Firefox config (Firefox opens but all settings set by default) and my tests do not work without the right PROXY configuration.

如何通过 Marionette 驱动程序在 Firefox 浏览器中设置代理?

How can I setup proxy in Firefox browser via a Marionette driver?

推荐答案

firefoxProfile 或 Proxy 类的旧技巧不再起作用.您所要做的就是通过 requiredCapabilities 一个具有新 Marionette 代理格式的 JSON:

The old tricks with firefoxProfile or Proxy class do not work any more. All you have to do is pass through requiredCapabilities a JSON with the new Marionette proxy format:

[{proxy={"proxyType":"MANUAL","httpProxy":"host.proxy","httpProxyPort":80,"sslProxy":"host.proxy","sslProxyPort":80}}]

[{proxy={"proxyType":"MANUAL","httpProxy":"host.proxy","httpProxyPort":80,"sslProxy":"host.proxy","sslProxyPort":80}}]

不再是proxyHost:proxyPort"格式,而是 httpProxy=host, httpProxyPort=port.

No more "proxyHost:proxyPort" format, but httpProxy=host, httpProxyPort=port.

JsonObject json = new JsonObject();
json.addProperty("proxyType", "MANUAL");
json.addProperty("httpProxy", aHost);
json.addProperty("httpProxyPort", aPort);
json.addProperty("sslProxy", aHost);
json.addProperty("sslProxyPort", aPort);

required.setCapability("proxy", json);

driver = new FirefoxDriver(service, cap, required);

代码如下:

 import java.io.File;

导入 java.io.IOException;

import java.io.IOException;

import org.apache.commons.lang.SystemUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.GeckoDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonObject;

public class TestProxy
{
  public static void main(String[] args)
  { 
    if (args.length == 0)
    {
      System.out.println("usage: IP port");
      System.exit(-1);
    }

    String proxyServer = args[0];
    int proxyPort = Integer.parseInt(args[1]);

    try
    {
      TestProxy test = new TestProxy();
      test.TestDriver(proxyServer, proxyPort);
    } catch (IOException e)
    {
      e.printStackTrace();
    }    
  }

  private void TestDriver(String aHost, int aPort) throws IOException
  {
    WebDriver driver = null;
    GeckoDriverService service = null;

    if (SystemUtils.IS_OS_LINUX)
    {
      FirefoxBinary firefoxBinary = new FirefoxBinary(new File("/home/ubuntu/firefox/firefox"));

      service = new GeckoDriverService.Builder(firefoxBinary)
          .usingDriverExecutable(new File("/usr/bin/geckodriver"))
          .usingAnyFreePort()
          .usingAnyFreePort()
          .withEnvironment(ImmutableMap.of("DISPLAY",":20"))
          .build();
      service.start();      
    }
    else if (SystemUtils.IS_OS_WINDOWS)
    {
      FirefoxBinary firefoxBinary = new FirefoxBinary(new File("C:\\Program Files\\Mozilla Firefox\\firefox.exe"));

      service = new GeckoDriverService.Builder(firefoxBinary)
          .usingDriverExecutable(new File("C:\\Windows\\geckodriver.exe"))
          .usingAnyFreePort()
          .usingAnyFreePort()
          .build();
      service.start();      
    }
    else
    {
      System.out.println("Unknown operation system");
      System.exit(-1);
    }

    DesiredCapabilities cap = DesiredCapabilities.firefox();
    DesiredCapabilities required = new DesiredCapabilities();

    JsonObject json = new JsonObject();
    json.addProperty("proxyType", "MANUAL");
    json.addProperty("httpProxy", aHost);
    json.addProperty("httpProxyPort", aPort);
    json.addProperty("sslProxy", aHost);
    json.addProperty("sslProxyPort", aPort);

    required.setCapability("proxy", json);

    driver = new FirefoxDriver(service, cap, required);

    driver.navigate().to("https://api.ipify.org/?format=text");

    System.out.println(driver.getPageSource());

    driver.quit();
  }
}

https://github.com/SeleniumHQ/selenium/issues/2963

https://github.com/mozilla/geckodriver/issues/97#issuecomment-255386464

这篇关于通过 marionette 驱动程序在 Firefox 浏览器中设置代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆