我们使用 Chutzpah 测试适配器在 Visual Studio 中运行一些 QUnit javascript 测试。一切都工作正常,直到我们最近更改了 api(由 js 文件测试的 API),并在 UserAgent http header 上添加了一些验证。当我尝试更新测试以更改/模拟用户代理时,我意识到即使通过 overriding the default browser property 也无法直接实现.
经过几天的搜寻,我终于知道到底发生了什么。 Chutzpah 正在创建一个 phantomjs page运行测试文件的对象。这是在位于 Chutzpah adapter installation path 的基本 javascript 文件 (chutzpahRunner.js) 上完成的。 。这些是文件中的最后几行,有效地启动测试:
...
// Allows local files to make ajax calls to remote urls
page.settings.localToRemoteUrlAccessEnabled = true; //(default false)
// Stops all security (for example you can access content in other domain IFrames)
page.settings.webSecurityEnabled = false; //(default true)
page.open(testFile, pageOpenHandler);
...
Phatomjs 支持通过在页面 settings object 中指定来更改用户代理 header 。如果我在我的计算机中编辑这个 chutzpahRunner.js 文件,并在那里手动设置用户代理,如下所示:
page.settings.userAgent = "MyCustomUserAgent";
我的测试再次开始工作。问题是这并不在项目本身中,因此无法与团队的其他成员共享。
是否可以更改 Chutzpah 创建的 phantomjs 对象的属性来运行测试?我想从我自己的测试中更改它们,或者从我可以嵌入管道的另一个脚本文件中更改它们。
最佳答案
如果不更改 Chutzpah 中的代码,就无法在 PhantomJS 对象上设置这些属性。请通过 https://github.com/mmanela/chutzpah 提交问题请求此功能,然后 fork/patch Chutzpah 来添加它(或者等待项目的开发人员希望能够实现此功能)。
更新: 我推了一个fix for this issue 。发布后,您可以在 Chutzpah.json 文件中使用以下内容:
{
"userAgent": "myUserAgent"
}
关于javascript - 如何使用 Chutzpah 更改底层 Phantomjs 对象设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26467096/