Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protractor times out waiting for sync with page when using $resource

I'm testing Protractor with a small AngularJS app.

This is the test:

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);
  });
});

Controller:

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

Draft service:

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

Running this test using Protractor results in the following error:

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

However, if in the controller I change this line:

$scope.drafts = Draft.query();

to this:

$scope.drafts = [];

The test fails as expected, but more importantly: it does not time out.

With query() enabled, both when running the app manually in a browser and when looking at the browser window opened by Protractor, the data returned by the API is correctly displayed by a repeater.

Why is Protractor not able to synchronize with the page when the service is communicating with the API?

AngularJS is v1.2.0-rc3. Protractor is v0.12.0.

like image 379
Joseph S. Avatar asked Nov 02 '13 12:11

Joseph S.


People also ask

How do you fix a timeout error in Protractor?

In order to modify this behavior to handle timeouts in Protractor Selenium, you need to add allScriptsTimeout (timeout in millis) to the protractor configuration file and this will reflect the change in timeout globally.

Is Protractor going to be deprecated?

Protractor Will Be Deprecated Some of the reasons behind these deprecation plans are: State of Protractor. Protractor is dependent on selenium-webdriver , and is not able to upgrade to the new version without introducing a huge breaking change, and forcing users to do a migration for all their tests.

How do I use browser wait in Protractor?

You can use then() to have a correct order in this case: beforeEach(function() { browser. wait(function() { console. log('1 - BeforeEach WAIT'); return true; }).

What is async and await in Protractor?

js provides native async/await, which means we can get stable e2e test without using control flow in javascript test. Note: To write and run native async/await test, the node.js version should be greater than or equal to 8.0, and Jasmine version should be greater than or equal to 2.7.


1 Answers

This is a known issue, but there is a temporary workaround. Set ptor.ignoreSynchronization = true.

For example:

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);
  });
});
like image 139
Harri Siirak Avatar answered Oct 01 '22 15:10

Harri Siirak