在 nightwatch.js 中设置代理

Set proxy in nightwatch.js

我正在 Node.js 应用程序中使用 nightwatch.js 编写集成测试。对于特定的测试用例,我希望 nightwatch 通过 proxy 连接。这样做的正确方法是什么?我无法从其 official documentation, or from its Google Group.

中找到任何内容

Selenium 文档建议按照 here 所述在 webdriver 实例上设置它。我不确定如何通过守夜人执行此操作。

nightwatch.json配置文件中,您应该可以在desiredCapabilities中设置代理参数:

"chrome" : {
  "desiredCapabilities": {
    "browserName": "chrome",
    "javascriptEnabled": true,
    "acceptSslCerts": true,
    "chromeOptions" : {
      "args" : [
        "disable-extensions",
        "start-maximized"
      ]
    },
    "proxy": {
      "proxyType": "manual",
      "httpProxy": "your_proxy:8080"
    }
  }
},

查看此文档:https://code.google.com/p/selenium/wiki/JsonWireProtocol#Proxy_JSON_Object

我在搜索 socks5 代理解决方案时偶然发现了这个问题。 当我使用 socksProxy 属性 使用 JsonWireProtocol 文档中的实现时,我总是遇到以下错误:

message: 'unknown error: cannot parse capability: proxy from unknown error:
proxyType is \'manual\' but no manual proxy capabilities were found

使用通过 proxy.pac 文件配置的 socks5 代理 - proxyType: 'pac' 使用 proxyAutoconfigUrl 没有任何问题。但这不适合我的用例。

经过一番摸索,我终于找到了解决该问题的两个方法:

  1. 为 chromedriver 使用 CLI 参数
desiredCapabilities: {
  browserName: 'chrome',
  /* … */
  chromeOptions: {
    args: [
      '--proxy-server=socks5://proxy_url:proxy_port'
    ]
  }
}

*编辑: 看起来这已被删除
2. 使用 sslProxy 属性
由于 socks 代理在理论上只不过是一个 ssl 隧道,我想我可以再试一次 属性。使它最终起作用的解决方案如下所示:

desiredCapabilities: {
  browserName: 'chrome',
  /* … */
  proxy: {
    proxyType: 'manual',
    sslProxy: 'socks5://proxy_url:proxy_port'
  }
}

希望这个回答对寻求 socks5 代理相关帮助的任何人有所帮助。 :)
但更重要的是,chromedriver 将在未来正确实现 JsonWireProtocol。

Nightwatch 在开始使用 proxy-agent 而不是 http-proxy 时改变了 nightwatch.conf.js 文件中代理对象的工作方式,不幸的是它似乎没有在任何地方记录。但它仍然存在你只需要在代理对象中传入不同的参数即可。它接受的 'protocols' 列在 proxy-agent github 中,请参见下面的示例。

firefox: {
      desiredCapabilities: {
        browserName: 'firefox',
        version: 'latest',
      },
      proxy: {
        host:'127.0.0.1',
        port:8001,
        protocol: 'http',
      },
    },