Javascript 用量角器打开文件
我在互联网上找到的每个量角器示例似乎都使用Javascript 用量角器打开文件,javascript,selenium,selenium-webdriver,protractor,Javascript,Selenium,Selenium Webdriver,Protractor,我在互联网上找到的每个量角器示例似乎都使用浏览器。使用web URI获取 browser.get('http://localhost:8000'); 我想使用Selenium简单地导航到文件://路径,这样我就不需要运行本地web服务器来执行测试。我只需要一个简单的HTML页面和一些资源 但这似乎不起作用 browser.get('file:///Users/myusername/dev/mpproject/spec/support/index.html'); 当我将URI粘贴到浏览器窗口中
浏览器。使用web URI获取
browser.get('http://localhost:8000');
我想使用Selenium简单地导航到文件://
路径,这样我就不需要运行本地web服务器来执行测试。我只需要一个简单的HTML页面和一些资源
但这似乎不起作用
browser.get('file:///Users/myusername/dev/mpproject/spec/support/index.html');
当我将URI粘贴到浏览器窗口中时,我会得到一个HTML页面。当我试图用量角器打开它时,我得到一个超时
如何使用量角器在此页面上运行测试?理想的答案是使用来自myproject
根目录的相对文件路径。我认为有打字错误。在“get”方法中,您应该在双引号“”中包含URL
尝试使用双引号,如下所示:
WebDriver driver=new FirefoxDriver();
driver.get('file:///E:/Programming%20Samples/HTML%20Samples/First%20Program.html');
错误日志是什么
这可能与“加载”角度有关。你可以试试看
browser.driver.ignoreSynchronization=true
错误日志肯定会有助于理解问题。我正在发布解决方案,它帮助我使用文件协议运行量角器
默认情况下,量角器使用数据:text/html,
作为重置URL
,但使用位置。将从数据:
替换为文件:
协议是不允许的(我们将得到“不允许的本地资源”错误),因此我们将重置URL
替换为文件:
协议:
此外,如果您的应用程序对某些端点执行XHR请求(不允许从文件:
),则可能必须使用自定义标志运行测试浏览器。在我的例子中,是Chrome:
exports.config = {
// ...
capabilities: {
browserName: 'chrome',
chromeOptions: {
// --allow-file-access-from-files - allow XHR from file://
args: ['allow-file-access-from-files']
}
}
// ...
}
我也有同样的错误,通过应用Michael Radinov的修复程序,但删除了baseUrl来修复。以下是我的设置:
progrator.config.js:
exports.config = {
capabilities: {
browserName: 'chrome'
},
specs: [
'*.js'
],
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.ignoreSynchronization = true;
browser.waitForAngular();
browser.sleep(500);
browser.resetUrl = 'file:///';
}
};
所以这没什么区别。我确实试过了,但没有成功。据我所知,当你使用dirver.get方法时,你应该提供双引号。。。当我们使用单引号时,它会抱怨一条编译错误消息。祝福你这奇妙的修复!一切都正是我所需要的。那么,如何在我的测试规范中打开本地html文件呢?我试着不使用browser.get
和browser.get()
而不使用参数,假设它会打开指定的baseUrl。但是两者都不起作用,它只是不加载页面,因此测试失败。实际上,它与browser.get(“”)
一起工作,但只有在添加browser.ignoreSynchronization=true代码>内部onPrepare
。这解决了我的问题。我仍然会将它与baseUrl相结合,以便在测试规范中有更好的URL。对我来说,它在没有睡眠功能的情况下也能工作,这可能会加快测试速度。
exports.config = {
// ...
capabilities: {
browserName: 'chrome',
chromeOptions: {
// --allow-file-access-from-files - allow XHR from file://
args: ['allow-file-access-from-files']
}
}
// ...
}
exports.config = {
capabilities: {
browserName: 'chrome'
},
specs: [
'*.js'
],
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.ignoreSynchronization = true;
browser.waitForAngular();
browser.sleep(500);
browser.resetUrl = 'file:///';
}
};
'use strict';
describe("Buttons' tests are started", function() {
it('Should retrieve 20 records into table', function() {
browser.get('file:///C:/Users/path_to_file/index.html');
/* Test codes here */
});
});