我想在进入 catch block 之前检查 try block 中单个函数的 2 个状态(条件)。
我有一个搜索功能,只要该功能正常工作,它就适用于以下两个网络驱动程序等待之一。
1. new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(kenshoSearchVerify));
2. new WebDriverWait(driver,5).until(ExpectedConditions.visibilityOfElementLocated(kenshoNoResultVerify));
我有一个测试,用户在框中输入术语,如果搜索术语存在结果,则 1 为 true,如果不存在结果,则 2 为 true。
现在我想在这里添加第三个选项,检查搜索功能是否损坏,是否找到上述两个等待之一。
我有这样的功能,
public void kenshoSearch(String searchTerm) throws Exception
{
driver.findElement(kenshoSearchBox).sendKeys(searchTerm);
try{
new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(kenshoSearchVerify));
new WebDriverWait(driver,5).until(ExpectedConditions.visibilityOfElementLocated(kenshoNoResultVerify));
}
catch(Exception e){
Assert.fail("Something's wrong with the search!");
}
}
在进入 catch block 之前,如何检查 try block 中的两个等待?上面的代码在try block 中显然是错误的。
最佳答案
这样就可以了:
public void kenshoSearch(String searchTerm) throws Exception
{
driver.findElement(kenshoSearchBox).sendKeys(searchTerm);
try{
new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(kenshoSearchVerify));
}
catch(Exception e){
try{
new WebDriverWait(driver,5).until(ExpectedConditions.visibilityOfElementLocated(kenshoNoResultVerify));
}
catch(Exception e){
Assert.fail("Something's wrong with the search!");
}
}
}
关于java - 如何在执行 catch block 之前检查 try block 中的 2 个条件 - Selenium WebDriver Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32718602/