【问题标题】:Protractor Execution hangs when validating element not present in DOM验证 DOM 中不存在的元素时量角器执行挂起
【发布时间】:2018-03-14 18:48:32
【问题描述】:

当我们验证测试用例在 dom 中不存在时,量角器执行会无限期挂起,这很奇怪。 下面是我们尝试过的代码,但它挂起不知道为什么。 它一直在等待 this.columnNamesInTableSingle 元素,即使它不存在于 DOM 中,我们只需要验证它

 var el = this.columnNamesInTableSingle;
        browser.wait(protractor.ExpectedConditions.not(protractor.ExpectedConditions.presenceOf(el)),5000);



expect(this.columnNamesInTableSingle.isPresent()).toBe(false);

 this.columnNamesInTableSingle.then(a=>{
    //         console.log(a.length+"==============================")
    //     },abc=>{
    //         console.log(abc)
    //     })

【问题讨论】:

  • 尝试使用 stalenessOf。无需添加'not'。protractortest.org/#/…
  • 我用过,但它再次挂在那里,没有抛出错误
  • 你还在等什么?它会因为某些动作而消失吗?如果不是,那么只需使用 expect 语句
  • 我有一个明确的列链接,点击它不会显示任何列。所以想通过验证没有显示列来测试它,但它没有帮助我也尝试过期望,但它挂起寻找那个元素
  • 我想看看你在“this.columnNamesInTableSingle”中有什么

标签: typescript protractor


【解决方案1】:

我知道这篇文章已经很老了,但我已经实现了一个函数,它可以用于测试 DOM 中是否存在(或不存在)元素,而无需任何 wait

3 个参数:

  • 字段:ElementFinder | ElementFinder[] 目标;
  • fieldName: string 目标名称(仅用于记录);
  • 出席:boolean 切换您的控制(测试是否存在);

功能:

export async function checkField(field: any, fieldName: string, present?: any) {
  let text: string;
  if (present === false) {
    if (field.length > 0) {
      field.forEach(async el => {
        try {
          text = await el.getText();
          expect('should not be present (' + fieldName + ')').toEqual('but it is');
        } catch (e) {
          expect(el.isPresent()).toBe(present);
        }
      });
    } else {
      try {
        text = await field.getText();
        expect('should not be present (' + fieldName + ')').toEqual('but it is');
      } catch (e) {
        expect(field.isPresent()).toBe(present);
      }
    }
  } else {
    if (field.length > 0) {
      field.forEach(async el => {
        try {
          text = await el.getText();
          expect(el.isPresent()).toBe(true);
        } catch (e) {
          expect('is present (' + fieldName + ')').toEqual('but i can\'t find it');
          throw e;
        }
      });
    } else {
      try {
        text = await field.getText();
        expect(field.isPresent()).toBe(true);
      } catch (e) {
        expect('is present (' + fieldName + ')').toEqual('but i can\'t find it');
        throw e;
      }
    }
  }
}

希望这仍然有用(我知道,可以写得更好)

如果这有效并满足您的问题,请考虑关闭它!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    • 1970-01-01
    • 2019-07-21
    • 1970-01-01
    • 2015-09-20
    • 1970-01-01
    相关资源
    最近更新 更多