Protractor: Cannot access variables in closure defined in parent function
I have a code in one of my JS files.
// 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 this in my test, for example:
// 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 user.createUser('test@test.com', 'test');
in the callback beforeEach
fails UnknownError: email is not defined
on auth.$createUser(email, password);
.
Why is the variable email
not available in the callback function? Is it possible to propagate arguments passed to the closure function that were passed to the function createUser
?
This is what worked for me based on Andres D's answer ..
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);
};
source to share
Here is a working example I used during the angular meeting:
You can only pass one argument to executeAsyncScript. I would recommend that you 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
source to share