Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

updating cucumber

  • Loading branch information...
commit 7623f39426e25ca24c3fa1b90fd0692195bab9f2 1 parent 5551d4f
@ricardoalmeida authored
View
2  config/cucumber.yml
@@ -1,7 +1,7 @@
<%
rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
-std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} --strict --tags ~@wip"
+std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~@wip"
%>
default: --drb <%= std_opts %> features
wip: --drb --tags @wip:3 --wip features
View
172 features/step_definitions/web_steps.rb
@@ -1,21 +1,46 @@
-# IMPORTANT: This file is generated by cucumber-rails - edit at your own peril.
-# It is recommended to regenerate this file in the future when you upgrade to a
-# newer version of cucumber-rails. Consider adding your own code to a new file
-# instead of editing this one. Cucumber will automatically load all features/**/*.rb
-# files.
+# TL;DR: YOU SHOULD DELETE THIS FILE
+#
+# This file was generated by Cucumber-Rails and is only here to get you a head start
+# These step definitions are thin wrappers around the Capybara/Webrat API that lets you
+# visit pages, interact with widgets and make assertions about page content.
+#
+# If you use these step definitions as basis for your features you will quickly end up
+# with features that are:
+#
+# * Hard to maintain
+# * Verbose to read
+#
+# A much better approach is to write your own higher level step definitions, following
+# the advice in the following blog posts:
+#
+# * http://benmabey.com/2008/05/19/imperative-vs-declarative-scenarios-in-user-stories.html
+# * http://dannorth.net/2011/01/31/whose-domain-is-it-anyway/
+# * http://elabs.se/blog/15-you-re-cuking-it-wrong
+#
require 'uri'
require 'cgi'
require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "paths"))
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "selectors"))
module WithinHelpers
def with_scope(locator)
- locator ? within(locator) { yield } : yield
+ locator ? within(*selector_for(locator)) { yield } : yield
end
end
World(WithinHelpers)
+# Single-line step scoper
+When /^(.*) within ([^:]+)$/ do |step, parent|
+ with_scope(parent) { When step }
+end
+
+# Multi-line step scoper
+When /^(.*) within ([^:]+):$/ do |step, parent, table_or_string|
+ with_scope(parent) { When "#{step}:", table_or_string }
+end
+
Given /^(?:|I )am on (.+)$/ do |page_name|
visit path_to(page_name)
end
@@ -24,28 +49,20 @@ def with_scope(locator)
visit path_to(page_name)
end
-When /^(?:|I )press "([^"]*)"(?: within "([^"]*)")?$/ do |button, selector|
- with_scope(selector) do
- click_button(button)
- end
+When /^(?:|I )press "([^"]*)"$/ do |button|
+ click_button(button)
end
-When /^(?:|I )follow "([^"]*)"(?: within "([^"]*)")?$/ do |link, selector|
- with_scope(selector) do
- click_link(link)
- end
+When /^(?:|I )follow "([^"]*)"$/ do |link|
+ click_link(link)
end
-When /^(?:|I )fill in "([^"]*)" with "([^"]*)"(?: within "([^"]*)")?$/ do |field, value, selector|
- with_scope(selector) do
- fill_in(field, :with => value)
- end
+When /^(?:|I )fill in "([^"]*)" with "([^"]*)"$/ do |field, value|
+ fill_in(field, :with => value)
end
-When /^(?:|I )fill in "([^"]*)" for "([^"]*)"(?: within "([^"]*)")?$/ do |value, field, selector|
- with_scope(selector) do
- fill_in(field, :with => value)
- end
+When /^(?:|I )fill in "([^"]*)" for "([^"]*)"$/ do |value, field|
+ fill_in(field, :with => value)
end
# Use this to fill in an entire form with data from a table. Example:
@@ -59,95 +76,70 @@ def with_scope(locator)
# TODO: Add support for checkbox, select og option
# based on naming conventions.
#
-When /^(?:|I )fill in the following(?: within "([^"]*)")?:$/ do |selector, fields|
- with_scope(selector) do
- fields.rows_hash.each do |name, value|
- When %{I fill in "#{name}" with "#{value}"}
- end
- end
-end
-
-When /^(?:|I )select "([^"]*)" from "([^"]*)"(?: within "([^"]*)")?$/ do |value, field, selector|
- with_scope(selector) do
- select(value, :from => field)
+When /^(?:|I )fill in the following:$/ do |fields|
+ fields.rows_hash.each do |name, value|
+ When %{I fill in "#{name}" with "#{value}"}
end
end
-When /^(?:|I )check "([^"]*)"(?: within "([^"]*)")?$/ do |field, selector|
- with_scope(selector) do
- check(field)
- end
+When /^(?:|I )select "([^"]*)" from "([^"]*)"$/ do |value, field|
+ select(value, :from => field)
end
-When /^(?:|I )uncheck "([^"]*)"(?: within "([^"]*)")?$/ do |field, selector|
- with_scope(selector) do
- uncheck(field)
- end
+When /^(?:|I )check "([^"]*)"$/ do |field|
+ check(field)
end
-When /^(?:|I )choose "([^"]*)"(?: within "([^"]*)")?$/ do |field, selector|
- with_scope(selector) do
- choose(field)
- end
+When /^(?:|I )uncheck "([^"]*)"$/ do |field|
+ uncheck(field)
end
-When /^(?:|I )attach the file "([^"]*)" to "([^"]*)"(?: within "([^"]*)")?$/ do |path, field, selector|
- with_scope(selector) do
- attach_file(field, path)
- end
+When /^(?:|I )choose "([^"]*)"$/ do |field|
+ choose(field)
end
-Then /^(?:|I )should see JSON:$/ do |expected_json|
- require 'json'
- expected = JSON.pretty_generate(JSON.parse(expected_json))
- actual = JSON.pretty_generate(JSON.parse(response.body))
- expected.should == actual
+When /^(?:|I )attach the file "([^"]*)" to "([^"]*)"$/ do |path, field|
+ attach_file(field, File.expand_path(path))
end
-Then /^(?:|I )should see "([^"]*)"(?: within "([^"]*)")?$/ do |text, selector|
- with_scope(selector) do
- if page.respond_to? :should
- page.should have_content(text)
- else
- assert page.has_content?(text)
- end
+Then /^(?:|I )should see "([^"]*)"$/ do |text|
+ if page.respond_to? :should
+ page.should have_content(text)
+ else
+ assert page.has_content?(text)
end
end
-Then /^(?:|I )should see \/([^\/]*)\/(?: within "([^"]*)")?$/ do |regexp, selector|
+Then /^(?:|I )should see \/([^\/]*)\/$/ do |regexp|
regexp = Regexp.new(regexp)
- with_scope(selector) do
- if page.respond_to? :should
- page.should have_xpath('//*', :text => regexp)
- else
- assert page.has_xpath?('//*', :text => regexp)
- end
+
+ if page.respond_to? :should
+ page.should have_xpath('//*', :text => regexp)
+ else
+ assert page.has_xpath?('//*', :text => regexp)
end
end
-Then /^(?:|I )should not see "([^"]*)"(?: within "([^"]*)")?$/ do |text, selector|
- with_scope(selector) do
- if page.respond_to? :should
- page.should have_no_content(text)
- else
- assert page.has_no_content?(text)
- end
+Then /^(?:|I )should not see "([^"]*)"$/ do |text|
+ if page.respond_to? :should
+ page.should have_no_content(text)
+ else
+ assert page.has_no_content?(text)
end
end
-Then /^(?:|I )should not see \/([^\/]*)\/(?: within "([^"]*)")?$/ do |regexp, selector|
+Then /^(?:|I )should not see \/([^\/]*)\/$/ do |regexp|
regexp = Regexp.new(regexp)
- with_scope(selector) do
- if page.respond_to? :should
- page.should have_no_xpath('//*', :text => regexp)
- else
- assert page.has_no_xpath?('//*', :text => regexp)
- end
+
+ if page.respond_to? :should
+ page.should have_no_xpath('//*', :text => regexp)
+ else
+ assert page.has_no_xpath?('//*', :text => regexp)
end
end
-Then /^the "([^"]*)" field(?: within "([^"]*)")? should contain "([^"]*)"$/ do |field, selector, value|
- with_scope(selector) do
+Then /^the "([^"]*)" field(?: within (.*))? should contain "([^"]*)"$/ do |field, parent, value|
+ with_scope(parent) do
field = find_field(field)
field_value = (field.tag_name == 'textarea') ? field.text : field.value
if field_value.respond_to? :should
@@ -158,8 +150,8 @@ def with_scope(locator)
end
end
-Then /^the "([^"]*)" field(?: within "([^"]*)")? should not contain "([^"]*)"$/ do |field, selector, value|
- with_scope(selector) do
+Then /^the "([^"]*)" field(?: within (.*))? should not contain "([^"]*)"$/ do |field, parent, value|
+ with_scope(parent) do
field = find_field(field)
field_value = (field.tag_name == 'textarea') ? field.text : field.value
if field_value.respond_to? :should_not
@@ -170,8 +162,8 @@ def with_scope(locator)
end
end
-Then /^the "([^"]*)" checkbox(?: within "([^"]*)")? should be checked$/ do |label, selector|
- with_scope(selector) do
+Then /^the "([^"]*)" checkbox(?: within (.*))? should be checked$/ do |label, parent|
+ with_scope(parent) do
field_checked = find_field(label)['checked']
if field_checked.respond_to? :should
field_checked.should be_true
@@ -181,8 +173,8 @@ def with_scope(locator)
end
end
-Then /^the "([^"]*)" checkbox(?: within "([^"]*)")? should not be checked$/ do |label, selector|
- with_scope(selector) do
+Then /^the "([^"]*)" checkbox(?: within (.*))? should not be checked$/ do |label, parent|
+ with_scope(parent) do
field_checked = find_field(label)['checked']
if field_checked.respond_to? :should
field_checked.should be_false
View
72 features/support/env.rb
@@ -1,27 +1,19 @@
# IMPORTANT: This file is generated by cucumber-rails - edit at your own peril.
-# It is recommended to regenerate this file in the future when you upgrade to a
-# newer version of cucumber-rails. Consider adding your own code to a new file
+# It is recommended to regenerate this file in the future when you upgrade to a
+# newer version of cucumber-rails. Consider adding your own code to a new file
# instead of editing this one. Cucumber will automatically load all features/**/*.rb
# files.
require 'rubygems'
require 'spork'
+require 'capybara'
+require 'capybara/dsl'
+
Spork.prefork do
- ENV["RAILS_ENV"] ||= "test"
- require File.expand_path(File.dirname(__FILE__) + '/../../config/environment')
+ require 'cucumber/rails'
- require 'cucumber/formatter/unicode' # Remove this line if you don't want Cucumber Unicode support
- require 'cucumber/rails/rspec'
- require 'cucumber/rails/world'
- require 'cucumber/rails/active_record'
- require 'cucumber/web/tableish'
-
- require 'capybara/rails'
- require 'capybara/cucumber'
- require 'capybara/session'
- # require 'cucumber/rails/capybara_javascript_emulation' # Lets you click links with onclick javascript handlers without using @culerity or @javascript
# Capybara defaults to XPath selectors rather than Webrat's default of CSS3. In
# order to ease the transition to Capybara we set the default here. If you'd
# prefer to use XPath just remove this line and adjust any selectors in your
@@ -29,39 +21,31 @@
Capybara.default_selector = :css
end
-
+
Spork.each_run do
- # If you set this to false, any error raised from within your app will bubble
- # up to your step definition and out to cucumber unless you catch it somewhere
- # on the way. You can make Rails rescue errors and render error pages on a
- # per-scenario basis by tagging a scenario or feature with the @allow-rescue tag.
+ # By default, any exception happening in your Rails application will bubble up
+ # to Cucumber so that your scenario will fail. This is a different from how
+ # your application behaves in the production environment, where an error page will
+ # be rendered instead.
#
- # If you set this to true, Rails will rescue all errors and render error
- # pages, more or less in the same way your application would behave in the
- # default production environment. It's not recommended to do this for all
- # of your scenarios, as this makes it hard to discover errors in your application.
- ActionController::Base.allow_rescue = false
-
- # If you set this to true, each scenario will run in a database transaction.
- # You can still turn off transactions on a per-scenario basis, simply tagging
- # a feature or scenario with the @no-txn tag. If you are using Capybara,
- # tagging with @culerity or @javascript will also turn transactions off.
+ # Sometimes we want to override this default behaviour and allow Rails to rescue
+ # exceptions and display an error page (just like when the app is running in production).
+ # Typical scenarios where you want to do this is when you test your error pages.
+ # There are two ways to allow Rails to rescue exceptions:
#
- # If you set this to false, transactions will be off for all scenarios,
- # regardless of whether you use @no-txn or not.
+ # 1) Tag your scenario (or feature) with @allow-rescue
#
- # Beware that turning transactions off will leave data in your database
- # after each scenario, which can lead to hard-to-debug failures in
- # subsequent scenarios. If you do this, we recommend you create a Before
- # block that will explicitly put your database in a known state.
- Cucumber::Rails::World.use_transactional_fixtures = true
- # How to clean your database when transactions are turned off. See
- # http://github.com/bmabey/database_cleaner for more info.
- if defined?(ActiveRecord::Base)
- begin
- require 'database_cleaner'
- DatabaseCleaner.strategy = :truncation
- rescue LoadError => ignore_if_database_cleaner_not_present
- end
+ # 2) Set the value below to true. Beware that doing this globally is not
+ # recommended as it will mask a lot of errors for you!
+ #
+ ActionController::Base.allow_rescue = false
+
+ # Remove/comment out the lines below if your app doesn't have a database.
+ # For some databases (like MongoDB and CouchDB) you may need to use :truncation instead.
+ begin
+ DatabaseCleaner.strategy = :transaction
+ rescue NameError
+ raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end
+
end
View
39 features/support/selectors.rb
@@ -0,0 +1,39 @@
+module HtmlSelectorsHelpers
+ # Maps a name to a selector. Used primarily by the
+ #
+ # When /^(.+) within (.+)$/ do |step, scope|
+ #
+ # step definitions in web_steps.rb
+ #
+ def selector_for(locator)
+ case locator
+
+ when /the page/
+ "html > body"
+
+ # Add more mappings here.
+ # Here is an example that pulls values out of the Regexp:
+ #
+ # when /the (notice|error|info) flash/
+ # ".flash.#{$1}"
+
+ # You can also return an array to use a different selector
+ # type, like:
+ #
+ # when /the header/
+ # [:xpath, "//header"]
+
+ # This allows you to provide a quoted selector as the scope
+ # for "within" steps as was previously the default for the
+ # web steps:
+ when /"(.+)"/
+ $1
+
+ else
+ raise "Can't find mapping from \"#{locator}\" to a selector.\n" +
+ "Now, go and add a mapping in #{__FILE__}"
+ end
+ end
+end
+
+World(HtmlSelectorsHelpers)
View
4 lib/tasks/cucumber.rake
@@ -43,6 +43,10 @@ begin
task :features => :cucumber do
STDERR.puts "*** The 'features' task is deprecated. See rake -T cucumber ***"
end
+
+ # In case we don't have ActiveRecord, append a no-op task that we can depend upon.
+ task 'db:test:prepare' do
+ end
rescue LoadError
desc 'cucumber rake task not available (cucumber not installed)'
task :cucumber do
Please sign in to comment.
Something went wrong with that request. Please try again.