How can I get the geb webdriver to move to an element on the page that may not be in the view of the browser. The element exists on the page and is displayed but there may be a possibility that the element will appear on the bottom of the page and the browser would need to scroll down in order to bring the element into view.
import geb.Page
class myPage extends Page {
static url = "mypage.xhtml"
static at = {title == "myPage"}
static content = {
someElement = {$("#bottomOfPage")}
anotherElement = {$(".someClass",5)}
}
void clickElement(){
//possibility to fail because element may not be displayed in browsers view
//if not in view, scroll to element, then click
someElement.click()
}
}
Using this page as an example, If an element is not in view on the page and possibly at the very bottom, how can I tell the webdriver to bring the element into view? I have some complicated tests that are failing when the page contents are not in view. In some cases, the driver will move to the element even if it's not in view. But I would like to explicitly tell the driver to move to an element if its not in view for the cases that it does not move on its own.
The selenium library does have a moveToElement() function but it only accepts objects of the WebElement class. Using geb, when creating page classes, all elements that are used in my tests are declared in the content section and thus are of the SimplePageContent class. SimplePageContent cannot be casted to a WebElement either. moveToElement(By.id("bottomOfPage"))
does work but is not ideal because I do not want hard coded values. Id much rather pass the page content object itself if possible.
From my experience I can say that if you ask WebDriver to click on an element that is not currently in view it will first scroll to that element and then click on it. So you're either hitting some kind of bug if it does not happen or the fact that the element is not in view is not what's causing your element not to be clicked.
If you still want to move to the element explicitly using Actions.moveToElement()
then you can easily turn objects returned from your content definition using the fact that they implement Navigator
which comes with a firstElement()
which you should use if you want to get the first WebElement
of a multi element Navigator
or singleElement()
when your Navigator
has exactly one WebElement
.
There is also a simpler way, which won't require extracting WebElement
s from your Navigator
s - use an interact {}
block:
interact {
moveToElement(someElement)
}