javascript Nightwatch:比`.pause(1000)` 更好的方法来避免脆弱测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33224546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Nightwatch: Better way than `.pause(1000)` to avoid brittle tests?
提问by Peter V. M?rch
Is .pause(1000)
really the best-practice there is to wait for form submission? I'm looking for a way to reliably submit a form without having to know details about the page appearing as a result of the form submission.
是否.pause(1000)
真的是最好的做法有等待表单提交?我正在寻找一种可靠地提交表单的方法,而不必知道由于表单提交而出现的页面的详细信息。
The example from the home pageuses .pause(1000)
to wait for form submissions, and ironically doesn't work any longer, but this version with a modified css-selector version does:
从这个例子主页用途.pause(1000)
等待表单提交,讽刺的是没有任何工作更长的时间,但这个版本与修改CSS选择器版本的功能:
module.exports = {
'Demo test Google' : function (client) {
client
.url('http://www.google.com')
.waitForElementVisible('body', 1000)
.assert.title('Google')
.assert.visible('input[type=text]')
.setValue('input[type=text]', 'rembrandt van rijn')
.waitForElementVisible('button[name=btnG]', 1000)
.click('button[name=btnG]')
.pause(1000)
// This selector is different from the home page's - this one
// works...
.assert.containsText('ol#rso div.g:first-of-type',
'Rembrandt - Wikipedia')
}
};
The problem with .pause(1000)
to ensure the form gets submitted is how to determine the timeout. It is either
going to make our tests slow if the timeout is too long or make them brittle if the timeout is too short. Slow hardware, other processes on the server, moon alignment, you name it can influence what "good" timeout values should be.
这个问题.pause(1000)
,以确保被提交的表格是如何确定的超时时间。如果超时太长,它要么会使我们的测试变慢,要么如果超时太短会使它们变得脆弱。缓慢的硬件,服务器上的其他进程,月亮对齐,你说它会影响“好的”超时值应该是什么。
Is there a better way to say: "Wait for the form to be submitted before continuing"?
有没有更好的说法:“等待表单提交后再继续”?
We've experimented with .waitForElementVisible('body', VERY_LONG_TIMEOUT)
instead, and it seems to work and not take longer than necessary, but I'm guessing this also isn't reliable. That it only works because the "current" page has disappeared (this time) and so we're waiting for the "new" page's body to appear. And that tomorrow some oddity will occur and it'll be faster than normal and .waitForElementVisible('body')
will return immediately because the old page is still there. == also brittle. Is that correct?
我们已经尝试过.waitForElementVisible('body', VERY_LONG_TIMEOUT)
,它似乎可以工作,而且花费的时间不会超过必要的时间,但我猜这也不可靠。这只是因为“当前”页面已经消失(这次),所以我们正在等待“新”页面的正文出现。明天会发生一些奇怪的事情,它会比正常情况更快,并且.waitForElementVisible('body')
会立即返回,因为旧页面仍然存在。==还脆。那是对的吗?
If so, is there some less brittle way than .pause(1000)
or
.waitForElementVisible('body')
? Especially if we don't know much about the
page returned after the submission, so we can't
.waitForElementVisible('.element-only-on-new-page')
?
如果是这样,有没有比.pause(1000)
or
更脆弱的方法.waitForElementVisible('body')
?特别是如果我们对提交后返回的页面不太了解,所以我们不能
.waitForElementVisible('.element-only-on-new-page')
?
The reason I'm asking is that our tests actually looked more like:
我问的原因是我们的测试实际上看起来更像是:
module.exports = {
'Test1 - submit form' : function (client) {
client
.url('http://some/url')
.waitForElementVisible('body', 1000)
.assert.title('MyTitle')
.setValue('input[name="widget"]', 'value')
// Click to submit the form to change some internal state
.click('button[name="postForm"]')
// Form got submitted fine in chromium 42 every single time. chromium
// 45 needs additionally:
//
// .pause(1000)
// or
// .waitForElementVisible('body', 1000)
}
'Test2 - continue using new value' : function (client) {
client
.url('http://some/other/url')
.waitForElementVisible('body', 1000)
.assert.title('MyOtherTitle')
.setValue('input[name="widget2"]', 'value2')
.waitForElementVisible('.bla-bla', 1000)
}
};
This broke because the form at 'http://some/url' no longer gets submitted in chromium 45 :-( We'd like find a good solution, not just one that seems to work under today's conditions...
这是因为' http://some/url'的表单不再以铬45提交:-(我们想找到一个好的解决方案,而不仅仅是一个在今天的条件下似乎有效的解决方案......
回答by Stuart Brock
Have you tried chaining waitForElementNotVisible
with waitForElementVisible
for the body html? This should only wait for the appropriate time at each step. I'd do some testing to make sure it isn't brittle though. We're using this to monitor a "simulated page transition" in a Single Page Application.
您是否尝试链接waitForElementNotVisible
与waitForElementVisible
对身体的HTML?这应该只在每一步等待适当的时间。我会做一些测试以确保它不脆。我们使用它来监视单页应用程序中的“模拟页面转换”。
e.g.
例如
module.exports = {
'Test1 - submit form' : function (client) {
client
.url('http://some/url')
.waitForElementVisible('body', 1000)
.assert.title('MyTitle')
.setValue('input[name="widget"]', 'value')
// Click to submit the form to change some internal state
.click('button[name="postForm"]')
.waitForElementNotVisible('body', 5000)
.waitForElementVisible('body', 10000)
}
};