Javascript CodeceptJS/木偶演员不';不认识';如果';陈述

Javascript CodeceptJS/木偶演员不';不认识';如果';陈述,javascript,node.js,puppeteer,codeceptjs,Javascript,Node.js,Puppeteer,Codeceptjs,不太熟悉js/node.js。使用codeceptjs/puppeter进行一些自动化测试。现在正在尝试编辑测试中的描述。但有时描述不存在,因此“编辑描述”按钮不存在,而是存在“添加描述”按钮。所以我写了一个if语句来指定。但代码只是在它上面滚动。不管语句是什么,它只会移到下一行。当前if(desc)和if(!desc)都执行相同的功能-它转到if语句的下一行。这会导致错误,因为已有说明,因此“添加说明”按钮不可用。我不知道发生了什么事 Scenario('test something', (I

不太熟悉js/node.js。使用codeceptjs/puppeter进行一些自动化测试。现在正在尝试编辑测试中的描述。但有时描述不存在,因此“编辑描述”按钮不存在,而是存在“添加描述”按钮。所以我写了一个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!')
});

为您提供正确的上下文:您首先要问的是Node.js问题,而不是CodeceptJS或puppeter

desc
始终为
true
,因为您将其声明为字符串,因此无论
if
中的代码运行什么,正如您已经发现的那样

您可以使用以下操作:

const numOfElements=wait I.grabNumberOfVisibleElements(“#显示卡_说明部分按钮”);//使用CSS定位器而不是Xpath更易于阅读
console.log(numOfElements);//调试
if(numOfElements==1){
…
}

另请参见

维护人员不支持在场景函数中使用常规功能文件的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;
    }
  }
参考-