Gabriel Kohen
Gabriel Kohen

Reputation: 4286

Can Protractor Jasmine adaptation properly wait for protractor.promise.defer().promise?

First of all, hats off to the Protractor team and community for coming up with such a library for a tough problem to implement such as E2E testing.

I have a wrapper JS Class around an ElementFinder since I wanted to add extra utility methods to inspect the ElementFinder further. When I return an instance of such class objects I return it with:

function myFunc(){
   var myElement = element(by.binding('plan.name'));
   var deferred = protractor.promise.defer();
   var myWrapper = new myElementWrapper(myElement);
   deferred.fulfill(myWrapper);
   return deferred.promise;
}

Later on I expect the value in Jasmine 2.1 using:

var val=myFunc();
expect(val).not.toBeNull();

According to the official documentation by Protractor queen,@juliemr , the expect is supposed to wait until the promise is resolved. It seems to be breezing by without stopping. Looking at the instance of promise my code has generated I see that it's of type:goog.scope.promise.Promise. In the Protractor code I've noticed it's using: webdriver.promise.isPromise(res). I've also tried wrapping the call with flow.execute without success and would like to avoid using series of chained .then calls since it makes the test less readable.

Will that properly wait to resolve my promise above before moving on with the test flow?

If not what is the proper way to create promise object to be properly inspected by Protractor's flavor of expect?

I'm running using the new and shiny Protractor 2.0 release.

Upvotes: 3

Views: 379

Answers (1)

P.T.
P.T.

Reputation: 25177

You create a deferred then immediately fulfill the deferred and return the promise for it (which is just a complicated way of returning the myWrapper object directly).

Promises are used to represent a value that you don't have yet. I don't see anything in your example that isn't immediately available, so its not clear why you need a promise.

I think you want something like this:

function myFunc() {
   var myElement = element(by.binding('plan.name'));
   return new myElementWrapper(myElement);
}

Your myElementWrapper should look like a promise if you want to pass it to expect, though (if you extend the existing ElementFinder that should be sufficient).

Upvotes: 1

Related Questions