(Capybara) access modal window
Asked Answered
T

2

7

I am writing request specs ... I use Capybara... And I am in trouble with some modal windows.

What I actually want in my test is to fill in a form that pops up in a modal window.

The modal is created with Bootstrap from Twitter (http://twitter.github.com/bootstrap/javascript.html#modals)... and it's going through a set of transitions (but I don't know if this is relevant to what I'm about to say).

I have tried a few workarounds I found on the web, like:

A) switching between pages with page.driver.browser.window_handles

page.driver.browser.switch_to.window(page.driver.browser.window_handles.last)

B) using wait_until to make sure that the modal loads

def modal_wrapper_id
  '#modal-edit'
end

def modal_visible
  wait_until { find(modal_wrapper_id).visible? }
rescue Capybara::TimeoutError
  flunk 'Expected modal to be visible.'
end

but none of those worked... so I thought to render the number of window handles at the moment when the modal window is active...

So I did this:

puts page.driver.browser.window_handles.length.should == 2 

And I got this:

Failure/Error: page.driver.browser.window_handles.length.should == 2
           expected: 2
           got: 1 (using ==)

From what I understand, practically my modal window doesn't exist.

Any help on this one would be much appreciated.

Thank you.

Tombouctou answered 21/3, 2012 at 12:54 Comment(0)
Y
1

I didn't use Capybara, but your problem has to do with the fact that Bootstrap's modal dialog is actually a pseudo-modal, in that it's actually just a div element and a transparent overlay behind it. A true modal dialog would be one created using window.confirm, for example, which can indeed be queried using your sample code. In your case you should give the modal div element an id, and use that as a handle to query it from Capybara and wait until its display is "block". Didn't test anything though.

Yolondayon answered 20/6, 2012 at 9:41 Comment(0)
D
1

Capybara by default uses :rack_test driver. Can you confirm you're using Selenium WebDriver or other driver where opening a modalbox is actually possible?

Destine answered 27/3, 2012 at 10:6 Comment(4)
I am using Selenium! Capybara.default_driver = :seleniumTombouctou
Ok, you have not mentioned this in your post. Since I don't see anything wrong, I would make sure that you really invoke that pop-up in your test suite (by clicking something, etc, I don't know the business logic). Also you can put some sleep 10 after that 'invoking pop-up' action, just for testing purposes.Destine
I did put some sleep actions in my spec and I've tested it in many ways... and, as you said, I don't see anything wrong either... But still, I get the specified error, meaning the handle to that modal window doesn't exist, thus the window doesn't exist.Tombouctou
I'm getting the same with with Selenium - If i put a breakpoint I can see the modal isn't popping - were you able to resolve it?Amputee
Y
1

I didn't use Capybara, but your problem has to do with the fact that Bootstrap's modal dialog is actually a pseudo-modal, in that it's actually just a div element and a transparent overlay behind it. A true modal dialog would be one created using window.confirm, for example, which can indeed be queried using your sample code. In your case you should give the modal div element an id, and use that as a handle to query it from Capybara and wait until its display is "block". Didn't test anything though.

Yolondayon answered 20/6, 2012 at 9:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.