Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cucumber + testing JS alert

I'm trying to test a JS confirmation dialog with Cucumber on Rails. I have a window.onbeforeunload event handler that will prompt you with a confirmation dialog if you try to navigate away from the page but I have no idea how to test it, anyone have an idea on how this can be done?

like image 445
Bart Jedrocha Avatar asked Aug 10 '09 18:08

Bart Jedrocha


2 Answers

There are various functions of selenium you can use to capture alerts/confirms. They are not directly available with the webrat selenium implementation, but when using webrat's config.mode = :selenium they can be used as follows:

Then /^I should see a JS alert$/ do
    selenium.is_alert_present.should be_true
end

# or

Then /^I should see a "Are you sure?" JS confirm dialog$/ do
    selenium.get_alert.should eql("Are you sure?")
end

# you can also click the OK/Cancel buttons on confirm boxes with

selenium.chooseOkOnNextConfirmation();
#and
selenium.chooseCancelOnNextConfirmation();

There are probably not the greatest tests, but gives you an idea. Internally selenium overrides the alert() and confirm() functions of JS so it can captures this information.

You can find more docs on the selenium faq or on your gem server

like image 123
Rob Avatar answered Oct 05 '22 11:10

Rob


See the method definitions in http://selenium-client.rubyforge.org/classes/Selenium/Client/Idiomatic.html

You can invoke them with the selenium helper object in your Cucumber step definitions -- e.g.,

Then /^I should see a JS confirm dialog saying "([^\"]*)"$/ do |statement|    
  selenium.confirmation.should eql(statement)                               
end
like image 31
Brian Noguchi Avatar answered Oct 05 '22 11:10

Brian Noguchi