猿问

CodeceptJS / Puppeteer 无法识别“if”语句

不太熟悉 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!')

});


慕无忌1623718
浏览 224回答 2
2回答

Cats萌萌

为您提供正确的上下文:您首先问的是 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 readabilityconsole.log(numOfElements); // Debugif(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;    }  }参考 - https://codecept.io/helpers/
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答