量角器,如果其他语句与期望

时间:2019-03-18 12:14:15

标签: javascript automation jasmine protractor

我有三个要检查的代码是否存在。当我运行我的代码时,我收到一条消息,提示“ Expected false to true”。我在做什么错

var Ecode1 = element(by.xpath('.//*[contains(text(),\'201\')]'));
  var Ecode2 = element(by.xpath('.//*[contains(text(),\'202\')]'));
  var Ecode3 = element(by.xpath('.//*[contains(text(),\'501\')]'));
if (expect(Ecode1.isPresent()).toBe(true)) {
    //ecode1 rules
  } 
  else if (expect(Ecode2.isPresent()).toBe(true)) {
    //ecode2 rules
  }
  else if (expect(Ecode3.isPresent()).toBe(true)) {
   //ecode3 rules
      }
  else {
    browser.close();
  }

1 个答案:

答案 0 :(得分:0)

只要像解决方案一样承诺isPresent,就可以:

async/await方法:

if (await Ecode1.isPresent()) {

} else if (await Ecode2.isPresent()) {

} else if (await Ecode3.isPresent()) {

} else {
  browser.close();
}

Web驱动程序Control Flow的方法很丑陋,并且需要额外的复杂性:

Promise.all([
    Ecode1.isPresent(),
    Ecode2.isPresent(),
    Ecode3.isPresent()
]).then((result)=>{
    // Ecode1 present
    if(result[0]) {

    }
    // Ecode2 present
    else if(result[1]) {

    }
    // Ecode3 present
    else if(result[2]) {

    }
    else {
        browser.close();
    }
});

此外,expect下的if语句是多余的