[英]Protractor: Can not access variables in closure defined in parent function
I have following code in one of my JS files. 我在我的一个JS文件中有以下代码。
// test/lib/UserHelper.js
'use strict';
var Firebase = require('firebase');
exports.createUser = function (email, password) {
browser.executeAsyncScript(function (done) {
var $firebaseSimpleLogin = angular.inject(['ng', 'firebase']).get('$firebaseSimpleLoging');
var firebaseRef = new Firebase('https://urltoapplication.firebaseio.com');
var auth = $firebaseSimpleLogin(firebaseRef);
auth.$createUser(email, password);
done();
});
};
I call it within my test like: 我在我的测试中称之为:
// test/settings/company.spec.js
'use strict';
var user = require('../lib/UserHelper');
describe('company specs', function () {
beforeEach(function () {
user.createUser('test@test.com', 'test');
});
});
The call to user.createUser('test@test.com', 'test');
对
user.createUser('test@test.com', 'test');
的调用user.createUser('test@test.com', 'test');
in the beforeEach
callback fails with UnknownError: email is not defined
at auth.$createUser(email, password);
在
beforeEach
回调失败时出现UnknownError: email is not defined
在auth.$createUser(email, password);
UnknownError: email is not defined
auth.$createUser(email, password);
. 。
Why is email
variable not accessible in the callback function? 为什么回调函数中无法访问
email
变量? Can it be made possible to propagate arguments passed to the closing function that were passed to createUser
function? 是否可以传播传递给关闭函数的参数传递给
createUser
函数?
This is what worked for me based on answer of Andres D. 根据Andres D的回答,这对我有用。
exports.createUser = function (data) {
browser.executeAsyncScript(function (data, done) {
var $firebaseSimpleLogin = angular.inject(['ng', 'firebase']).get('$firebaseSimpleLoging');
var firebaseRef = new Firebase('https://urltoapplication.firebaseio.com');
var auth = $firebaseSimpleLogin(firebaseRef);
auth.$createUser(data.email, data.password);
done();
}, data);
};
Here is a working example that I used during an angular meetup: 这是我在角度聚会期间使用的一个工作示例:
You can only pass one argument to executeAsyncScript. 您只能将一个参数传递给executeAsyncScript。 I would recommend you to pass an object if you need to pass more than one value:
如果您需要传递多个值,我建议您传递一个对象:
module.exports = {
create: function(data) {
return browser.executeAsyncScript(function(data, callback) {
var api = angular.injector(['ProtractorMeetupApp']).get('apiService');
api.member.save(data, function(newItem) {
callback(newItem._id);
})
}, data);
}
};
create({email: 'sdf@sdf.com', password: 'sfd'}).then(function(response){
// Handle response here.
})
https://github.com/andresdominguez/protractor-meetup/blob/master/test/e2e/api-helper.js#L12 https://github.com/andresdominguez/protractor-meetup/blob/master/test/e2e/api-helper.js#L12
The problem here is that executeAsyncScript is actually performed in the browser that selenium is running, and therefor does not pass variables from the above scope through it. 这里的问题是executeAsyncScript实际上是在运行selenium的浏览器中执行的,因此不会通过它传递来自上述范围的变量。
I'm not sure if anyone knows a way to pass variables through this way. 我不确定是否有人知道通过这种方式传递变量的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.