public void verifyErrorMsgforInvalidUserTransPosting(
{
String errorMSG="You dont have permission to create transaction using this ID";
String errorMSGSYS=driver.findElement(By.xpath("html/div/.....").getText();
if(errorMSGSYS.equals(errorMSG))
{
System.out.println("System didnt allowed user to post the Transaction");
report.updateTestLog("Verify System is NOT allowing user to post transaction for an invalid user", "System didnt allowed user to post the Transaction", Status.SCREENSHOT);
}
else
{
System.out.println("System is allowing user to post the Transaction which is NOT expected behavior");
report.updateTestLog("Verify System is NOT allowing user to post transaction for an invalid user", "System is allowing user to post the Transaction which is NOT expected behavior", Status.FAIL);
}
}
在上面的代码中,如果(条件1)在找到元素并且条件1满足时将起作用 else语句在不匹配时起作用
有些人可以帮助我在使用try catch或任何其他方法找不到元素时如何处理else。
答案 0 :(得分:0)
这是你想要的东西吗?
public void verifyErrorMsgforInvalidUserTransPosting(
{
String errorMSG="You dont have permission to create transaction using this ID";
Boolean isPresent = driver.findElements(By.xpath("html/div/.....").size() > 0;
if(!isPresent)
{
System.out.println("Failed to find element");
return;
}
String errorMSGSYS = driver.findElement(By.xpath("html/div/.....").getText();
if(errorMSGSYS.equals(errorMSG))
{
System.out.println("System didnt allowed user to post the Transaction");
report.updateTestLog("Verify System is NOT allowing user to post transaction for an invalid user", "System didnt allowed user to post the Transaction", Status.SCREENSHOT);
}
else
{
System.out.println("System is allowing user to post the Transaction which is NOT expected behavior");
report.updateTestLog("Verify System is NOT allowing user to post transaction for an invalid user", "System is allowing user to post the Transaction which is NOT expected behavior", Status.FAIL);
}
}
答案 1 :(得分:0)
我会做一些不同的事情。你有一些重复几个地方的字符串,所以我把它们全部放在首位。我认为它使代码更容易阅读。我还处理了在检查文本值之前确保所需元素存在的情况。
public void verifyErrorMsgforInvalidUserTransPosting()
{
String testDescription = "Verify System is NOT allowing user to post transaction for an invalid user";
String expectedCondition = "System didnt allowed user to post the Transaction";
String unexpectedCondition = "System is allowing user to post the Transaction which is NOT expected behavior";
String errorMSG = "You dont have permission to create transaction using this ID";
List<WebElement> error = driver.findElements(By.xpath("html/div/....."));
if (error.isEmpty())
{
System.out.println("Failed to find element");
return;
}
String errorMSGSYS = error.get(0).getText();
if (errorMSGSYS.equals(errorMSG))
{
System.out.println(expectedCondition);
report.updateTestLog(testDescription, expectedCondition, Status.SCREENSHOT);
}
else
{
System.out.println(unexpectedCondition);
report.updateTestLog(testDescription, unexpectedCondition, Status.FAIL);
}
}
注意:我注意到您的示例XPath以HTML
标记开头。我希望你没有实际执行的代码......这是一个非常非常糟糕的主意,因为它使XPath非常脆弱。 HTML结构中的任何更改都将破坏该定位器。我建议你寻找方法来找到更接近所需元素的元素而不使用所有相对路径。
答案 2 :(得分:0)
请记住,driver.findElements()可能会返回当前不可见的元素(如果您有多个页面)。因此,这可能是一个问题。
我会推荐以下内容:
try
{
String errorMSGSYS=driver.findElement(By.xpath("html/div/.....").getText();
}
catch(Exception e)
{
//code to handle "element not found"
}
//normal flow as earlier
在这种情况下,如果找到了元素,则您的正常流程将按预期工作。如果未找到element,则findElement将抛出一个异常,该异常将在catch块中捕获。您可以按需要处理流程。
希望这有帮助。