Interrupting a cucumber test

Problem

When you interrupt a cucumber test, usualy by presssing Ctrl+C twice, the database tables are not truncated, so you consequently could have a problem with duplicates records.

Solution

To make sure that the database is clear before running the test again run the following tasks to clear the database and clone it again:

rake db:test:purge
rake db:test:clone

Element is not currently visible and so may not be interacted with (Selenium::WebDriver::Error::ElementNotVisibleError)

Problem

Trying to run your cucumber tests and interact with links,buttons in a page you have the following error page:

Element is not currently visible and so may not be interacted with (Selenium::WebDriver::Error::ElementNotVisibleError)

Solution

Try to use the :visible => true in your matchers, finders like:

find('#id_name',:visible => true).click

Firefox 19 and unable to obtain stable firefox connection in 60 seconds

Problem
You have just upgraded your Firefox to the latest release (currently 19) and when running your javascript cucumber tests you get the following error:

 unable to obtain stable firefox connection in 60 seconds

Solution
Add the latest selenium-webdriver to your Gemfile and run bundle update:

gem "selenium-webdriver", "~> 2.30.0", :group => :test

javascript runtime gem required for cucumber tests with rails

Problem

According to the ‘Cucumber book’ the gems required to start using cucumber with a rails application are the following:

group
 :test do
gem
 'cucumber-rails', '1.2.1'
gem
 'rspec-rails', '2.7.0'
gem
 'database_cleaner', '0.7.0'
end

On the most recent rails version (currently 3.2.9) the gems need to be updated and also include the javascript runtime gem ‘therubyracer’

Solution

The following gems are working with Rails 3.2.9

group :test do
  gem 'cucumber-rails', '~> 1.3.0'
  gem 'rspec-rails', '~> 2.12.0'
  gem 'database_cleaner', '~> 0.9.1'
  # Add javascript runtime environment
  gem "therubyracer", "~> 0.10.2"
end

You can then run the

$ rails g cucumber:install

command to generate the files and folders necessary for cucumber.

Cucumber step to select auto suggested google map search result

Problem

You have a location search in your application that uses google maps auto suggest, and you want to be able to select the first item, so that it can be used in your cucumber tests.

Solution

You can create a step as in the code below, and put it in one of your cucumber step definitions:

When /^I do a map search$/ do
  item = page.find(".pac-container .pac-item:first")
  item.click
end

and then you can call this step from any other step in your cucumber tests by:

step "I do a map search"

Modal dialog present (Selenium::WebDriver::Error::UnhandledAlertError)

Problem
You are using cucumber for your tests and you would like to use the modal dialog confirmation box in a rails application, with capybara but you are getting the following error:

Modal dialog present Selenium::WebDriver::Error::UnhandledAlertError)

Solution

In your steps file use the following to accept (press OK), in the confirmation dialog that pops up:

page.driver.wait_until(page.driver.browser.switch_to.alert.accept)

NOTE: Or in more recent versions when the above complains for ‘undefined method wait_until’

page.driver.browser.switch_to.alert.accept

NOTE: Taken from the second comment made here

Cucumber testing and ssl

Problem

Your application is ussing force_ssl to redirect all calls to ‘https’, but when you try to use your cucumber tests they fail.

Solution

By using the suggestion here, you can add a file in your initializers to bypass the ssl in development and test environment as follows:

module ActionController
  module ForceSSL
    module ClassMethods
      def force_ssl(options = {})
        before_filter(options) do
          if !request.ssl? && !Rails.env.development? && !Rails.env.test?
            redirect_to :protocol => 'https://', :status => :moved_permanently
          end
        end
      end
    end
  end
end

Pausing between steps in cucumber

Problem

You want to be able to go through a scenario step by step when you are testing with a browser as sometimes can be very fast.

 

Solution

By following the suggestion here you would only need to add the following to your features/support/hooks.rb file

AfterStep('@pause') do
  print "Press Return to continue ..."
  STDIN.getc
end

and then you should be able to use it in your scenario by adding the @pause tag.
Then you would have to press continue in your terminal to go step by step

Cucumber – Carrierwave – ImageUploader – Variable class

Problem
You would like to test the carrierwave imageuploader with a cucumber test, but you would also like to use a generic step that can be used with different classes and different image file column names.

Solution
Use the following and perhaps put it a file named features/common_steps.rb

Then /^the url for the column "([^"]*)" of model "([^"]*)" should be "([^"]*)"$/ do |col,mdl,url|
  # First get tne class name from the mdl argument by using the Kernel.const_get method
  m = Kernel.const_get(mdl).first
  # And then use the send with the column name (col) to call the model's field,
  # the url method of ImageUploader to get the full path
  # and basename to get only the file name
  File.basename(m.send(col).url).should == url
end