【问题标题】:Opening a file with protractor用量角器打开文件
【发布时间】:2014-09-09 13:28:32
【问题描述】:

我可以在互联网上找到的每个量角器示例似乎都使用带有网络 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 页面。当我尝试用量角器打开它时,我得到了超时。

如何使用量角器在此页面上运行测试?理想的答案将使用来自 myproject 根目录的相对文件路径。

【问题讨论】:

    标签: javascript selenium selenium-webdriver protractor


    【解决方案1】:

    我正在发布我 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']
            }
        }
    
        // ...
    }
    

    【讨论】:

    • 祝福你这个美妙的修复!一切正是我所需要的
    • 那么如何在我的测试规范中打开本地 html 文件?我尝试不使用不带参数的browser.getbrowser.get(),假设它会打开指定的baseUrl。但两者都不起作用,只是没有加载页面,因此测试失败。
    • 实际上,它与browser.get('') 一起工作,但只有在onPrepare 中添加browser.ignoreSynchronization = true; 之后。
    【解决方案2】:

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

    量角器.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 */
    
        });
    
    });
    

    【讨论】:

    • 这解决了我的问题。我仍然会将它与 baseUrl 结合起来,以便在测试规范中拥有更好的 URL。对我来说,它也可以在没有睡眠功能的情况下工作,这可能会加快测试速度。
    【解决方案3】:

    什么是错误日志?

    这可能与“加载”角度有关。为此,您可以尝试 browser.driver.ignoreSynchronization = true;

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

    【讨论】:

      【解决方案4】:

      我认为有错字。在“get”方法中,您应该将 URL 包含在双引号“”中。

      尝试使用如下双引号:

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

      【讨论】:

      • Double and single quotes are equivalent in JavaScript 这样不会有什么不同。我确实尝试过,但它不起作用。
      • 据我所知,当您使用 dirver.get 方法时,您应该提供双引号......当我们使用单引号时,它会抱怨编译错误消息。
      猜你喜欢
      • 1970-01-01
      • 2016-01-11
      • 2020-04-11
      • 1970-01-01
      • 1970-01-01
      • 2014-09-22
      • 1970-01-01
      • 2020-05-17
      相关资源
      最近更新 更多