使用Protractor打开文件

18

我能在互联网上找到的所有量角器示例都似乎使用带有Web URI的browser.get

browser.get('http://localhost:8000');

我想使用Selenium简单地导航到一个file://路径,这样我就不需要运行本地Web服务器来执行测试。我只需要一个简单的HTML页面和一些资源。

然而,似乎这样做不起作用。

browser.get('file:///Users/myusername/dev/mpproject/spec/support/index.html');

当我将该URI粘贴到浏览器窗口时,我会得到一个HTML页面。但是当我尝试用Protractor打开它时,会超时。

我该如何使用Protractor在此页面上运行测试?理想的答案将使用相对于myproject根目录的文件路径。

4个回答

16

我在这里发布了我找到的解决方案(点击此处),该方案帮助我使用文件协议运行Protractor。

默认情况下,Protractor使用data:text/html,<html></html>作为resetUrl,但是从data:协议到file:协议的location.replace不被允许(我们将收到“不允许本地资源”错误),因此我们将resetUrl替换为一个带有file:协议的URL:

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']
        }
    }

    // ...
}

1
感谢您为我解决了这个问题!一切都正是我所需要的。 - Henry Blyth
我该如何在我的测试规范中打开本地html文件呢? 我尝试过不使用browser.getbrowser.get(),假设它会打开指定的baseUrl。但是两者都没有起作用,页面没有加载,因此测试失败。 - CGFoX
1
实际上,这可以使用 browser.get('') 实现,但是要在 onPrepare 中添加 browser.ignoreSynchronization = true; - CGFoX

6

我遇到了同样的错误,通过应用Michael Radionov的修复方法并删除baseUrl进行了修复。这是我的设置:

protractor.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:///';
  }

};

e2etest.js:

'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 */

    });

});

1
这解决了我的问题。我仍然会将它与baseUrl结合使用,以便在测试规范中拥有更好的URL。对我来说,即使没有sleep函数,它也可以正常工作,这可能会加快测试速度。 - CGFoX

1

什么是错误日志?

这可能与加载 Angular 相关。为此,您可以尝试使用 browser.driver.ignoreSynchronization = true;

错误日志肯定有助于尝试理解问题。


-4

我认为有一个笔误。在“get”方法中,您应该在双引号“”中包含URL。

尝试像下面这样使用双引号:

WebDriver driver=new FirefoxDriver();
driver.get('file:///E:/Programming%20Samples/HTML%20Samples/First%20Program.html');

“在JavaScript中,双引号和单引号是等效的”,所以它们不会有任何区别。虽然我尝试过,但并没有起作用。 - David Tuite
据我所知,当您使用dirver.get方法时,应提供双引号... 当我们使用单引号时,它会报告编译错误消息。 - Uday

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接