Javascript 量角器设置全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31203398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Protractor set global variables
提问by pietrovismara
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
我也看了这个问题:protractor angularJS global variables
so i tried to set the variable glob in conf.js in this way:
所以我尝试以这种方式在 conf.js 中设置变量 glob:
exports.config = {
...,
onPrepare: function () {
global.glob = 'test';
}
};
Still, having the same error.
仍然,有同样的错误。
How can i add properly global variables in protractor tests?
如何在量角器测试中正确添加全局变量?
回答by Michael Radionov
It is possible to set globals from the Protractor config file with the help of params
property:
可以在params
属性的帮助下从量角器配置文件设置全局变量:
exports.config = {
// ...
params: {
glob: 'test'
}
// ...
};
And you can access it in the specs using browser.params.glob
.
您可以使用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:
params 对象将直接传递给 Protractor 实例,并且可以作为 browser.params 从您的测试中访问。它是一个任意对象,可以包含您在测试中可能需要的任何内容。这可以通过命令行更改为:
protractor conf.js --params.glob 'other test'
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:
因此,在这种情况下,JavaScript 范围不起作用,传递给的函数browser.executeScript
不会有任何来自规范的闭包变量,例如browser
. 但是您可以显式传递这些变量:
browser.executeScript(function (glob) {
// use passed variables on the page
console.log(glob);
}, browser.params.glob);
回答by alecxe
You can also set global variables in onPrepare()
using global
:
您还可以在onPrepare()
using 中设置全局变量global
:
onPrepare: function () {
global.myVariable = "test";
},
Then, you would just use myVariable
throughout the tests as is.
然后,您只需myVariable
按原样在整个测试中使用即可。
This is actually how protractor
, browser
and other built-in global variables were made available globally:
这实际上是如何protractor
,browser
以及其他内置全局变量都有了全球范围内:
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;
// ...
}
Note that with this approach you are polluting your global scope/namespace, be careful.
请注意,使用这种方法会污染全局范围/命名空间,请小心。
回答by Haseeb
I know I'm a bit late to the answer but here's another way of setting global variables that can be used throughout the file
我知道我的答案有点晚了,但这是设置可在整个文件中使用的全局变量的另一种方法
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
});
});