Nightwatch:比`.pause(1000)`更好的方法来避免进行脆弱测试? [英] Nightwatch: Better way than `.pause(1000)` to avoid brittle tests?

查看:245
本文介绍了Nightwatch:比`.pause(1000)`更好的方法来避免进行脆弱测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.pause(1000)确实是等待表单提交的最佳实践吗?我正在寻找一种可靠地提交表单的方法,而不必知道有关表单提交后出现的页面的详细信息.

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)等待表单提交,具有讽刺意味的是,它不起作用更长,但是此版本具有修改的CSS选择器版本:

The example from the home page uses .pause(1000) to wait for form submissions, and ironically doesn't work any longer, but this version with a modified css-selector version does:

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')
  }
};

.pause(1000)确保表单提交的问题是如何确定超时.要么是 如果超时时间太长,将会使测试变慢;如果超时时间太短,则会使测试变脆.缓慢的硬件,服务器上的其他进程,月球对齐,您可以说它会影响应设置的良好"超时值.

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.

有没有更好的说法:请等待之前提交表格 继续"?

Is there a better way to say: "Wait for the form to be submitted before continuing"?

相反,我们已经尝试了.waitForElementVisible('body', VERY_LONG_TIMEOUT),它似乎可以正常工作,并且花费的时间不会比必要的长,但是我想这也不可靠.之所以只能这样做是因为(当前)当前"页面已消失(因此),因此我们正在等待新"页面的正文出现.明天会出现一些奇怪的情况,它将比正常情况下更快,并且.waitForElementVisible('body')会立即返回,因为旧页面仍然存在. ==也很脆.正确吗?

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?

如果是这样,是否有比.pause(1000)少的脆性方式? .waitForElementVisible('body')?特别是如果我们对 提交后返回的页面,所以我们不能 .waitForElementVisible('.element-only-on-new-page')?

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')?

我问的原因是我们的测试实际上更像是:

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)
  }
};

之所以失败,是因为不再提交" http://some/url "中的表单 铬45 :-(我们想找到一个好的解决方案,而不仅仅是在当今条件下可以工作的解决方案...

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...

推荐答案

您是否尝试将waitForElementNotVisiblewaitForElementVisible链接为正文html?这仅应在每个步骤上等待适当的时间.我会做一些测试,以确保它不会变脆.我们正在使用它来监视单页应用程序中的模拟页面转换".

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.

例如

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)
  }
};

这篇关于Nightwatch:比`.pause(1000)`更好的方法来避免进行脆弱测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆