Kita Lance
Kita Lance

Reputation: 33

How to test confirm popup by cucumber in Ruby on Rails

I am trying to test a feature of my app in Ruby on Rails by Cucumber and Capybara: when you click "delete" button, there is a confirm says " Are you sure?" Then it is supposed to click "OK".

At first I simply tried

Given('I accept the popup') do
  click_button('OK')
end

Then Cucumber throw an error:

Unable to find button "OK" (Capybara::ElementNotFound)

Then I tried:

Given('I accept the popup') do
  page.driver.browser.switch_to.alert.accept
end

as mentioned in How to test a confirm dialog with Cucumber? Cucumber throw this error:

undefined method `switch_to' for #<Capybara::RackTest::Browser:0x0000000009241c20> (NoMethodError)

Then I tried add "@javascript" before my scenario in my 'test.feature' like:

@javascript
Scenario: Admin can manage scales
  Given I am on Scales page
  Given I destroy a scale
  Given I accept the popup
  Then I should see "Scale deleted"

Then Cucumber throw an error:

Unable to find Mozilla geckodriver. Please download the server from https://github.com/mozilla/geckodriver/releases and place it somewhere on your PATH. More info at https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver.

I'm so confused. Did I configure the environment wrong?

My Gemfile:

group :test do
  gem 'shoulda-matchers'
  gem 'simplecov', :require => false
  gem 'rails-controller-testing', '1.0.2'
  gem 'minitest-reporters', '1.1.14'
  gem 'guard', '2.13.0'
  gem 'guard-minitest', '2.4.4'
  gem 'capybara'
  gem 'launchy'
  gem 'selenium-webdriver'
  gem 'cucumber-rails', :require => false
  gem 'cucumber-rails-training-wheels'
end

My web_steps.rb:

require 'uri'
require 'cgi'
require 'selenium-webdriver'

Upvotes: 1

Views: 1658

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49850

When testing with a driver that doesn't support JS (RackTest) obviously you can't test JS triggered system modals. By adding the @javascript tag to your test you've told Capybara to swap to using a driver that does support JS (selenium driver).

The next error you're getting is self-explanatory - You don't have geckodriver installed in your system which is needed for selenium to talk to Firefox - If you had configured your driver to talk to Chrome instead it would need chromedriver. The easiest way to get them installed is to add webdrivers to your test gems - https://github.com/titusfortner/webdrivers#usage

Once you have that solved then you'll need to write your steps so they end up running the code

page.accept_confirm do
  click_button('delete') # The action that causes the confirm modal to appear
end

If you also want to verify the message in the confirm modal it would be

page.accept_confirm "Are you sure? do
  click_button('delete')
end

Upvotes: 2

Related Questions