对js / node.js不太熟悉。使用codeceptjs / puppeteer进行一些自动化测试。现在尝试在测试中编辑描述。但有时不存在描述-因此“编辑描述”按钮不存在-而是“添加描述”按钮。所以我写了一个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!')
});
答案 0 :(得分:1)
为您提供正确的上下文:您首先要问的是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
答案 1 :(得分:0)
维护人员不支持在场景函数中使用常规功能文件的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;
}
}