量角器设置全局变量 [英] Protractor set global variables

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

问题描述

我正在尝试在量角器上设置一个全局变量以在所有描述块中使用.

var glob = 'test';描述('全局测试',函数(){it('应该设置 glob', function () {browser.get('http://example.com/test');browser.executeScript(function () {window.glob = glob;});});});

但这会返回以下错误:

消息:[firefox #2] UnknownError: glob 未定义

我也看了这个问题:protractor angularJS global variables

所以我尝试以这种方式在 conf.js 中设置变量 glob:

exports.config = {...,onPrepare: 函数 () {global.glob = '测试';}};

仍然有同样的错误.

如何在量角器测试中正确添加全局变量?

解决方案

params 属性的帮助下,可以从 Protractor 配置文件中设置全局变量:

exports.config = {//...参数:{glob: '测试'}//...};

您可以使用 browser.params.glob 在规范中访问它.

请参阅参考配置文件.><块引用>

params 对象将直接传递给 Protractor 实例,并且可以作为 browser.params 从您的测试中访问.它是一个任意对象,可以包含您在测试中可能需要的任何内容.这可以通过命令行更改为:

量角器 conf.js --params.glob '其他测试'

更新:

来自 browser.executeScript 的文档:

<块引用>

如果脚本作为函数对象提供,该函数将被转换为字符串以注入目标窗口.除了脚本之外提供的任何参数都将作为脚本参数包含在内,并且可以使用参数对象进行引用.

因此,在这种情况下,JavaScript 作用域不起作用,传递给 browser.executeScript 的函数不会像 browser 那样具有来自规范的任何闭包变量.但是您可以显式传递这些变量:

browser.executeScript(function (glob) {//在页面上使用传递的变量控制台日志(glob);}, browser.params.glob);

I am trying to set a global variable on protractor to use in all describe blocks.

var glob = 'test';

describe('glob test', function () {
    it('should set glob', function () {
        browser.get('http://example.com/test');
        browser.executeScript(function () {
            window.glob = glob;
        });
    });    
});

But this returns me the following error:

Message:
[firefox #2]      UnknownError: glob is not defined

I also looked at this question: protractor angularJS global variables

so i tried to set the variable glob in conf.js in this way:

exports.config = {
  ...,
  onPrepare: function () {
      global.glob = 'test';
  }
};

Still, having the same error.

How can i add properly global variables in protractor tests?

解决方案

It is possible to set globals from the Protractor config file with the help of params property:

exports.config = {
    // ...

    params: {
        glob: 'test'
    }

    // ...
};

And you can access it in the specs using browser.params.glob.

See the reference config file.

The params object will be passed directly to the Protractor instance, and can be accessed from your test as browser.params. It is an arbitrary object and can contain anything you may need in your test. This can be changed via the command line as:

protractor conf.js --params.glob 'other test'

Update:

From the docs for browser.executeScript:

If the script is provided as a function object, that function will be converted to a string for injection into the target window. Any arguments provided in addition to the script will be included as script arguments and may be referenced using the arguments object.

So, JavaScript scope in this case does not work, you function that is passed to browser.executeScript won't have any closure variables from spec like browser. But you can pass these variables explicitly:

browser.executeScript(function (glob) {

    // use passed variables on the page
    console.log(glob);

}, browser.params.glob);

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

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