I am having some issues with the way the WebDriverWait and FluentWait is (not) working.
First of all, the requirement: Checking for the visibility of an validation error after submitting a login form.
So, I perform a successful login which leaves me at another page. To check if the login was fine I poll for the validation error which obviously is not there. To do that, I am checking the webelement that is supposed to contain the validation error message for it's visibility. Since it wont be there I am expecting a timeout or no such element exception.
I have tried it like this:
public static WebElement isVisible(By webElementLocator, WebDriver driver) {
WebElement element = (new WebDriverWait(driver, 1))
.ignoring(TimeoutException.class, NoSuchElementException.class)
.until(ExpectedConditions.visibilityOfElementLocated(webElementLocator));
return element;
}
I am ignoring timeouts and missing elements but get a timeout exception anyway. My question is: WHY?
I have a version of that method where I can pass a WebElement instead of an By-Locator. Looks like this:
public static WebElement isVisible(WebElement webElement, WebDriver driver) {
WebElement element = (new WebDriverWait(driver, 2))
.ignoring(TimeoutException.class, NoSuchElementException.class)
.until(ExpectedConditions.visibilityOf(webElement));
return element;
}
This, however, results in a longer wait of 20 seconds because (that is my guess) the implicit timeout set by the AjaxElementLocatorFactory I use to retrieve the Webelements overrides that wait instruction in the isVisible Method. This is very unexpected. Am I doing something wrong? This also raises a timeout exception which it should not.
//excerpt of a page object
public class SomeLoginPage extends ABaseLoginPage {
...
@FindBy(css= "p.form-group.login-toggle > a > span")
private WebElement loginToggleButton;
@FindBy(className="login-center-form")
private WebElement loginForm;
@FindBy(css="div.alert.alert-danger > span")
private WebElement loginValidationError;
By ByloginValidationError = By.cssSelector("div.alert.alert-danger > span");
public SomeLoginPage(WebDriver driver) {
super(driver);
AjaxElementLocatorFactory factory = new AjaxElementLocatorFactory(this.driver, 20);
PageFactory.initElements(factory, this);
}
@Override
public String getValidationErrorMessage() {
String valErrMsg = "";
WebElement valErr = DriverUtils.isVisible(ByloginValidationError, driver);
if(valErr!=null) valErrMsg = valErr.getText();
return valErrMsg;
}
}
Am I missing something?
I know I could work around this issues by just catching the Exception the the utility method by that would defeat the purpose of the waiting concept in general. Furthermore I want to avoid try-catch blocks as much as possible.