量角器设置全局变量
Protractor set global variables
我正在尝试在量角器上设置一个全局变量以在所有描述块中使用。
var glob = 'test';
describe('glob test', function () {
it('should set glob', function () {
browser.get('http://example.com/test');
browser.executeScript(function () {
window.glob = glob;
});
});
});
但是这个returns我出现了以下错误:
Message:
[firefox #2] UnknownError: glob is not defined
我也看了这个问题:protractor angularJS global variables
所以我尝试以这种方式在 conf.js 中设置变量 glob:
exports.config = {
...,
onPrepare: function () {
global.glob = 'test';
}
};
还是一样的错误。
如何在量角器测试中正确添加全局变量?
可以在 params
属性:
的帮助下从 Protractor 配置文件设置全局变量
exports.config = {
// ...
params: {
glob: 'test'
}
// ...
};
您可以使用 browser.params.glob
在规范中访问它。
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'
更新:
来自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.
因此,在这种情况下,JavaScript 作用域不起作用,传递给 browser.executeScript
的函数不会像 browser
那样具有来自规范的任何闭包变量。但是您可以显式传递这些变量:
browser.executeScript(function (glob) {
// use passed variables on the page
console.log(glob);
}, browser.params.glob);
您还可以使用 global
:
在 onPrepare()
中设置全局变量
onPrepare: function () {
global.myVariable = "test";
},
然后,您只需在整个测试过程中按原样使用 myVariable
。
其实就是这样 protractor
, browser
等内置全局变量 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;
// ...
}
请注意,使用这种方法会污染全局 scope/namespace,请小心。
我知道我回答的有点晚了,但这是另一种设置全局变量的方法,可以在整个文件中使用
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
});
});
另一种选择是使用过程变量
量角器是一个节点进程。任何节点进程都可以使用自定义节点变量启动。不确定在 windows 中是如何完成的(如果您知道,请发表评论)但是对于 mac 和任何 linux/unix OS 您可以
像这样使用环境变量启动量角器
MY_VAR=Dev protractor tmp/config.js
然后它将在您的流程中的任何地方可用,甚至在您的配置中
console.log(process.env.MY_VAR)
我正在尝试在量角器上设置一个全局变量以在所有描述块中使用。
var glob = 'test';
describe('glob test', function () {
it('should set glob', function () {
browser.get('http://example.com/test');
browser.executeScript(function () {
window.glob = glob;
});
});
});
但是这个returns我出现了以下错误:
Message:
[firefox #2] UnknownError: glob is not defined
我也看了这个问题:protractor angularJS global variables
所以我尝试以这种方式在 conf.js 中设置变量 glob:
exports.config = {
...,
onPrepare: function () {
global.glob = 'test';
}
};
还是一样的错误。
如何在量角器测试中正确添加全局变量?
可以在 params
属性:
exports.config = {
// ...
params: {
glob: 'test'
}
// ...
};
您可以使用 browser.params.glob
在规范中访问它。
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'
更新:
来自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.
因此,在这种情况下,JavaScript 作用域不起作用,传递给 browser.executeScript
的函数不会像 browser
那样具有来自规范的任何闭包变量。但是您可以显式传递这些变量:
browser.executeScript(function (glob) {
// use passed variables on the page
console.log(glob);
}, browser.params.glob);
您还可以使用 global
:
onPrepare()
中设置全局变量
onPrepare: function () {
global.myVariable = "test";
},
然后,您只需在整个测试过程中按原样使用 myVariable
。
其实就是这样 protractor
, browser
等内置全局变量 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;
// ...
}
请注意,使用这种方法会污染全局 scope/namespace,请小心。
我知道我回答的有点晚了,但这是另一种设置全局变量的方法,可以在整个文件中使用
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
});
});
另一种选择是使用过程变量
量角器是一个节点进程。任何节点进程都可以使用自定义节点变量启动。不确定在 windows 中是如何完成的(如果您知道,请发表评论)但是对于 mac 和任何 linux/unix OS 您可以
像这样使用环境变量启动量角器
MY_VAR=Dev protractor tmp/config.js
然后它将在您的流程中的任何地方可用,甚至在您的配置中
console.log(process.env.MY_VAR)