The Problem:
In Protractor, expect()
is patched to implicitly understand promises which enables a shorthand assertion style. E.g.:
expect(elm.getText()).toEqual("expected text");
elm.getText()
here does not need to be explicitly resolved with then()
and would be implicitly resolved by Protractor before the expectation is checked.
But, what if the "to equal" part is also a promise. For instance, a text from an another element. In this case we have to resolve the second part explicitly:
elm2.getText().then(function (text2) {
expect(elm1.getText()).toEqual(text2);
});
The Question:
Is it possible to patch Jasmine/Protractor to make it understand promises on both sides of the assertion? To be able to write:
expect(elm1.getText()).toEqual(elm2.getText());
Just tested with promises for both sides - and it resolves them OK.
Try at your project. Maybe you have nothing to do:
describe('ololo', function () {
it('both sides are promises', function () {
browser.get('http://www.protractortest.org/testapp/ng1/#/form');
let elementText1 = $('.ng-scope p').getText();
let elementText2 = $('#transformedtext>h4').getText();
//Will fail here, but you can see that it successfully resolved promises
expect(elementText1).toEqual(elementText2);
});
});
If this does not work for you - i think you can use protractor.promise.all, just example:
protractor.promise.all([elm2.getText(), elm1.getText()])
.then(texts=> expect(texts[0]).toEqual(texts[1]), 'texts should be same')
Or harder way - create own matchers. See how i work with promises inside matcher in my lib:
https://github.com/Xotabu4/jasmine-protractor-matchers/blob/master/index.js#L39
Not pretty, but you could resolve the param. It's a no-op for non promises...
expect(elm1.getText()).toEqual(Promise.resolve(elm2.getText()));