多个期望在单个函数中

时间:2017-10-16 21:23:30

标签: javascript protractor cucumberjs

我在量角器中编写测试,并且我编写了一个带有2个期望语句的函数

 this.Then(/^I should see a pane on the right with an Interactions and Remarks tab$/, () => {
   return waitForPresence(mobileQADashboard.getTabPanel()).then(()=>{
      return mobileQADashboard.selectInteractionsTab().then(()=>{
          return waitForPresence(mobileQADashboard.pageElements.viewAllInteractionsLink).then(()=>{
             return expect(mobileQADashboard.pageElements.viewAllInteractionsLink.isDisplayed()).to.eventually.be.true;

          });
      });
   });
   return waitForPresence(mobileQADashboard.getTabPanel()).then(()=>{
       return mobileQADashboard.selectRemarksTab().then(()=>{
           return waitForLoader().then(()=>{
               return  waitForPresence(mobileQADashboard.pageElements.addRemarkButton).then(()=>{
                   return expect(mobileQADashboard.pageElements.addRemarkButton.isDisplayed()).to.eventually.be.true;
               });
           })
       });
   })

});

这是一个傻瓜证明的方法,我想知道写这样的函数的天气是正确的

1 个答案:

答案 0 :(得分:0)

对我来说,你看起来有点过分了。对于正常情况,量角器应该已经处理同步执行。

只要您的命令按列出的顺序执行(看起来就是这样),您就不需要构建这样的金字塔。

但是,当您使用then()时,您可以立即解决承诺,但您也可以启动新的异步任务,让量角器继续使用then()之外的行。因此,当您在第2行输入第一个then()时,函数的第二部分将与第一部分并行执行(不确定,如果是这样的话)。

关于案件中间的expect:虽然不是最佳做法,但仍有效。如果您在中间的期望失败,则测试用例将持续到结束,但测试用例状态仍然失败。您更有可能有两个测试用例而不是一个。

this.Then(/^I should see a pane on the right with an Interactions and Remarks tab$/, () => {
    waitForPresence(mobileQADashboard.getTabPanel());
    mobileQADashboard.selectInteractionsTab();
    waitForPresence(mobileQADashboard.pageElements.viewAllInteractionsLink);
    expect(mobileQADashboard.pageElements.viewAllInteractionsLink.isDisplayed()).to.eventually.be.true;

    //gets now executed after the firt expect. In your code it's executed in parallel to the first.
    waitForPresence(mobileQADashboard.getTabPanel());
    mobileQADashboard.selectRemarksTab();
    waitForLoader();
    waitForPresence(mobileQADashboard.pageElements.addRemarkButton);
    expect(mobileQADashboard.pageElements.addRemarkButton.isDisplayed()).to.eventually.be.true; 
});

也不希望在页面对象中包含expect。要编写测试用例(it() - 块),你应该a)保持对pass / fail的控制,b)同时不需要查看pageObject。没有它,人们应该理解测试用例。

总而言之,所有正确的方式似乎都是这样的:

it(/first case/,function(){
    this.ThenFirst();
    expect(mobileQADashboard.pageElements.viewAllInteractionsLink.isDisplayed()).to.eventually.be.true;
});
it(/second case/,function(){
    this.ThenSecond();
    expect(mobileQADashboard.pageElements.addRemarkButton.isDisplayed()).to.eventually.be.true; 
});

然后是这些页面对象:

this.ThenFirst(/^I should see a pane on the right with an Interactions and Remarks tab$/, () => {
    waitForPresence(mobileQADashboard.getTabPanel());
    mobileQADashboard.selectInteractionsTab();
    waitForPresence(mobileQADashboard.pageElements.viewAllInteractionsLink);
};
this.ThenSecond(/^I should see a pane on the right with an Interactions and Remarks tab$/, () => {
    waitForPresence(mobileQADashboard.getTabPanel());
    mobileQADashboard.selectRemarksTab();
    waitForLoader();
    waitForPresence(mobileQADashboard.pageElements.addRemarkButton); 
});