无法找到动态文本的

时间:2018-05-18 01:24:12

标签: java selenium xpath automation

我正在编写一些Selenium UI自动化测试,而我目前正在开发一个部分,根据源系统的响应,可以出现多个验证消息。我有几个不同的消息要触发。

每次在UI上显示错误消息时,它都将位于以下元素中,但文本字符串每次都会更改,具体取决于消息的内容,而id和类保持不变:

<span id="lblErrorText" class="validationError">
    "My error message will appear here"
</span>

在我的页面对象类中,我在一个方法中使用它之前定义了我的字段。所以我有以下内容:

By errorMessage  = By.xpath("//span[@class='validationError']");
然后我在方法中使用errorMessage将其作为布尔值返回,以使我能够断言。如下:

public boolean getInvalidRefNumberErrorMessage(){
    return driver.findElements(errorMessage).contains("My error message will");
}

但每次运行测试时,我都会看到getInvalidRefNumberErrorMessage方法返回FALSE。

我尝试使用id而不是运气。我之前从未验证过动态消息,所以我有点卡在这里。

1 个答案:

答案 0 :(得分:3)

如果你的代码没有复制/粘贴错误,那么你的代码似乎有些错误。

  1. 应该使用返回findElement的{​​{1}},而不是findElements
  2. 应致电List将消息内容作为getText()返回,然后致电String

    String.contains()
相关问题