Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check alert box text in Capybara

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 ?

like image 844
userxyz Avatar asked Feb 25 '14 10:02

userxyz


1 Answers

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.

like image 200
raoul_dev Avatar answered Oct 18 '22 14:10

raoul_dev