0

I have a page where I click a "change" link which displays another section. When this happens, the page scrolls vertically a bit so that the visible section is somewhat centered on the screen.

My next step in the test is to click something inside this newly shown container. But since the viewport scrolled, the coordinates that geb picks up for the clickable element are no longer accurate and geb tells me it can't click the link.

There's nothing I can assert in the waitFor in terms of visible content. But I'm wondering if there is a way for me to waitFor the content to stop scrolling?

waitFor { // page no longer scrolling }

If not, is there a way to just tell geb to wait a few seconds before moving on to the next event?

vidit
  • 6,293
  • 3
  • 32
  • 50
Gregg
  • 34,973
  • 19
  • 109
  • 214

2 Answers2

1

If you know what element you're scrolling to (the element that is at the top of browser viewport when you're done with scrolling) you can wait for the y property of a navigator representing that element to equal zero. Following is an example that you can paste into groovy console which goes to a page and then scrolls to an element by using it's id in the url (I know there is no waiting here nor the scrolling is animated but I just want to show how that property can be used to achieve want you want):

@Grapes([
   @Grab('org.gebish:geb-core:0.9.0'),
   @Grab('org.seleniumhq.selenium:selenium-firefox-driver:2.32.0')
])

import geb.Browser

Browser.drive {    
    //at the top of the page
    go 'http://docs.codehaus.org/display/GROOVY/Creating+an+extension+module'

    //an element we'll scroll to later
    def elem = $('#Creatinganextensionmodule-Themoduledescriptor')
    assert elem.y != 0

    //scroll to the element
    go 'http://docs.codehaus.org/display/GROOVY/Creating+an+extension+module#Creatinganextensionmodule-Themoduledescriptor'
    assert elem.y == 0
}

So you should end up with something like:

waitFor { elementWeScrollTo.y == 0 }

or even:

waitFor { !elementWeScrollTo.y }
erdi
  • 6,944
  • 18
  • 28
-2

I don't know how to express it in geb, but I'd write a loop checking the document.body.pageYOffset (document.body.scrollTop for IE) repeatedly until it settles down.

Ross Patterson
  • 9,527
  • 33
  • 48