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

Javascript 使用$resource时,量角器在等待与页面同步时超时,javascript,angularjs,angularjs-resource,protractor,Javascript,Angularjs,Angularjs Resource,Protractor,我正在用一个小的AngularJS应用程序测试量角器 这就是测试: 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

我正在用一个小的AngularJS应用程序测试量角器

这就是测试:

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);
  });
});
控制器:

angular.module('myApp.controllers', []).
  controller('DraftsCtrl', ['$scope', 'Draft', function($scope, Draft) {
    $scope.drafts = Draft.query();
}])
草稿服务:

angular.module('myApp.services', ['ngResource']).
  factory('Draft', ['$resource',
    function($resource) {
      return $resource('api/drafts/:id')
    }])
使用量角器运行此测试会导致以下错误:

Error: Timed out waiting for Protractor to synchronize with the page after 11 seconds
但是,如果我在控制器中更改此行:

$scope.drafts = Draft.query();
为此:

$scope.drafts = [];
测试按预期失败,但更重要的是:它没有超时

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

当服务与API通信时,为什么量角器不能与页面同步

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

这是一个,但有一个临时解决办法。设置ptor.ignoreSynchronization=true

例如:


browser.ignoreSynchronization=true对我有效。

我正在使用量角器3.3.0,为了让它在我的测试中起作用,我不得不将忽略同步推迟到完成设置之后

因此,在我的“以前”中,我称我的行动为:

var searchBox = element(by.css('#inpt_search'));
searchBox.sendKeys('test');
然后,我必须等待模拟后端使用
browser.sleep(500)
填充视图(我对这些
sleep
调用不满意,因此如果有人有更好的方法,请发表评论,我无法获得
expectedConditions.presenceOf
作为同一个bug的一部分工作)。 然后在测试中,我设置了
browser.ignoreSynchronization=true
,它取消阻止任何被阻止的内容并查看浏览器内容

describe('standard search', function (){
    beforeEach(function (){
        openApp();
        var searchBox = element(by.css('#inpt_search'));
        searchBox.sendKeys('test');
        browser.sleep(500);
    });
    it('should work or summat', function () {
        browser.ignoreSynchronization = true;
        var fileItems = element.all(by.repeater('item in list'));
        expect(fileItems.count()).toEqual(50);
    });
});

不要使用
browser.ignoreSynchronization
,而是使用
browser.waitForAngularEnabled(*boolean*)
<代码>浏览器。waitForAngularEnabled(false)
设置
浏览器。忽略同步
浏览器。waitForAngularEnabled(true)
设置
浏览器。忽略同步

您还可以将其作为测试套件配置文件的一部分:

onPrepare: function () {
    'use strict';
    browser.waitForAngularEnabled(false);
}

谢谢,我稍后会尝试一下,让你知道它是否有效。我在这里发布之前确实读过GitHub的问题,但我的印象是,只有在定期轮询API时才需要解决方法,而我的测试应用程序没有这样做。当控制器初始化时,它只发送一次API请求。这确实是正确的解决方案。由于量角器0.12.0将量角器实例公开为
browser
,只需添加
browser.ignoreSynchronization=true就足够了
before
element.all(by.repeater(“…”)
.Gragrator.getInstance()未使用(在v0.12.0中被全局浏览器替换),并且已被删除。
onPrepare: function () {
    'use strict';
    browser.waitForAngularEnabled(false);
}