请问我究竟做错了什么。AssertTrue in try/catch
我检查过,但都没有效果。 我也检查了以前的代码,但我没有得到一个错误,所以我的代码工作正常,但只是轻微的错误的地方。
代码运行良好,并预期assertTrue
的表现,但是当我把它放在try
/catch
,我只得到日志中的catch块,甚至当文字被发现。
我相信,如果assertTrue
找到文本,它应该转到try
块的下一行代码,并通过测试而不是catch
块。不要误解我的意思,我没有收到任何错误,只是它打印出错误的信息。
以下代码包括控制台中的打印输出消息。
public boolean verifyTextPresent(String value) throws Exception {
Thread.sleep(5000);
try{
boolean txtFound = driver.getPageSource().contains(value);
log.log(value + " : text Found, .......continue");
return txtFound;
}catch(Exception e)
{
log.log(value + " :NOT Found, check element again ot Contact developer.");
return false;
}
}
public static void verifySignOutBtn() throws Exception
{
log.header("VERIFY IF SIGN_OUT EXIST AND CLICKABLE.........");
callMethod.myAccountPageNative(CONSTANTElements.SIGN_IN_LINK);
Thread.sleep(2000);
log.header("LOCATE SIGN_OUT BTN, AND CLICK ......");
callMethod.elementPresent_Click(By.cssSelector(CONSTANTElements.SIGN_OUT_BTN));
Thread.sleep(4000);
log.header("VERIFY SIGN_OUT NAVIGATES TO HOME PAGE WHEN CLICKED......");
try{
Assert.assertTrue(callMethod.verifyTextPresent("SIGN IN"), "SIGN IN");
log.log("User Successfully Signed Out.......");
log.log("Test Passed!...");
//callMethod.close();
}
catch(Throwable e)
{
log.log("User NOT Successfully Signed Out.... Contact developer.");
log.log("Test Failed!...");
//callMethod.close();
}
callMethod.close();
}
}
Msg in console:
SIGN IN : text Found, .......continue
User NOT Successfully Signed Out.... Contact developer.
Test Failed!...
混乱的部分是,为什么它打印出catch
块,而不是在try
块的下一行?
为什么你认为'尝试布尔txtFound = driver.getPageSource()。contains(value); log.log(value +“:text Found,....... continue”);}'不会打印'Found'? – jhamon
@jhamon .........感谢您的回复......不要错。 verifyTextPresent()的行为如预期。我只添加了该方法,以便您可以看到如何编写该方法。问题是为什么打印catch块而不是verifySignOutBtn()中的try块?请再次阅读我的问题.... tanx – Joe
好吧,有2个try/catch块,你不坐在哪一个是有缺陷的。有了这些信息,我会说'veryTextPresent'总是返回false。编辑:LordAnomander是对的,交换'assertTrue'参数。 – jhamon