量角器:在一个 js 中初始化全局变量并在其他 js 中使用相同的变量 [英] Protractor: initialise Global variable in one js and use the same var in other js

查看:66
本文介绍了量角器:在一个 js 中初始化全局变量并在其他 js 中使用相同的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用量角器来测试 angularJS 应用程序.我在我的测试 js 中声明了一个变量logDir",并想在其他 js 中将它用作全局变量.

I am using protractor to test angularJS apps. I have declared a variable 'logDir' in my test js and want to use it as global variable in other js.

//test.js

var regex = /(.*automation)/;
var result = regex.exec(__dirname);
var baseDir = result[0]; 
var libDir = baseDir + '/lib';

我想在其他js中全局使用libDir变量.

I want to use libDir variable globally in other js.

我是量角器的新手,需要帮助.如果需要更多说明,请告诉我.

I am new to protractor and need help. If more clarification needed, let me know.

提前致谢.

推荐答案

您可以将您需要的变量全局转储到 Protractor browser 对象,并在 Protractor 运行环境中的任何地方使用

You can dump the variable you need globally onto Protractor browser object and use it anywhere in the Protractor run Environment

browser.libDir = baseDir + '/lib';

然后如果您在任何其他测试用例中需要它,您可以直接将其用作 browser.libDir

And then if you need it in any other test case you can directly use it as browser.libDir

一种更专业的处理方法是在 onPrepare() 中声明全局变量.

A more professional way to handle this would be declaring global variables in onPrepare().

  onPrepare: function() {
       browser.libDir = baseDir + '/lib'
  },

这篇关于量角器:在一个 js 中初始化全局变量并在其他 js 中使用相同的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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