【问题标题】:Leadfoot's pollUntil() Not Triggering on Variable Value Change?Leadfoot 的 pollUntil() 未触发变量值更改?
【发布时间】:2015-06-29 14:14:02
【问题描述】:

我目前正在实习生编写功能测试,遇到了一个小问题。

在我的测试套件的前面部分,我对 API 进行了 ajax 调用以检索变量的值。设置的这个变量对于功能测试的下一步至关重要,因此我想暂停测试,直到从 ajax 调用返回该变量。

我读到了 Leadfoot 的 pollUntil() 函数,它听起来像是我需要做的。我写了以下代码:

var valueThatChanges = 0;

// ... (some functional test setup stuff)

//Ajax call that sets value of valueThatChanges
.then(function() {
    return ajaxCall(valueThatChanges);
})

//valueThatChanges is initially 0 before/during ajax call
//Is set to a randomly generated value that is non-zero after response recieved
.then(pollUntil(function(valueThatChanges) {
        return valueThatChanges !== 0 ? true : null;
    },[valueThatChanges], 30000, 100))

    .then(function() { //On success
        console.log('Value is no longer zero.')
    }, function(error) { //On failure/timeout
        console.log(error)
    })
});

但是这不起作用,因为尽管valueThatChanges 的值仍然为0,但函数会立即进入成功回调。

我了解 pollUntil() 可能不是为处理此类情况而设计的(因为我没有直接处理 pollUntil 中的 DOM 元素),但我不确定为什么它不适用于这种特定情况。

似乎 pollUntil() 没有在每次调用它的轮询函数时传递更新的变量。

pollUntil() 能否处理在变量值更改时触发事件?

【问题讨论】:

    标签: javascript intern leadfoot


    【解决方案1】:

    pollUntil 的一般用例是您需要等待远程浏览器中发生的事情。例如,pollUntil 常用于等待功能测试页面完全初始化:

    // ---------------------
    // functional test (in Node.js)
    this.remote.get('testpage.html')
        .then(pollUntil('return window.pageIsReady ? true : null'))
        // rest of test
    
    // ---------------------
    // remote test page (in the browser)
    <script>
        var pageIsReady = false;
        require( ..., function ( ... ) {
            // do setup stuff
            pageIsReady = true;
        });
    </script>
    

    如果您在测试设置中执行一些异步操作,而 涉及浏览器,请从测试套件中的 before 函数返回一个 Promise,当异步操作已完成。

    var valueThatChanges;
    
    registerSuite({
        before: function () {
            return new Promise(function (resolve) {
                // Assuming ajaxCall calls a callback when it's finished:
                ajaxCall(function (newValue) {
                    valueThatChanges = newValue;
                    resolve();
                });
            });
        },
    
        test1: function () {
            return this.remote
                // rest of test
        },
    
        // ...
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 2016-02-06
      • 2019-05-14
      • 2011-09-17
      • 1970-01-01
      • 2015-10-13
      • 2021-01-23
      相关资源
      最近更新 更多