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);
};

      

+3


source to share


2 answers


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

+6


source


The problem is that executeAsyncScript is actually executed in the browser that the selenium is running on and therefore it doesn't skip variables from the above scope.



I'm not sure if anyone knows of a way to pass variables this way.

+1


source







All Articles