問題描述
我正在嘗試編寫一個(gè)腳本,該腳本需要根據(jù) CSS 選擇器找到的特定瀏覽器對(duì)象是否存在來調(diào)整其工作流行為.
I'm trying to write a script that needs to adapt it's workflow behavior depending on whether a particular browser object found by CSS selector exists or does not exist.
我不想使用 document.getElementByID 方法,因?yàn)檫@在技術(shù)上不是 CSS 選擇器,而且我們的整個(gè)企業(yè)都在 CSS 選擇器上進(jìn)行了標(biāo)準(zhǔn)化,所以除了 CSS 選擇器之外的任何遍歷 DOM 的東西都不會(huì)通過我們的無論如何都要進(jìn)行代碼審查.
I do not want to use a document.getElementByID method as this is not technically a CSS selector, and our entire enterprise is standardized on CSS selector so anything that walks the DOM other then a CSS selector won't make it past our code review process anyway.
var thing = await things.thingSelector(thingName);
if (await t.expect(thing.exists).notOk()) {
await t.click(things.OpenThing(thingName));
} else {
return false;
}
return true;
thingSelector 在哪里:
Where thingSelector is:
const thingSelector = name =>
Selector('p.thing-header-title span')
.withText(name)
.parent('div.thing');
OpenThing 在哪里:
Where OpenThing is:
const OpenThing = name =>
thingSelector(name)
.find('a.thing-footer-item')
.withText('Open');
如果對(duì)象不存在并且我正在檢查它是否存在,或者如果對(duì)象存在并且我正在檢查它不存在,以及對(duì)象存在的情況,我需要能夠繼續(xù)執(zhí)行不存在,不存在,對(duì)象不存在,我正在檢查它是否不存在.
I need to be able to continue execution if the object is not there and I'm checking that it exists, or if the object is there and I'm checking that it does not exist, and also the cases where the object is not there and it does not exist and the object is not there and I'm checking that it does not exist.
在所有情況下,我仍然需要繼續(xù)工作流程.
In all cases I still need to proceed with the workflow.
邏輯硬幣的兩面我都試過了:
I've tried both sides of the logic coin:
if (!await t.expect(thing.exists).ok())
和
if (await t.expect(thing.exists).notOk())
如果上述其中一項(xiàng)在一種情??況下未失敗,則在另一種情況下將失敗,而在第一個(gè)未失敗的情況下,另一項(xiàng)將失敗.我需要一些能給我邏輯的東西,但永遠(yuǎn)不會(huì)使腳本執(zhí)行失敗,并且仍然允許我根據(jù)對(duì)象是否存在返回 True 或 False.
If one of the above doesn't fail in one scenario it will fail in the other, and the other one will fail in the scenario that the first one didn't fail. I need something that will give me the logic, but not ever fail the script execution and still allow me to return either True or False depending on if the object is present or not present.
提前感謝您幫助我解決了這個(gè)問題,也感謝您學(xué)習(xí)和提高我的 Javascript 技能!
Thank you in advance for helping me to solve this problem, and also to learn and grow in my Javascript skills!
推薦答案
您可以通過以下方式檢查 if
條件中的 async exists
屬性:
You can check the async exists
property in the if
condition in the following way:
if(await things.thingSelector(thingName).exists) {
// do something
}
這篇關(guān)于TestCafe - 如何在不通過測試的情況下檢查 Web 元素是否存在?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!