Reputation: 841
I want to check 2 states (conditions) of a single function in a try block before going to catch block.
I have a search functionality, that holds true for one of the two following webdriver waits as long as the functionality works properly.
1. new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(kenshoSearchVerify));
2. new WebDriverWait(driver,5).until(ExpectedConditions.visibilityOfElementLocated(kenshoNoResultVerify));
I have a test where user enters the term in the box, if result exist for the search term, then 1 holds true, and if no result exist then 2 holds true.
Now I want to add a third option here, to check if the search functionality is broken or not, whether it is finding one of the above two waits or not.
I have my function like this,
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!");
}
}
How can I check the two waits in try block before moving forward to catch block? The above code is clearly wrong in the try block.
Upvotes: 0
Views: 1919
Reputation: 25542
Instead of catching exceptions, have you tried something like this? I'm assuming the waits aren't really necessary... but they may be.
public void kenshoSearch(String searchTerm) throws Exception
{
driver.findElement(kenshoSearchBox).sendKeys(searchTerm);
if (driver.findElements(kenshoSearchVerify).isEmpty() && driver.findElements(kenshoSearchVerify).isEmpty())
{
Assert.fail("Something's wrong with the search!");
}
}
Upvotes: 0
Reputation: 109547
Not sure at all, try
try {
new WebDriverWait(driver, 5)
.until(ExpectedConditions.visibilityOfElementLocated(
By.All(kenshoSearchVerify, kenshoNoResultVerify)));
}
Upvotes: 0
Reputation: 1584
This will do the trick:
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!");
}
}
}
Upvotes: 2