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.

adb insufficient permissions for device

Problem

You are trying to use the Android Debug Bridge (adb), but running it from the command line trying to list the connected devices you get the error:

insufficient permissions for device

Solution

You would need to start the adb server with su permissions, so you can try the following:

adb kill-server
sudo adb start-server
adb devices
adb logcat

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

deploy:assets:precompile insists on running on production environment in capistrano

Problem
You want to deploy your rails 3.2 application to another environment except production, but the default capistrano recipe for precompiling the assets keeps using the production environment as in:

 * executing `deploy:assets:precompile'
  * executing "cd /var/www/dev/app/releases/20120816130649 && rake RAILS_ENV=production RAILS_GROUPS=assets assets:precompile"
    servers: ["server.name.com"]

Solution
Add the following environment option in your deploy/other_env.rb file:

set :rails_env, "other_env"

Creating a user token with Devise for other uses except log in into the system

Problem

You are using Devise in your rails application, and you want to have a token for authenticating an API call, but you don’t want to use the default authentication token so that.
The purpose of doing something like that is that you don’t want the application to have the same authentication rights as the devise authentication tokens.

Solution

Add the field to your user model (ie app_token).

On your user model create the following:

class User < ActiveRecord::Base

before_create :create_app_token
...
private
def create_app_token
  self.app_token = Devise.friendly_token
end