测试总是超时,不显示承诺输出

时间:2016-12-12 14:25:30

标签: node.js promise protractor

        browser.sleep(4000);
        var discount_type = element(by.id("discount_type"));
        var discount_value= element(by.id("discount_value"));
        var grandTotal    = element(by.id("grand-total"));
        var subtotal      = element(by.id("subtotal"));
        var delivery_fee  = element(by.id("delivery_fee"));

        var last = protractor.promise.defer();

        console.log("hallo");
        subtotal.getText().then(function(sub) {
          console.log("#############");
          console.log(sub);
          delivery_fee.getText().then(function(fee) {
            console.log(fee);
            var calc = parseFloat(sub) - parseFloat(fee) - parseFloat(config.promocodes.referral.discount);
            console.log(calc);
            last.fulfill(calc);
          }); 
        }); 
        last.then(function(calc) { 
            console.log("final wait");
            expect(grandTotal.getText()).toBe("$" + calc); 
        }); 

在确定我的测试运行正常之前,我需要计算一个值。

在我的测试中,我总是在控制台中看到“hallo”。 然后我得到了一个

> hallo
 A Jasmine spec timed out. Resetting the WebDriver Control Flow.
> F
> 
> Failures: 1) Refer a user and apply referral code should assign
> referral discount to user   Message:
>     Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.   Stack:
>     Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
>         at Timer.listOnTimeout (timers.js:92:15)

似乎代码永远不会进入subtotal.getText()承诺!为什么??? ???

1 个答案:

答案 0 :(得分:1)

永远不会从承诺解析功能返回。并且,您不必延迟:

var last = subtotal.getText().then(function(sub) {
  return delivery_fee.getText().then(function(fee) {
    return parseFloat(sub) - parseFloat(fee) - parseFloat(config.promocodes.referral.discount);
  }); 
}); 
last.then(function(calc) { 
    expect(grandTotal.getText()).toBe("$" + calc); 
});