如果 NightwatchJS 中不存在元素如何停止测试而不会出错
If element is not present in NightwatchJS how to stop test without error
在我的 Nightwatch 测试中,我有一个 before each 运行和从 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)
只是想知道如何检查某些内容而不是错误。或者如果元素存在则继续断言?
谢谢水桶
我检查了 code of the elementPresent
assertion:
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';
};
它使用一个 low-level api 来测试元素是否存在:
.elements()
Search for multiple elements on the page, starting from the document root. The located elements will be returned as WebElement JSON objects.
First argument to be passed is the locator strategy, which is detailed on the WebDriver docs.
我想解决方案可能是这样的:
browser.elements('css selector', '#root div.touchable a', result => {
const isPresent = result.status === 0 && result.value.length > 0;
// doStuff
});
在我的 Nightwatch 测试中,我有一个 before each 运行和从 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)
只是想知道如何检查某些内容而不是错误。或者如果元素存在则继续断言?
谢谢水桶
我检查了 code of the elementPresent
assertion:
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';
};
它使用一个 low-level api 来测试元素是否存在:
.elements()
Search for multiple elements on the page, starting from the document root. The located elements will be returned as WebElement JSON objects. First argument to be passed is the locator strategy, which is detailed on the WebDriver docs.
我想解决方案可能是这样的:
browser.elements('css selector', '#root div.touchable a', result => {
const isPresent = result.status === 0 && result.value.length > 0;
// doStuff
});