How to scroll a modal window in Capybara

Viewed 2524

I'm testing with Selenium WebDriver, RSpec and Capybara.

I let the program successfully fill in some fields in a modal window. Now I want to click on a button that is at the bottom of this modal window. At the first glance, I cannot see this button, so Capybara needs to scroll down in the modal window.

The two relevant code snippets of the webpage:

<div class = “modal”> </div>
   <button class=”btn …..”> TextOnButton ::after </button>

I tried:

within('.modal') do
      find('.btn', text: ‘TextOnButton').scrollIntoView(true)
end

but received the error message.

Unable to find visible css ".btn" with text "TextOnButton"

I tried:

within('.modal’) do
       page.execute_script 'window.scrollBy(0,100)'
end

but then he scrolls the main window but not the modal window.

3 Answers

Assuming your HTML snippet is incorrect and the button element is actually contained in the modal (as in your text description), then you can try something like

within('.modal') do
  btn = find(:button, 'TextOnButton', visible: :all)
  execute_script('arguments[0].scrollIntoView(true)', btn)
  btn.click
end

I understand you are already executing code on the modal, but just wondering have you used driver.switchTo().window(handle) to switch to that modal first?

Related