我正在发布我 found in here 的解决方案,它帮助我使用文件协议运行 Protractor。
默认情况下,Protractor 使用data:text/html,<html></html> 作为resetUrl,但是从data: 到file: 协议的location.replace 是不允许的(我们会得到“不允许本地资源”错误),所以我们用file: 协议替换resetUrl:
exports.config = {
// ...
baseUrl: 'file:///absolute/path/to/your/project/index.html',
onPrepare: function() {
// By default, Protractor use data:text/html,<html></html> as resetUrl, but
// location.replace from the data: to the file: protocol is not allowed
// (we'll get ‘not allowed local resource’ error), so we replace resetUrl with one
// with the file: protocol (this particular one will open system's root folder)
browser.resetUrl = 'file://';
}
// ...
};
如果您想运行项目文件夹的相对路径,那么您可以只使用 Node.js 工具,因为 Protractor 在 Node.js 环境中运行。例如,__dirname 将返回保存 Protractor 配置文件的目录的绝对路径。结果使用:
exports.config = {
// ...
baseUrl: 'file://' + __dirname + '/spec/support/index.html'
// ...
};
此外,如果您的应用程序向某些端点发出 XHR 请求,而 file: 不允许这些请求,您可能必须使用自定义标志运行您的测试浏览器。就我而言,它是 Chrome:
exports.config = {
// ...
capabilities: {
browserName: 'chrome',
chromeOptions: {
// --allow-file-access-from-files - allow XHR from file://
args: ['allow-file-access-from-files']
}
}
// ...
}