design firms in singapore,singapore web development,singapore mobile application developer,mobile app development singapore,singapore web design services,ios app development singapore,web development singapore,developer in singapore,ios developer singapore,website development singapore,singapore app developer,web designer singapore,mobile developer singapore,website designer singapore,design agency singapore,singapore mobile app developer,app development singapore,singapore web design,ruby on rails developer singapore,mobile apps singapore,singapore website design,mobile app developer singapore,web design services singapore,web development company singapore,website developer singapore,android developer singapore,web design company singapore,mobile application development singapore,app developer singapore,developers in singapore,web application singapore,mobile application developer singapore,mobile apps development singapore,web design singapore,website design singapore,graphic designer in singapore,mobile game developer singapore

Testing for javascript alerts with Cucumber & Capybara

Using Cumcumber + Capybara to run automated testing is a TRENDY now. But you might have trouble finding a way to assert a javascript alert, for e.g. a link created by the link_to … :confirm => “Are you sure?” and similar.

Don’t worry, here is a work around to confirm Javascript alert and dialog in your Capybara tests, simply override alert and confirm methods:

Given /^I will confirm on next step$/ do
begin
evaluate_script(“window.alert = function(msg) { return true; }”)
evaluate_script(“window.confirm = function(msg) { return true; }”)
rescue Capybara::NotSupportedByDriverError
end
end

Rescue here is added in case, you run want to run your tests without javascript (e.g. default capybara driver). If you run feature without javascript simply nothing will happen.

Moreover, using this technique you can simulate moving mouse over some elements. It is useful, when you have links that appears when certain div is under the mouse cursor.

Then /^I move mouse over “([^”]*)”$/ do |label|
begin
Capybara.current_session.driver.browser.execute_script(“$(‘#{label}’).mouseover();”)
rescue Capybara::NotSupportedByDriverError
end
end

That’s it. Happy testing !

Malcare WordPress Security