这可能是一个愚蠢的问题,但是我很难找到我需要的答案。我有一个计算中继器的测试。我想返回计数变量,以便将其传递给另一个测试,以用于for循环。

我尝试过的



this.countsInitialBookings = function() {
  var j = 0;
  browser.driver.sleep(3000);
  bookingLists = element.all(by.repeater('booking in bookingsCtrl.bookings'));
  bookingLists.count().then(function(count) {
    //have also tried making bookingCount global and setting value to 0
    var bookingCount = count;
  });
  return bookingCount;
};





这就是吐出来的东西

.{ closure_uid_966269928: 651,
  flow_:
   { events_: {},
     closure_uid_966269928: 1,
     activeFrame_:
      { events_: {},
        closure_uid_966269928: 642,
        flow_: [Circular],
        parent_: [Object],
        children_: [Object],
        lastInsertedChild_: [Object],
        pendingTask_: null,
        isLocked_: false,
        isBlocked_: false,
        pendingCallback: false,
        pendingRejection: false,
        cancellationError_: null },
     schedulingFrame_:
      { events_: {},
        closure_uid_966269928: 642,
        flow_: [Circular],
        parent_: [Object],
        children_: [Object],
        lastInsertedChild_: [Object],
        pendingTask_: null,
        isLocked_: false,
        isBlocked_: false,
        pendingCallback: false,
        pendingRejection: false,
        cancellationError_: null },
     shutdownTask_: null,
     eventLoopTask_: null,
     hold_:
      { _idleTimeout: 2147483647,
        _idlePrev: [Object],
        _idleNext: [Object],
        _idleStart: 99601735,
        _onTimeout: [Function: wrapper],
        _repeat: true },
     yieldCount_: 1 },
  stack_: null,
  parent_:
   { closure_uid_966269928: 649,
     flow_:
      { events_: {},
        closure_uid_966269928: 1,
        activeFrame_: [Object],
        schedulingFrame_: [Object],
        shutdownTask_: null,
        eventLoopTask_: null,
        hold_: [Object],
        yieldCount_: 1 },
     stack_: null,
     parent_:
      { closure_uid_966269928: 647,
        flow_: [Object],
        stack_: null,
        parent_: [Object],
        callbacks_: [Object],
        state_: 'pending',
        handled_: true,
        pendingNotifications_: false,
        value_: undefined },
     callbacks_: [ [Object] ],
     state_: 'pending',
     handled_: true,
     pendingNotifications_: false,
     value_: undefined },
  callbacks_: null,
  state_: 'pending',
  handled_: false,
  pendingNotifications_: false,
  value_: undefined }
.


2 specs, 0 failures
Finished in 17.516 seconds
[launcher] 0 instance(s) of WebDriver still running
[launcher] chrome #1 passed

Process finished with exit code 0

最佳答案

由于.count()是异步的,因此不可能从countsInitialBookings返回整数。取而代之的是,您应该返回承诺,然后可以在以后使用承诺来获得价值。

this.countsInitialBookings = function() {
  var j = 0;
  browser.driver.sleep(3000);
  bookingLists = element.all(by.repeater('booking in bookingsCtrl.bookings'));
  return bookingLists.count()
};


稍后的...

this.countsInitialBookings().then(function (count) {
    console.log(count);
});
// do not attempt to get `count` out here, it's simply not possible.
// It will only be accessible inside the above callback.

关于angularjs - 把promise变成int,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32126948/

10-11 11:59