包含任何文本的元素的量角器预期条件 [英] Protractor expected condition for element containing any text

查看:75
本文介绍了包含任何文本的元素的量角器预期条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以检查元素中是否包含任何文本?我已经找到了textToBePresentInElement,但是此函数检查指定的值,如果失败,则不会返回正确的错误.

Is there a way how to check whether an element has any text in it? I already found textToBePresentInElement but this function checks for specified value and does not return a proper error if it fails.

我正在通过API填充元素,并且稍后将其加载,因此我希望浏览器等待直到元素中出现任何信息,然后检查正确的值.

I'm population the element via API and it's loaded bit later, so I want the browser to wait till any information appears in the element and then check for correct value.

或者,当EC发生故障时设法获得特定的错误消息也非常有帮助:

Alternatively it would be also very helpful to manage to get a specific error message when EC fails:

browser.wait(EC.textToBePresentInElement(element(by.binding('myvar')), "expected"), 5000);

推荐答案

browser.wait()的第三个参数是自定义错误消息:

The third argument to browser.wait() is a custom error message:

browser.wait(EC.textToBePresentInElement(element(by.binding('myvar')), "expected"), 5000, "Text is not something I've expected");

另请参阅:

要等待元素包含任何文本,可以编写自定义期望条件:

To wait for an element to contain any text, you can write a custom expected condition:

var EC = protractor.ExpectedConditions;

var anyTextToBePresentInElement = function(elementFinder) {
  var hasText = function() {
    return elementFinder.getText().then(function(actualText) {
      return actualText;
    });
  };
  return EC.and(EC.presenceOf(elementFinder), hasText);
};

这是用法:

browser.wait(anyTextToBePresentInElement(element(by.binding('myvar'))), 5000);

这篇关于包含任何文本的元素的量角器预期条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆