不太熟悉js/node.js。使用codeceptjs/傀儡机进行一些自动化测试。现在试着在测试中编辑一个描述。但有时没有描述--因为“编辑描述”按钮不存在,而是有一个“添加描述”按钮。所以我写了一个if语句来指定。但代码就这么翻过来了。不管语句是什么,它只是移到下一行。目前,if (desc)和if(!desc)都执行相同的函数--它转到if语句的下一行。这会导致错误,因为已经有了描述,因此“添加描述”按钮不可用。我不知道怎么回事。
Scenario('test something', (I, userLoginPage, FBPagePage) => {
userLoginPage.validate();
I.click('//*[@id="card_611"]');
// clicks the card
var desc = '//*[@id="show_card_description"]/section/button';
// add description button
// tried 'Add description' but the result was the same
if (desc){
// this is where the error happens. it simply looks for the add description button
// no matter what button is there it decides to click the 'add description' button
I.click('//*[@id="show_card_description"]/section/button');
// click add desc button
I.fillField('//*[@id="description_editor_container"]/div[2]/div[1]',
'not admin user created this description thanks to automated testing');
// types this stuff
I.click('//*[@id="description_editor_container"]/button[1]');
// saves it
I.wait(1);
}
I.click('//*[@id="show_card_description"]/section/h5/a');
// click edit desc button if a description already exists
I.fillField('//*[@id="description_editor_container"]/div[2]/div[1]', 'not admin user edited this description thanks to automated testing');
I.click('//*[@id="description_editor_container"]/button[1]');
I.say('success!')
});发布于 2019-10-21 12:22:54
为了给你正确的上下文:你是在问一个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
发布于 2019-12-03 19:00:21
维护人员不支持在场景函数中使用常规特性文件的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://stackoverflow.com/questions/58459223
复制相似问题