protractor icon indicating copy to clipboard operation
protractor copied to clipboard

Promise finally needed

Open psech opened this issue 12 years ago • 4 comments

I am looking for Promise.finally feature, as I need to call expects regardless of promise resolution.

I have a function that performs navigation step:

function navigateStep(linkText) {
        ptor.wait(waitForItemByLinkText(linkText), 10000);
        return element(by.linkText(linkText)).click();
    }

which returns:

{ then: [Function: then],
  cancel: [Function: cancel],
  isPending: [Function: isPending] }

This is a simplified example as I have got chain of navigateStep but in general I need to call expect regardless of promise resolution. For now I have got:

navigateStep('elementName').then(function success() {
        expect(ptor.getCurrentUrl()).toBe(urlComplete);
}, function failure() {
        expect(ptor.getCurrentUrl()).toBe(urlComplete);
});

When the chain is broken somewhere I also would like to fail the test.

Can you please provide me with an example how can I use WebDriver thenFinally if this possible?

Moreover I am using Q for grouping promises and notices that Q is installing with protractor 0.20.1. Can I use it somehow?

psech avatar Mar 25 '14 15:03 psech

You should be able to just use element(by.linkText()).click().thenFinally();

It doesn't show up in the output string because the promise object is inheriting those methods.

Re: Q - Protractor uses Q internally for some promises which should not be linked to the webdriver control flow - but in your tests, it's probably easier not to mix promise libraries unless you really know what you're doing.

juliemr avatar Mar 26 '14 21:03 juliemr

Thank you for your replay.

I have updated protractor to the newest version, cleaned up code to use only WD promise library and it works fine now.

Additionally, I could not find anything like grouping promises in WebDriver. Would it be possible to have grouping in Protractor?

I meen:

var Q = require('q'),
    promises = [];
for (var i = 0; i < 4; i++) {
    promises.push(useBackButton());
}
// Click back button 4 times
Q.all(promises).done(function () {
    callExpects();
});

I am not interested to use button in defined order but only four times. The order of resolving promises is not the case for me, so there is no need to chain them.

psech avatar Mar 31 '14 11:03 psech

Yes! Check out https://code.google.com/p/selenium/source/browse/javascript/webdriver/promise.js#751 which you can use as protractor.promise.all(promises).then(function(arr) { /* arr is your array of promises */ }

juliemr avatar Apr 01 '14 04:04 juliemr

Thank you, works perfect!

psech avatar Apr 02 '14 21:04 psech