Nightwatch waitForElementVisible-将abortOnFailure参数设置为false-测试退出状态为非零

时间:2019-02-20 19:17:52

标签: nightwatch.js

我正在使用Nightwatch API docs中的waitForElementVisible(<selector>, <timeout>, false)命令,它的运行情况与我预期的不太一样。如何调整此代码以获得预期的行为?

预期行为:

  
      
  • 致电.waitForElementVisible('foobar', 10, false)
  •   
  • 查看命令失败,并继续执行下一条命令
  •   
  • 所有其他命令均通过
  •   
  • 查看脚本退出状态为0
  •   

实际行为:

  
      
  • 致电.waitForElementVisible('foobar', 10, false)
  •   
  • 查看命令失败,并继续执行下一条命令
  •   
  • 所有其他命令均通过
  •   
  • 查看脚本退出状态为1
  •   

这里是要复制的示例代码

module.exports = {
  tags: ['smoke'],

  before: browser =>
    browser
      .maximizeWindow('current').url('https://google.com'),

  after: browser => browser.end(),

  'smoke test': browser =>
    browser
      .waitForElementVisible('foobar', 10, false)
      .waitForElementVisible('img')
      .assert.visible('img'),
};

这是运行该命令的控制台输出:

Starting selenium server in parallel mode... started - PID:  75459

Started child process for: 01_smoke 
 01_smoke   \n
 01_smoke   [01 Smoke] Test Suite
=========================
 01_smoke   
 01_smoke   Results for:  smoke test
 01_smoke   ✖ Timed out while waiting for element <foobar> to be present for 10 milliseconds.  - expected "visible" but got: "not found"
 01_smoke       at Object.smokeTest [as smoke test] (/path/to/tests/01_smoke.js:12:8)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
 01_smoke   ✔ Element <img> was visible after 33 milliseconds.
 01_smoke   ✔ Testing if element <img> is visible.
 01_smoke   
 01_smoke   Retrying (1/3):  smoke test
 01_smoke   ✖ Timed out while waiting for element <foobar> to be present for 10 milliseconds.  - expected "visible" but got: "not found"
 01_smoke       at Object.smokeTest [as smoke test] (/path/to/tests/01_smoke.js:12:8)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
 01_smoke   ✔ Element <img> was visible after 21 milliseconds.
 01_smoke   ✔ Testing if element <img> is visible.
 01_smoke   
 01_smoke   Retrying (2/3):  smoke test
 01_smoke   ✖ Timed out while waiting for element <foobar> to be present for 10 milliseconds.  - expected "visible" but got: "not found"
 01_smoke       at Object.smokeTest [as smoke test] (/path/to/tests/01_smoke.js:12:8)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
 01_smoke   ✔ Element <img> was visible after 20 milliseconds.
 01_smoke   ✔ Testing if element <img> is visible.
 01_smoke   Retrying (3/3):  smoke test
 01_smoke   ✖ Timed out while waiting for element <foobar> to be present for 10 milliseconds.  - expected "visible" but got: "not found"
 01_smoke       at Object.smokeTest [as smoke test] (/path/to/tests/01_smoke.js:12:8)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
 01_smoke   ✔ Element <img> was visible after 20 milliseconds.
 01_smoke   ✔ Testing if element <img> is visible.
 01_smoke   FAILED:  1 assertions failed and 2 passed (53ms)

  >> 01_smoke finished.  


 _________________________________________________

 TEST FAILURE:  1 assertions failed, 2 passed. (6.259s)

 ✖ 01_smoke

   - smoke test (53ms)
   Timed out while waiting for element <foobar> to be present for 10 milliseconds.  - expected "visible" but got: "not found"
       at Object.smokeTest [as smoke test] (/path/to/tests/01_smoke.js:12:8)
       at _combinedTickCallback (internal/process/next_tick.js:131:7)

1 个答案:

答案 0 :(得分:0)

是的,这就是应该的方式!我认为您误解了waitForVisible命令的abortOnFailure标志的工作方式。 false标志仅使方法字符由测试运行程序评估为不间断 步骤。但这并不意味着它不会将该步骤视为失败步骤。

注意:在assert/verify情况下也会发生类似的情况(其中verify是一个不间断的断言,类似于abortOnFailure: false参数waitForElementVisible )。


尽管如此,我仍然可以看到人们的印象。如果您阅读了API调用的说明,则会显示:

  

如果元素不存在且以指定的数量可见   时间长了,测试失败了。您可以通过设置abortOnFailure来更改此设置   为假。

这使您认为即使waitForVisible命令失败,测试也可能最终通过。 但是... API调用的 Parameters 部分有助于我们,消除了错误的假设:

  

默认情况下,如果未找到该元素,则测试将失败。 设置   如果您希望测试继续进行,则此为false,即使   断言失败。 要全局设置此属性,可以定义一个属性   全局变量中的abortOnAssertionFailure。


最后... ,在DOC可能使您无法通过的地方,代码将永远不会说谎:

  process.on('exit', function (code) {
    var exitCode = code;

    if (exitCode === 0 && globalResults && (globalResults.errors > 0 || globalResults.failed > 0)) {
      exitCode = 1;
    }

    process.exit(exitCode);
  });

上面是片段Nightwatch的测试运行器(nightwatch/lib/runner/run.js)。亲自查看它认为有效的exit code 1条件。

干杯!