Automatically scroll buttons to view with Capybara and Selenium

Sometime last month (June 2013), some of our Capybara tests started with an error, mainly because the buttons that they are trying to click do not appear. Ideally, I would like to find out what has changed. We are currently working on selenium-webdriver 2.33, but I tried to return to 2.29 and it still does not work. We only work against Firefox at the moment, and possibly due to a newer version of Firefox.

The ban is that I can’t figure out how to scroll buttons in a view. From what I'm compiling, I can use scrollIntoView, but not sure what to name it in the Capybara step. I tried the options:

Capybara.current_session.driver.execute_script("arguments[0].scrollIntoView(true;)", find_button(button).native) 

But no luck, because find_button itself is not working.

Note: we select based on the text of the button. ID-based selection is possible, but will require many changes to our user interface tests, so we would like to avoid this.

+7
selenium-webdriver capybara
source share
3 answers

I usually have a JavascriptDriver module that I use to include Selenium functionality in the test, and there I define a helper method:

 module JavascriptDriver # other code that prepares capybara to work with selenium def scroll_to(element) script = <<-JS arguments[0].scrollIntoView(true); JS Capybara.current_session.driver.browser.execute_script(script, element.native) end end 

And then in your test, you can use this code by passing a regular Capybara element:

 scroll_to(page.find("button.some-class", visible: false)) 
+18
source share

This is a scroll error that appeared in Selenium and Chrome. Bug fix stack overflow

+1
source share

Since your page does not scroll, you can use the same approach as the related answer specified in another answer, but with an option to customize your scrollable element. For example, if your scrollable item has a scrollable id:

 page.execute_script "document.getElementById("scrollable").scrollTop += 100" 

I personally don't like scrolling tests, so if anyone comes up with a better solution for Capybara + Selenium, I would like to see it.

0
source share

All Articles