相关疑难解决方法(0)

使用$ resource时,量角器会等待与页面同步

我正在使用一个小型AngularJS应用程序测试Protractor.

这是测试:

describe('Testing Protractor', function() {
  var draftList;

  it('should count the number of drafts', function() {
    browser.get('#/');
    draftList = element.all(by.repeater('newsletter in drafts'));
    expect(draftList.count()).toEqual(2);
  });
});
Run Code Online (Sandbox Code Playgroud)

控制器:

angular.module('myApp.controllers', []).
  controller('DraftsCtrl', ['$scope', 'Draft', function($scope, Draft) {
    $scope.drafts = Draft.query();
}])
Run Code Online (Sandbox Code Playgroud)

服务草案:

angular.module('myApp.services', ['ngResource']).
  factory('Draft', ['$resource',
    function($resource) {
      return $resource('api/drafts/:id')
    }])
Run Code Online (Sandbox Code Playgroud)

使用Protractor运行此测试会导致以下错误:

Error: Timed out waiting for Protractor to synchronize with the page after 11 seconds
Run Code Online (Sandbox Code Playgroud)

但是,如果在控制器中我改变了这一行:

$scope.drafts = Draft.query();
Run Code Online (Sandbox Code Playgroud)

对此:

$scope.drafts = [];
Run Code Online (Sandbox Code Playgroud)

测试按预期失败,但更重要的是:它没有超时.

启用query()后,无论是在浏览器中手动运行应用程序还是查看Protractor打开的浏览器窗口时,API返回的数据都由转发器正确显示.

为什么当服务与API通信时,Protractor无法与页面同步?

AngularJS是v1.2.0-rc3.量角器是v0.12.0.

javascript angularjs angularjs-resource protractor

14
推荐指数
1
解决办法
2万
查看次数