【问题标题】:Protractor set global variables量角器设置全局变量
【发布时间】:2015-07-03 09:33:28
【问题描述】:

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

var glob = 'test';

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

但这会返回以下错误:

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

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

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

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

还是有同样的错误。

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

【问题讨论】:

    标签: javascript angularjs protractor


    【解决方案1】:

    可以借助 params 属性从 Protractor 配置文件中设置全局变量:

    exports.config = {
        // ...
    
        params: {
            glob: 'test'
        }
    
        // ...
    };
    

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

    请参阅reference config file

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

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

    更新:

    来自docs for browser.executeScript

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

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

    browser.executeScript(function (glob) {
    
        // use passed variables on the page
        console.log(glob);
    
    }, browser.params.glob);
    

    【讨论】:

    • 我正在尝试,唯一的问题是,如果我尝试在 browser.executeScript 的回调中获取浏览器对象,我会得到 UnknownError: unknown error: browser is not defined
    • 我已将browser.executeScript 的工作解决方案添加到答案中。
    • 现在可以完美运行了!谢谢你的时间。
    【解决方案2】:

    您还可以使用globalonPrepare() 中设置全局变量:

    onPrepare: function () {
        global.myVariable = "test";
    },
    

    然后,您将在整个测试中按原样使用myVariable

    其实protractorbrowser等内置全局变量were made available globally是这样的:

    Runner.prototype.setupGlobals_ = function(browser_) {
      // Export protractor to the global namespace to be used in tests.
      global.protractor = protractor;
      global.browser = browser_;
      global.$ = browser_.$;
      global.$$ = browser_.$$;
      global.element = browser_.element;
      global.by = global.By = protractor.By;
    
      // ...
    }
    

    请注意,使用这种方法会污染全局范围/命名空间,请小心。

    【讨论】:

    • 我已经为此苦苦挣扎了一段时间 - 使用这种方法是有效的,除非我做类似 global.foo = require('./foo.js'); 之类的事情,其中​​ foo.js 是像 module.exports = 'asdf'; 这样简单的事情
    【解决方案3】:

    另一种选择是使用过程变量

    量角器是一个节点进程。任何节点进程都可以使用自定义节点变量启动。不确定它是如何在 Windows 中完成的(如果您知道,请发表评论),但对于 mac 和任何 linux/unix 操作系统,您都可以

    使用这样的环境变量启动量角器

    MY_VAR=Dev protractor tmp/config.js
    

    然后它将在您的流程中的任何位置甚至在您的配置中都可用

    console.log(process.env.MY_VAR)
    

    【讨论】:

      【解决方案4】:

      我知道我回答得有点晚了,但这是另一种设置全局变量的方法,可以在整个文件中使用

      describe("Some Global most outer describe", function(){
          var glob = "some global variable";
          var blob = "some other global variable";
      
          it('should test glob', function(){
              expecte(glob).toEqual("some global variable");
          });
      
          it('should test blob', function(){
              expecte(glob).toEqual("some other global variable");
          });
      
          describe('Some inner describe', function(){
              //Some other inner tests can also see glob and blob
          });
      });
      

      【讨论】:

      • globblob 不能在 describe ("Some Global most outer describe", function()) 范围外使用,而是在外部声明它们,在该文件中的任何位置启动和使用它们。
      • 这与我的示例相同,只是您将变量声明移到了 describe 函数中。它不起作用。
      • 如果你在最外层的描述中有其他描述,它就可以工作。这就是我前一段时间使用它的方式......我现在同意这不是最好的解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-18
      • 2020-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多