gpt4 book ai didi

javascript - 如果 NightwatchJS 中不存在元素,如何停止测试而不出错

转载 作者:行者123 更新时间:2023-11-28 18:01:03 25 4
gpt4 key购买 nike

在我的 Nightwatch 测试中,我在每个测试之前都会运行一个应用程序并从 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
});

关于javascript - 如果 NightwatchJS 中不存在元素,如何停止测试而不出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43542313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com