使两个部分都期望解决承诺 [英] Making both parts of expect resolve promises

查看:159
本文介绍了使两个部分都期望解决承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

在量角器中, expect()已修补到隐含地理解启用速记断言风格的承诺。例如:

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()这里没有需要使用 then()显式解决,并且在检查期望之前由Protractor隐式解决。

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

问题:

是否有可能修补Jasmine / Protractor以使其理解断言双方的承诺?为了能够写:

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

});

如果这不适合你 - 我想你可以使用protractor.promise.all,只是一个例子:

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')

或更难的方式 - 创建自己的匹配器。看看我如何在我的lib中使用matcher里面的promises:
https://github.com/Xotabu4/jasmine-protractor-matchers/blob/master/index.js#L39

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

这篇关于使两个部分都期望解决承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆