Selenium Standalone with CORS

时间:2015-04-05 12:21:21

标签: node.js selenium cors webdriver-io

我目前正在通过nodejs使用webdriver io和selenium standalone,这一切都正常,直到我尝试做基于CORS的请求。我目前正在使用firefox作为测试浏览器,并且由于对另一个域的CORS请求而引发错误。

服务器配置为返回CORS的正确头,并且XHR对象配置为允许CORS。我知道这些工作,因为当我通过firefox / chrome手动使用网站时它按预期工作,但是当我测试时它似乎爆炸,这让我感到困惑,因为服务器和客户端配置为CORS,并且没有HTTPS涉及当前的测试。

为了让CORS在测试浏览器中运行,我还有什么特别之处吗?我知道Selenium在自己的配置文件中启动了浏览器,但我在浏览器设置的配置中找不到与cors相关的任何细节,也找不到与CORS实现相关的任何dependentFeatures。

如果有助于提供答案,我很乐意提供更多信息。

1 个答案:

答案 0 :(得分:1)

首先,CORS正在您的浏览器中工作,但由于Selenium的工作方式以及由于您的应用程序逻辑,它们可能会发生冲突。

我建议只为Selenium打开CORS检查。

对于Chrome,可以通过向功能添加“disable-web-security”来轻松关闭CORS检查。 对于Firefox,它将是“security.fileuri.strict_origin_policy”首选项(不是一项功能)。例如,使用https://github.com/saadtazi/firefox-profile-js

var FirefoxProfile = require('firefox-profile'),
    webdriverjs = require('webdriverjs');

var fp = new FirefoxProfile();
fp.setPreference("security.fileuri.strict_origin_policy", false);

fp.encoded(function(prof) {

    var client = webdriverjs.remote({
        desiredCapabilities: {
            browserName: 'firefox',
            firefox_profile: prof
        }
    });

    client
        .init()
        .url('http://stackoverflow.com/')
        .end();

});