Check alert box text in Capybara

poltergeist, rspec-rails, ruby-on-rails

Solution

You can only do this if you are testing with Capybara using a javascript driver such as Selenium or Poltergeist. Using rspec/capybara and selenium you can do it like this:

text = page.driver.browser.switch_to.alert.text
expect(text).to eq 'Message you are looking for'

If you are using poltergeist alert boxes are automatically confirmed, and it seems there is currently no way of interrogating the box contents before this is done. You would need to test for some change or lack thereof after the alert box is dismissed.

Problem

I am using rspec and capybara for test with rails 4.0 I am displaying an alert box after sending ajax request. I want to test alert box text in my spec. Is there any way to test it ?

Original source