initializing page object fields - how does the @FindBy call work in PageFactory

377 views
Skip to first unread message

StatusQuo

unread,
Oct 1, 2012, 11:35:48 AM10/1/12
to webd...@googlegroups.com
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;
}

awt

unread,
Oct 2, 2012, 4:51:25 PM10/2/12
to webd...@googlegroups.com
Hi,
I like the idea of using the PageFactory and FindBy annotations but like you have found that I cannot use it in a dynamic page (very well). So, I ended up using the annotations for elements that I know will be static and ExpectedConditions for the dynamic elements. Not really very satisfactory and I am considering a re-write just using ExpectedConditions.

Your call probably works because it is visible in the DOM when you ask for it to be looked up.

awt

StatusQuo

unread,
Oct 3, 2012, 10:57:47 AM10/3/12
to webd...@googlegroups.com
Thank you for your response. I felt like I was missing something when I could not really benefit from the FindBy annotations and the PageFactory . I guess, I would keep using properties file for a repository of locators of all the dynamic elements in the page object .



Reply all
Reply to author
Forward
0 new messages