用量角器打开文件 [英] Opening a file with protractor

查看:133
本文介绍了用量角器打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在互联网上找到的每个量角器示例似乎都使用带有网址的 browser.get

  browser.get(的 'http://本地主机:8000'); 

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



但这似乎不起作用。

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

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



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

解决方案

我发布的解决方案我在这里找到这帮助我用文件协议运行Protractor。


默认情况下,Protractor使用 data:text / html, < html>< / html> as resetUrl ,但 location.replace 来自数据:文件:协议是不允许的(我们将得到不允许本地资源错误) ,所以我们用文件替换 resetUrl protocol:




  exports.config = {
// ... ...

baseUrl:'file:// /absolute/path/to/your/project/index.html',

onPrepare:function(){

//默认情况下,量角器使用数据:text / html ,< HTML >< / HTML> as resetUrl,但
// location.replace从data:到文件:protocol不允许
//(我们将'不允许本地资源'错误),所以我们用resetUrl替换一个
//带文件:protocol(这个特定的一个会打开系统的根文件夹)
browser.resetUrl ='file://';
}

// ... ...
};

如果你想运行项目文件夹的相对路径,那么你可以使用Node.js工具,因为Protractor在Node.js环境中运行。例如, __ dirname 将返回保存Protractor配置文件的目录的绝对路径。结果使用:

  exports.config = {
// ...

baseUrl:'file://'+ __dirname +'/ specl / support / index.html'

// ... ...
};

此外,如果您的应用程序对某些端点执行XHR请求,则不允许从 file:,您可能必须使用自定义标志运行测试浏览器。就我而言,它是Chrome:

  exports.config = {
// ... ...

功能:{
browserName:'chrome',
chromeOptions:{
// --allow-file-access-from-files - 允许来自file:// $ b的XHR $ b args:['allow-file-access-from-files']
}
}

// ... ...
}


Every protractor example I can find on the internet seems to use browser.get with a web URI.

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

I'd like to use Selenium to simple navigate to a file:// path so that I don't need a local web server running in order to perform tests. All I need is a simple HTML page and some assets.

That doesn't seem to work though.

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

When I paste that URI into my browser window I get a HTML page. When I try to open it with protractor I get a timeout.

How can I run tests on this page with protractor? The ideal answer will work with a relative file path from the myproject root.

解决方案

I am posting the solution I've found in here which helped me run Protractor with a file protocol.

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:

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

    // ...
};

If you want to run a relative path to your project folder, then you could just use Node.js tools, because Protractor runs in a Node.js environment. For example, __dirname will return an absolute path to a directory where your Protractor config file is saved. As a result use:

exports.config = {
    // ...

    baseUrl: 'file://' + __dirname + '/spec/support/index.html'

    // ...
};

Also, if you application does XHR requests to some endpoints, which are not allowed from file:, you may have to run your test browser with custom flags. In my case it was Chrome:

exports.config = {
    // ...

    capabilities: {
        browserName: 'chrome',
        chromeOptions: {
            // --allow-file-access-from-files - allow XHR from file://
            args: ['allow-file-access-from-files']
        }
    }

    // ...
}

这篇关于用量角器打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆