【问题标题】:Test async page changes with the intern与实习生一起测试异步页面更改
【发布时间】:2017-12-16 17:52:14
【问题描述】:

假设我在页面上有一个按钮和一个 div 元素。

<button /><div>old text</div>

通过单击按钮,javascript 异步更改 div 的文本(即在 ajax 调用之后)。我如何与实习生一起测试它?

这不起作用:

function testButtonClick() {
      return command
                 .findByTagName('button')
                 .click()
                 .end()
                 .findByTagName('div')
                 .getVisibleText()
                 .then(function(text) { assert.strictEqual(text,'new text')}); //errror, text is 'old text'
}

如果我在.end() 之后添加.sleep(5000),那么它可以正常工作(因为我的异步请求通常在 5 秒内完成)。但我不想等这么久,因为异步通常会更早完成。

但是使用时间值较低的睡眠,我冒着在请求完成之前对其进行测试的风险。

有没有更好的办法?

【问题讨论】:

    标签: javascript testing intern


    【解决方案1】:

    最有效的解决方案是使用pollUntil,例如:

    return command
        .findByTagName('button')
        .click()
        .end()
        .then(pollUntil(function () {
            var el = document.getElementByName('div');
            if (el && el.textContent === 'new text') {
                return true;
            }
        }, 5000));
    

    轮询直到将在浏览器上下文中重复运行一个函数,直到它返回一个非空、非未定义的答案,或者直到它超时。在上面的 sn-p 中,如果元素存在并且具有预期的文本,则轮询函数返回 true,否则返回 undefined。它会在 5000 毫秒后超时,但会在预期文本出现后立即结束。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-27
      • 1970-01-01
      • 1970-01-01
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多