Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test a confirm dialog with Cucumber?

I am using Ruby on Rails with Cucumber and Capybara.

How would I go about testing a simple confirm command ("Are you sure?")?

Also, where could I find further documentation on this issue?

like image 465
Yuval Karmi Avatar asked Mar 16 '10 22:03

Yuval Karmi


People also ask

How do Cucumber tests work?

How Cucumber testing works in a nutshell. Cucumber works by reading our specifications from plain English text files called feature files. It scans them for test scenarios, and runs those scenarios against our product. The feature files must follow a set of rules called Gherkin.

What are the files required to run a Cucumber test?

If you want to execute a Cucumber test, then make sure it has the following two files. 1- A feature file. 2- A step definition file. Q-10: What does a feature file contain?


3 Answers

The selenium driver now supports this

From Capybara you would access it like this:

page.driver.browser.switch_to.alert.accept 

or

page.driver.browser.switch_to.alert.dismiss 

or

 page.driver.browser.switch_to.alert.text 
like image 61
Derek Ekins Avatar answered Sep 18 '22 18:09

Derek Ekins


Seems like there's no way to do it in Capybara, unfortunately. But if you're running your tests with the Selenium driver (and probably other drivers that support JavaScript), you can hack it. Just before performing the action that would bring up the confirm dialog, override the confirm method to always return true. That way the dialog will never be displayed, and your tests can continue as if the user had pressed the OK button. If you want to simulate the reverse, simply change it to return false.

page.evaluate_script('window.confirm = function() { return true; }')
page.click('Remove')
like image 38
Theo Avatar answered Sep 22 '22 18:09

Theo


I've implemented these two web steps in /features/step_definitions/web_steps.rb:

When /^I confirm popup$/ do
  page.driver.browser.switch_to.alert.accept    
end

When /^I dismiss popup$/ do
  page.driver.browser.switch_to.alert.dismiss
end
like image 36
Dynamick Avatar answered Sep 21 '22 18:09

Dynamick