使用“期望”检查部分文本内容

时间:2019-06-03 19:15:26

标签: selenium protractor

我想使用“期望”来验证元素中是否存在某些文本字符串。

我正在查看的元素将包含以下文本:

“您的预订参考是:DBM038763。”

我要确认文本中出现“ DBM”

var conf1 = element(by.xpath(".//*[@id='root']/div[2]/div/div[2]/div/section/div[1]/div[2]/div/h4")).getText();
    expect(conf1.textToBePresentInElement("DBM"));

这将返回:

    - Failed: conf1.textToBePresentInElement is not a function

我确定我遗漏了一些非常明显的东西! 谢谢

2 个答案:

答案 0 :(得分:1)

如果<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js" integrity="sha384-NaWTHo/8YCBYJ59830LTz/P4aQZK1sS0SneOgAvhsIl3zBu8r9RevNg5lHCHAuQ/" crossorigin="anonymous"></script> <iframe id=iframe></iframe> <script> var doc = new jsPDF(); doc.text("Hello World", 35, 25); var blob = doc.output( 'blob' ) var file = new File( [blob], 'a_name.pdf', { type: 'application/pdf' } ) iframe.src = URL.createObjectURL( file ) </script> 中存在值,则可以使用以下内容。

conf1

答案 1 :(得分:0)

textToBePresentInElementexpectedCondition,但是您尝试将其作为conf1上的方法来调用,这是一个承诺,最终会通过getText()返回一个字符串。您需要稍微更改语法以使用预期条件,如下所示:

var EC = protractor.ExpectedConditions;
var conf1 = element(by.xpath(".//*[@id='root']/div[2]/div/div[2]/div/section/div[1]/div[2]/div/h4")).getText();
expect(EC.textToBePresentInElement(conf1, "DBM")).toBe(true); //added .toBe(true)

上面的链接中提供了默认示例

var EC = protractor.ExpectedConditions;
// Waits for the element with id 'abc' to contain the text 'foo'.
browser.wait(EC.textToBePresentInElement($('#abc'), 'foo'), 5000);
相关问题