CodeceptJS / Puppeteer 无法识别 'if' 语句
CodeceptJS / Puppeteer doesn't recognize 'if' statement
不太熟悉 js/node.js。使用 codeceptjs/puppeteer 进行一些自动化测试。现在正在尝试编辑测试中的描述。但有时没有描述 - 因此 'edit description' 按钮不存在 - 而是有一个 'add description' 按钮。所以我写了一个 if 语句来指定 .但代码只是滚动它。语句是什么并不重要,它只是移动到下一行。目前 if (desc)
和 if(!desc)
都执行相同的功能 - 它转到 if 语句的下一行。这会导致错误,因为已经有描述,因此 'add description' 按钮不可用。我不确定发生了什么。
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!')
});
为了给你正确的上下文:你问的是 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
维护者不支持在场景函数中使用常规特征文件的 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;
}
}
不太熟悉 js/node.js。使用 codeceptjs/puppeteer 进行一些自动化测试。现在正在尝试编辑测试中的描述。但有时没有描述 - 因此 'edit description' 按钮不存在 - 而是有一个 'add description' 按钮。所以我写了一个 if 语句来指定 .但代码只是滚动它。语句是什么并不重要,它只是移动到下一行。目前 if (desc)
和 if(!desc)
都执行相同的功能 - 它转到 if 语句的下一行。这会导致错误,因为已经有描述,因此 'add description' 按钮不可用。我不确定发生了什么。
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!')
});
为了给你正确的上下文:你问的是 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
维护者不支持在场景函数中使用常规特征文件的 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;
}
}