【问题标题】:CodeceptJS / Puppeteer doesn't recognize 'if' statementCodeceptJS / Puppeteer 无法识别“if”语句
【发布时间】:2020-02-15 22:40:45
【问题描述】:

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

【问题讨论】:

    标签: javascript node.js puppeteer codeceptjs


    【解决方案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

    【讨论】:

      【解决方案2】:

      维护者不支持在场景函数中使用常规特性文件的 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/

      【讨论】:

        猜你喜欢
        • 2017-09-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-25
        • 1970-01-01
        • 1970-01-01
        • 2017-01-03
        • 2020-08-30
        相关资源
        最近更新 更多