Hitesh Patwari
Hitesh Patwari

Reputation: 19

Protractor waitForAngular() not working for an Async Call

I am working on creating Mock test cases for my application. I don't want to call our Service end-points just for testing, as they have the dedicated tests for them. So, I am mocking the data and intercepting the request. But it seems that the protractor test case finish too early for the request to respond. I have tried waitForAngular but it does not seem to have any effect on the test. The test still fails.

Here is my code :

  // Adding a metric
 it('should allow the user to add a metric', function() {
    var count = 0;
    element(by.model('p.title')).clear();
    element(by.cssContainingText('.btn', 'Add Metric')).click();

    browser.waitForAngular();

    element.all(by.cssContainingText('.btn-success', 'Add')).first().click(); // Add first metric on list

   // If adding metric fails, retry 5 times before giving up
  while (count < 3 && element(by.buttonText('Retry')).isDisplayed()) {
     element(by.buttonText('Retry')).click();
     count++;
  }

  // If we were able to load the metric data
  if (count < 3) {
     element(by.css('.close')).click();
     expect(element.all(by.repeater('metric in p.metrics')).count()).toBe(1);
  } else {
     element(by.buttonText('Cancel')).click();
  }

}, 20000);

Can anybody please tell me, what I am doing wrong here ?

Upvotes: 1

Views: 1281

Answers (1)

Andres D
Andres D

Reputation: 8900

Your while will not work because isDisplayed() returns a promise. The promise will always evaluate to truthy and your loop will always click().

try using wait.

browser.wait(function() {
  return element(by.buttonText('Retry')).isDisplayed();
});

Upvotes: 1

Related Questions