The protractor waits for an asynchronous function to use when checking expectations

Hei,

I am using a protractor to run e2e tests and would like to have something like:

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

      

I would like to avoid:

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

      

It is also possible to run an async request in a beforeEach which will also block further execution until completion

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

[Update] 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 the values โ€‹โ€‹of x) if:

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

      

In this case, the transporter waits for "Y" millis, then compares the values, otherwise the comparison always passes.

If implicitWait is less than the timeout, then it always goes slowly

+3


source to share


1 answer


Your first case ( expect(...).toContain(asyncMethodResult())

should work. See test example here .

It is also possible to run an async request in a block beforeEach

. Here's an example:



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

      

+3


source







All Articles