Javascript量角器比较承诺的总和

时间:2018-09-20 18:27:43

标签: javascript promise protractor

我正试图从一个网页中获取3个值,将它们中的2个值相加(货币,例如$ 2,213.93),并比较它是否等于第三个值。

量角器函数locator.getText()返回一个Promise,我必须在Promise中进行所有转换

这就是我尝试的方法

从网页获取价值并进行转换:

    SettingsPage.prototype.getValueAndConvert = function (locator) {
        locator.getText().then(function (text) {
            numb = text.replace(/,/g, '');
            numb = numb.replace(/\$/g, '');
            numb= numb.replace(/\./g, '');
            numb = parseInt(numb);
            console.log(numb);
            return numb
         });
        };

确认值相等

            var workingBalancevalue = settings.getValueAndConvert(workingBalanceField);
            var totalInflowvalue = settings.getValueAndConvert(totalInflowField);
            var totalOutflowvalue = settings.getValueAndConvert(totalOutflowField);
            protractor.promise.all([totalInflowvalue, totalOutflowvalue, workingBalancevalue]).then(function (values) {
                expect((values[0]) + (values[1])).toEqual(values[2]);
            });

它可以工作,但是NaN中console.log(numb)的值,所以不正确,因为它将NaN匹配为NaN

实际如何做?

1 个答案:

答案 0 :(得分:1)

答案是增加诺言的回报

 SettingsPage.prototype.getValueAndConvert = function (locator) {
        return locator.getText().then(function (text) {       // added return
            numb = text.replace(/,/g, '');
            numb = numb.replace(/\$/g, '');
            numb= numb.replace(/\./g, '');
            numb = parseInt(numb);
            console.log(numb);
            return numb
         });
        };