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

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

问题描述

我想设置量角器一个全局变量在使用所有描述块。

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

我也看了一下这个问题:

所以我试图设置conf.js变量水珠是这样的:

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?

推荐答案

这是可以设置从量角器配置文件全局与 PARAMS 的帮助属性:

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

exports.config = {
    // ...

    params: {
        glob: 'test'
    }

    // ...
};

和可以使用的规格访问 browser.params.glob

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

查看参考配置文件

params对象将被直接传递给量角器实例,并且可以从测试为browser.params进行访问。这是一个任意对象,并且可以包含任何你可能需要在您的测试。这可以通过命令行被改变为:

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:

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

更新:

从<一个href=\"http://angular.github.io/protractor/#/api?view=webdriver.WebDriver.prototype.executeScript\">docs为 browser.executeScript

如果脚本作为函数对象提供,该函数将被转换为字符串注射到目标窗口。除了剧本提供的任何参数将作为脚本参数,并可以使用arguments对象引用。

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.

因此​​,在这种情况下,JavaScript的范围不工作,你的功能传递给 browser.executeScript 不会从任何规格封变量,如浏览器。但是你可以明确地传递这些变量:

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天全站免登陆