Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 501
Alex Sayegh Avatar asked May 07 '26 21:05

Alex Sayegh


1 Answers

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);
});
like image 57
Jmr Avatar answered May 10 '26 12:05

Jmr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!