Alex Sayegh
Alex Sayegh

Reputation: 243

Protractor wait for async function used in expect check

Hei,

I'm using protractor to run e2e tests, and would like to have something of the sort:

expect(element(by.model('someModel')).getText()).toContain(asyncMethodResult());

I would like to avoid doing:

asyncMethodResult().then(function(result){
    expect(element(by.model('someModel').getText()).toContain(result);
});

Also possibly run an async request in a beforeEach which also 'blocks' further execution until done

Is that doable? is something like https://www.npmjs.com/package/wait.for the only way to do so?

[Update] For the sake of answer:

it('test spec', function () {
   var expectFn = function (x, t) {
      var deferred = Q.defer();
      setTimeout(function () {
        console.log('Timeout complete:', x, t);
        deferred.resolve(x);
      }, t);
      return deferred.promise;
   };
   expect(expectFn(3, 1000)).toEqual(expectFn(4, 2000));
}

Passes or fails correctly (depending on x values) if:

browser.manage().timeouts().implicitlyWait(Y); //where Y > promise timeout value

In this case protractor waits for 'Y' millis, then compares the values, otherwise compares always passes.

If the implicitWait is less than the timeout, then it always passes immetiatly

Upvotes: 3

Views: 2183

Answers (1)

Jmr
Jmr

Reputation: 12108

Your first case (expect(...).toContain(asyncMethodResult()) should already work. See an example test here.

It is also possible to run an async request in a beforeEach block. Here's an example:

beforeEach(function(done) {
    doSyncStuff();
    doAsyncStuff.then(done);
});

Upvotes: 3

Related Questions