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.

adritha84
  • 957
  • 1
  • 7
  • 15

2 Answers2

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.

user664833
  • 18,397
  • 19
  • 91
  • 140
Ionuț G. Stan
  • 176,118
  • 18
  • 189
  • 202
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?

socjopata
  • 5,028
  • 3
  • 30
  • 33
  • I am using Selenium! `Capybara.default_driver = :selenium` – adritha84 Mar 28 '12 at 07:37
  • 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. – socjopata Mar 28 '12 at 10:20
  • 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. – adritha84 Mar 28 '12 at 13:37
  • 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? – mattvv May 03 '12 at 23:02