我正在自动化表单,方案是,当给出无效条目时,不应出现“成功”消息
我尝试使用
进行检查s_assert.assertEquals(driver.findElement(By.xpath("//div[contains(.,'Succes')]")).isDisplayed(), false);
但是,在运行时,它会显示“无法找到元素:”
仅当我的测试失败时才会显示该消息。所以预期的行为是,元素将不存在。
如何告诉webdriver
只检查它是否存在并且不会抛出错误!提前谢谢......
答案 0 :(得分:0)
您遇到的问题与 Assertion 无关,而是找到元素机制。您期望Selenium
找不到一个元素,因此直到 Assertion
您必须根据需要进行某种异常处理。断言之前。
public bool Test()
{
try
{
Driver.FindElement(By.Id("test"));
return true;
}
catch (Exception ex)
{ // catch the exception you want
return false;
}
}
public void TestAssert()
{
Assert.AreEqual(Test(),false);
}
注意:我的是C#和NUnit
答案 1 :(得分:0)
谢谢......我添加了以下代码来解决我的问题。
public static boolean isElementPresent(String xpath) {
try {
driver.findElement(By.xpath(xpath));
return true;
} catch (Exception e) {
return false;
}
}
public void formsNegetive() {
boolean b = isElementPresent((“// div [@ class ='alert-box success']”));
if(b){
s_assert.assertEquals(“成功:很快我们会联系您。”,driver.findElement(By.cssSelector(“div.alert-box.success”))。getText());
}
}