Making both parts of expect resolve promises

梦想的初衷 提交于 2019-12-13 16:17:34

问题


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());

回答1:


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




回答2:


Not pretty, but you could resolve the param. It's a no-op for non promises...

expect(elm1.getText()).toEqual(Promise.resolve(elm2.getText()));


来源:https://stackoverflow.com/questions/38512113/making-both-parts-of-expect-resolve-promises

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!