Yaelet
Yaelet

Reputation: 160

protractor test before initial promises resolve

I want to test the state of the controller after initialization completes, but before promises resolve.

I have a button which is disabled (by class) until loading completes:

<button ng-class="{disabled: isLoading}">Show</button>

When the controller is initialized, an ajax call is made and when it resolves, the isLoading is set to false:

$scope.isLoading = true;
$http.get('/list')
  .then(function(response) {
     $scope.list = response.data;
     $scope.isLoading = false;
  });

When I test the button using protractor, there is no disabled class.

it('should enable the button after load completes', function() {
    element(by.buttonText('Show')).getAttribute('class')
        .then(function(buttonClasses) {
            expect(buttonClasses).toContain('disabled');
        });
});

I modified the button to have another class, just to see I'm not going bonkers:

<button ng-class="{disabled: isLoading, enabled: !isLoading}">Show</button>

and the test:

it('should show the select tables list after clicking the Change button', function() {
    element(by.buttonText('Show')).getAttribute('class')
        .then(function(buttonClasses) {
            expect(buttonClasses).toContain('enabled');
        });
});

Now the test passes.

I'm guessing that waitForAngular is part of the controller initialization process. How do I test the state of the button Before the promise resolves?

Upvotes: 0

Views: 124

Answers (1)

Michal Charemza
Michal Charemza

Reputation: 27062

You can set browser.ignoreSynchronization = true before finding the element/class to not wait for promises in the app to resolve

it('should show the select tables list after clicking the Change button', function() {
  browser.ignoreSynchronization = true; 
  expect(element(by.buttonText('Show')).getAttribute('class')).toContain('disabled');
  browser.ignoreSynchronization = false;
});

Note you rarely need to use .then callbacks for expect, since expect handles unwraping of the promises.

Upvotes: 1

Related Questions