首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CodeceptJS / Puppeteer不识别if语句

CodeceptJS / Puppeteer不识别if语句
EN

Stack Overflow用户
提问于 2019-10-18 23:23:36
回答 2查看 1.1K关注 0票数 1

不太熟悉js/node.js。使用codeceptjs/傀儡机进行一些自动化测试。现在试着在测试中编辑一个描述。但有时没有描述--因为“编辑描述”按钮不存在,而是有一个“添加描述”按钮。所以我写了一个if语句来指定。但代码就这么翻过来了。不管语句是什么,它只是移到下一行。目前,if (desc)if(!desc)都执行相同的函数--它转到if语句的下一行。这会导致错误,因为已经有了描述,因此“添加描述”按钮不可用。我不知道怎么回事。

代码语言:javascript
复制
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!')
});
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-10-21 12:22:54

为了给你正确的上下文:你是在问一个Node.js问题,而不是CodeceptJS或Puppeteer。

desc始终是true,因为您将它声明为字符串,所以不管if中的代码将运行什么,您已经知道了。

您可以使用这样的方法:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2019-12-03 19:00:21

维护人员不支持在场景函数中使用常规特性文件的if语句,并将其作为意外结果导致测试的不良做法,而必须在自定义助手文件中这样做:

代码语言:javascript
复制
   /**
   * 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/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58459223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档