2 回答
![?](http://img1.sycdn.imooc.com/54586431000103bb02200220-100-100.jpg)
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
![?](http://img1.sycdn.imooc.com/533e4c1500010baf02200220-100-100.jpg)
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/
添加回答
举报