使用实习生测试异步页面更改

时间:2017-07-12 10:38:05

标签: javascript testing intern

假设我在页面上有一个按钮和一个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'
}

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

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

有更好的方法吗?

1 个答案:

答案 0 :(得分: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));

轮询直到在浏览器上下文中重复运行一个函数,直到它返回一个非null,非未定义的答案,或者直到它超时。在上面的代码片段中,如果元素存在且具有预期文本,则轮询函数返回true,否则返回undefined。它将在5000毫秒后超时,但一旦预期的文本显示就会结束。

相关问题