如果NightWatchJS中不存在元素,则如何在没有错误的情况下停止测试



在我的夜间测试中,我都有一个运行并从Facebook中删除应用程序的一个。如果不存在该应用程序,我只想停止测试之前。

.waitForElementPresent('#root div.touchable a', 2000, false, function (result) {
  if (result.value === false) {
    browser.saveScreenshot('./test/e2e/img/element-not-there.png')
    browser.end()
  }
})
.doSomethingIfElementIsThere() // not real ;)

即使设置false参数,这似乎也无法使用。我得到这个错误:

ERROR: Unable to locate element: "#root div.touchable a" using: css selector
    at Object.before (/Users/user/projects/project/test/e2e/specs/testy-mc-test-face.js:30:8)
 ✖ Timed out while waiting for element <#root div.touchable a> to be present for 2500 milliseconds.  - expected "found" but got: "not found"
    at Object.before (/Users/user/projects/project/test/e2e/specs/testy-mc-test-face.js:30:8)

只是想知道如何检查某些东西而不是错误。或继续主张是否存在?

Merci Buckets

我检查了elementPresent断言的代码:

this.command = function(callback) {
  return this.api.elements(this.client.locateStrategy, selector, callback);
};
this.value = function(result) {
  return (result.status !== 0 || result.value.length === 0) ? 'not present' : 'present';
};

它使用低级API,您可以使用该API来测试是否存在元素:

.elements((

从文档根开始搜索页面上的多个元素。定位元素将作为Webelement JSON对象返回。 要传递的第一个论点是定位策略,该策略在WebDriver文档中详细介绍。

我想解决方案看起来像这样:

browser.elements('css selector', '#root div.touchable a', result => {
  const isPresent = result.status === 0 && result.value.length > 0;
  // doStuff
});

最新更新