在 AssertionError 上断言调用 browser.end() 的 nightwatchjs

nightwatchjs with assert calling browser.end() on AssertionError

我正在使用 nightwatch 检查页面上的 table 单元格是否包含大于 0 的数字。

为此,我必须使用节点断言包:

const assert = require('assert');

所以首先,我得到 table 单元格元素文本,在回调中我将它解析为一个 int 并使用断言来检查它的值:

...
    .getText("//table[@id='topology-summary-table']/tbody/tr/td[7]", function(el){
        assert(parseInt(el.value) > 0, "Num Executors == 0!");
    }).end()
...

唯一的问题是,如果 assert 抛出 AssertionError(即:如果 table 单元格中的数字为 0),则测试停止,并且 .end() 永远不会被调用,从而使浏览器进程保持打开状态并徘徊。不理想。

我通过执行以下操作解决了这个问题:

...
    .getText("//table[@id='topology-summary-table']/tbody/tr/td[7]", function(el){
        try {
            assert(parseInt(el.value) > 0, "Num Executors == 0!");
        } catch(e){
            this.end();
            throw e;
        }
    })
...

但出于某些原因,这让我感觉很糟糕。 :(

我的问题是:有没有更好的方法?

nightwatch api 实际上扩展了断言 api,所以没有必要 require 它。

相反,这样做:

.getText("//table[@id='topology-summary-table']/tbody/tr/td[7]", function(el){
  this.assert.ok(parseInt(el.value) > 0, "Num Workers == 0!");
})

beatfactor 在这里提供的答案:https://github.com/nightwatchjs/nightwatch/issues/1002#issuecomment-223240103