I am trying to convert all my tests to the Page Object Pattern and very much like the OO goodness that comes along with it. I am kind of confused about the field initialization of page object fields. With the idea to map all the elements of the page in their respective page objects I have been using the annotations . However, since the app I am testing uses javascript and is kind of Ajaxy, I end up retrieving webelements through the use of utility methods that use WebDriverWait with ExpectedConditions. Now, most of the methods in ExpectedConditions use locator arguments . I would very much like to pass WebElement as arguments so that I wouldn't have to hard code the locators in my methods. But , if I am correct, passing web element as argument might trigger the find call first before the wait . So I am kind of confused regarding how people use the annoations in the PageFactory .
When I use the following method and pass the webelement as an argument, it kind of works and I am confused why wouldn't it make the findBy call and trigger an element not found exception instead of working fine.
I know I totally sound crazy and confused but hey, it's Monday ;)
public static WebElement waitAndFindElement(WebDriver driver, WebElement element, int timeOutInSeconds){
WebDriverWait wait = new WebDriverWait(driver,timeOutInSeconds);
WebElement e = wait.until(ExpectedConditions.visibilityOf(element));
return e;
}
public static WebElement waitForElementToBeClickable(WebDriver driver, By by, int timeOutInSeconds){
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(by));
return element;
}