14

我正在用一个小型 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);
  });
});

控制器:

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')
    }])

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

Error: Timed out waiting for Protractor to synchronize with the page after 11 seconds

但是,如果在控制器中我更改此行:

$scope.drafts = Draft.query();

对此:

$scope.drafts = [];

测试按预期失败,但更重要的是:它不会超时。

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

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

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

4

4 回答 4

26

这是一个已知问题,但有一个临时解决方法。设置ptor.ignoreSynchronization = true

例如:

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

  beforeEach(function() {
    ptor = protractor.getInstance();
    ptor.ignoreSynchronization = true;
  });

  it('should count the number of drafts', function() {
    ptor.get('#/');
    draftList = element.all(by.repeater('newsletter in drafts'));
    expect(draftList.count()).toEqual(2);
  });
});
于 2013-11-07T22:55:36.843 回答
3

browser.ignoreSynchronization = true;为我解决了。

于 2015-10-30T07:35:46.253 回答
1

我正在使用 Protractor 3.3.0 并且要让它在我的测试中工作,我必须将忽略同步推迟到完成设置之后。

因此,在我的 beforeEach 中,我调用了我的操作:

var searchBox = element(by.css('#inpt_search'));
searchBox.sendKeys('test');

然后我必须等待模拟后端填充视图(我对这些sleep调用不满意,所以如果有人有更好的方法请评论,我无法开始expectedConditions.presenceOf工作,因为它是同一个错误的一部分)使用browser.sleep(500). 然后在测试中,我设置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);
    });
});
于 2016-06-04T10:27:23.747 回答
1

而不是使用browser.ignoreSynchronization,使用browser.waitForAngularEnabled(*boolean*)browser.waitForAngularEnabled(false)设置browser.ignoreSynchronizationtruebrowser.waitForAngularEnabled(true)设置browser.ignoreSynchronizationfalse

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

onPrepare: function () {
    'use strict';
    browser.waitForAngularEnabled(false);
}
于 2017-09-26T21:56:48.157 回答