2 回答

TA貢獻1805條經驗 獲得超9個贊
為您提供正確的上下文:您首先問的是 Node.js 問題,而不是 CodeceptJS 或 Puppeteer。
desc總是true因為你將它聲明為一個字符串,所以不管里面的代碼是什么if都會運行,正如你已經發現的那樣。
您可以使用執行以下操作:
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貢獻1735條經驗 獲得超5個贊
維護人員不支持在場景函數中使用常規功能文件的 if 語句,將其作為由于意外結果進行測試的不良做法,而是您必須在自定義幫助文件中執行它們,如下所示:
/**
* 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/
添加回答
舉報