Webdriver.IO-以非阻塞方式检查元素是否存在 [英] Webdriver.IO - check in a non-blocking way whether an element exists
问题描述
菜鸟问题.我正在使用 webdriver.io 编写
Rookie question. I am using webdriver.io to write Appium end to end tests.
我正在使用以下命令来检查元素的存在:
I am using these commands to check the presence of an element:
-
$(selector)
-
browser.waitForVisible(selector, timeout, waitForAppearOrDisappear);
-
browser.element('selector')
$(selector)
browser.waitForVisible(selector, timeout, waitForAppearOrDisappear);
browser.element('selector')
如果找不到该元素,它们都会阻止我的测试.
They all block my tests if the element is not found.
如何在不阻止测试执行的情况下检查元素在特定时刻是否存在?
How can I check whether an element exists in a particular moment without blocking the test execution?
(简洁的)文档中找不到任何内容.
I cannot find anything in the (concise) documentation.
PS. browser.findElement(By.css('[data-qa="some-id"]'));
应该可以解决问题,但这不是 wdio 命令. (无法识别findElement
和By
)
PS. This should do the trick browser.findElement(By.css('[data-qa="some-id"]'));
but it's not a wdio command. (findElement
and By
are not recognized)
npm依赖项:
"appium": "^1.10.0",
"appium-doctor": "^1.6.0",
"wdio-appium-service": "^0.2.3",
"wdio-jasmine-framework": "^0.3.8",
"wdio-spec-reporter": "^0.1.5",
"webdriverio": "^4.14.1",
推荐答案
好吧,简单的方法是滥用查询Selenium服务器的事实(使用 元素 )是不间断的操作.
Well, the simples way would be to abuse the fact that querying the Selenium server (using $ / element) is a non-breaking operation.
假设您必须查询2个元素,一个元素已在DOM中呈现,另一个元素不存在(或该元素根本不存在).
Suppose you have to query 2 elements, one has rendered inside the DOM, the other didn't (or the element doesn't exist altogether).
let thisLoaded = $('span.coreSpriteFacebookIconInverted');
let thisDidnt = $('span.coreSpriteInstagramIconInverted');
这两个变量将具有以下内容:
The two variables would have the following contents:
thisDidnt = {
sessionId: '7056961e1950b5c54827a51c137cca96',
value: { ELEMENT: '0.8611270215748772-1',
'element-6066-11e4-a52e-4f735466cecf': '0.8611270215748772-1' },
selector: 'span.coreSpriteFacebookIconInverted',
_status: 0
}
thisDidnt = {
type: 'NoSuchElement',
message: 'An element could not be located on the page using the given search parameters.',
state: 'failure',
sessionId: '7056961e1950b5c54827a51c137cca96',
value: null,
selector: 'span.coreSpriteInstagramIconInverted'
}
因此,您现在可以继续进行检查了...
Thus, you can now proceed to do your checks ...
if (thisLoaded.value) {
// > if element loaded <
} else {
// > if element didn't load <
}
我回到家时会添加更多...
I'll add more when I get home ...
这篇关于Webdriver.IO-以非阻塞方式检查元素是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!