尝试使用selenium webdriver断言值,但似乎有些不对劲

时间:2015-09-15 09:37:09

标签: java selenium-webdriver

这里有什么问题:

  assert(Base.getdriver().findElement(By.xpath("//*[@id='mainBG']/div[1]/form[1]/div/div[1]/div/span[2]/span/span").getText().contains?("Email address is required"),"Validation message for Email is firing")));

在Eclipse中,它显示处的支架缺少红线("需要电子邮件地址")

测试场景:

有登录表单,我点击提交按钮而不填写电子邮件字段中的任何数据,只是想验证它的验证警告消息需要电子邮件地址。

2 个答案:

答案 0 :(得分:1)

尝试将其分解为这样以改善阅读(请注意我正在编写返回类型)

Element element = Base.getdriver().findElement(By.xpath("//*[@id='mainBG']/div[1]/form[1]/div/div[1]/div/span[2]/span/span"));
String elemText = element.getText();
assert elemText.contains("Email address is required") : "Validation message for Email is NOT firing";

出于好奇,contains之后的问号是什么?

编辑:因为似乎断言是本机Java断言,所以请尝试这样做。

答案 1 :(得分:0)

我会使用Hamcrest断言http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html

assertThat(selenium.findElement(By.xpath("//*[@id='mainBG']/div[1]/form[1]/div/div[1]/div/span[2]/span/span").getText(), containsString("Email address is required" ,"Validation message for Email is firing"));

可能你想先将字符串存储到变量中,然后打印它(然后显然删除这段代码),以确保你断言正确的数据:

String emailData = selenium.findElement(By.xpath("//*[@id='mainBG']/div[1]/form[1]/div/div[1]/div/span[2]/span/span")).getText();
System.out.println("Data to be printed" +emailData);

导入:

import static org.hamcrest.CoreMatchers.*;