2 回答

TA貢獻(xiàn)1805條經(jīng)驗 獲得超9個贊
為您提供正確的上下文:您首先問的是 Node.js 問題,而不是 CodeceptJS 或 Puppeteer。
desc總是true因為你將它聲明為一個字符串,所以不管里面的代碼是什么if都會運行,正如你已經(jīng)發(fā)現(xiàn)的那樣。
您可以使用執(zhí)行以下操作:
const numOfElements = await I.grabNumberOfVisibleElements('#show_card_description section button'); // Use CSS locator instead of Xpath for easier readability
console.log(numOfElements); // Debug
if(numOfElements === 1) {
…
}
另見https://codecept.io/helpers/Puppeteer#grabnumberofvisibleelements

TA貢獻(xiàn)1735條經(jīng)驗 獲得超5個贊
維護(hù)人員不支持在場景函數(shù)中使用常規(guī)功能文件的 if 語句,將其作為由于意外結(jié)果進(jìn)行測試的不良做法,而是您必須在自定義幫助文件中執(zhí)行它們,如下所示:
/**
* Checks the specified locator for existance, if it exists, return true
* If it is not found log a warning and return false.
*/
async checkElement(locator)
{
let driver = this.helpers["Appium"].browser;
let elementResult = await driver.$$(locator);
if (elementResult === undefined || elementResult.length == 0)
{
//console.log("Locator: " + locator + " Not Found");
return false;
}
else if (elementResult[0].elementId)
{
//console.log("Locator: " + locator + " Found");
return true;
}
}
參考 - https://codecept.io/helpers/
添加回答
舉報