Check alert box text in Capybara
Asked Answered
D

1

8

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 ?

Dietz answered 25/2, 2014 at 10:33 Comment(0)
S
12

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.

Septillion answered 12/11, 2014 at 13:45 Comment(2)
is there a way to check for absence of alert? @SeptillionBradfield
@Bradfield This works for me: expect { page.driver.browser.switch_to.alert }.to raise_error(Selenium::WebDriver::Error::NoSuchAlertError)Zennas

© 2022 - 2024 — McMap. All rights reserved.