简体   繁体   English

量角器:无法访问父函数中定义的闭包中的变量

[英]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 definedauth.$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.

相关问题 函数可以访问在其自己的父函数中定义的变量吗? - Can functions access variables defined in its own parent function? 如何从闭包中的函数内部访问闭包中的变量? - How can I access variables in a closure from inside of a function in that closure? 为什么我必须在闭包中声明一个函数,访问闭包中定义的变量? - Why do I have to declare a function within a closure access variables defined in the closure? 用闭包创建的私有函数如何访问构造函数中定义的变量? - How private function created with closure can access variable defined in constructor? 回调函数访问闭包变量? - callback function access to closure variables? 可以回调函数访问父函数变量 - can callback functions access parent function variables 我的函数以某种方式无法访问其父闭包并且缺少变量。怎么样? - My function somehow doesn’t have access to its parent closure & is missing variables. How? 在尝试验证功能中无法访问javascript父函数 - 可能需要关闭 - Can't access javascript parent function in attempted validation function - maybe closure needed JavaScript闭包无法访问外部变量 - JavaScript closure can't access outer variables 为什么我这个Closure的内部函数无法访问外部函数变量? - Why the inner function of this Closure I did can not access to the outer function variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM