sharagoz.com

Interacting with Javascript prompts with Capybara

Published July 21st 2011

I tried switching from Webrat to Capybara about 4 or 5 months ago, but aborted it due to some reason I can't recall. I think it was because the "have_selector" matcher wasn't implemented yet. I decided to give it another try today hoping to be able to implement some of those pending Javascript related specs Webrat was unable to handle.

And I was not disappointed, Capybara has excellent support for the selenium-webdriver. Look at this:



find('span', :text => 'Register your email address').click
prompt = page.driver.browser.switch_to.alert
prompt.send_keys('me@example.com')
prompt.accept
Email.first.address.should eq('me@example.com')

I am clicking a non-button/link that triggers a javascript prompt. I fill out the text field in this prompt and then click the "OK" button, and it works!

I'm in love with Capybara now.

Btw, converting the existing specs created for Webrat was a very easy job.
There was some syntax changes to the "have_selector" matcher, calls to "response" had to be replaced by "page", and the "contains" matcher had to be replaced by "have_content". Easy.

Right now I can't think of a single reason to choose Webrat over Capybara. Webrat isnt even maintained anymore as far as I can tell.