From eabc239dc2daa234ab6884f0edae6e032fc689d7 Mon Sep 17 00:00:00 2001 From: szpyxlwoni Date: Thu, 15 Nov 2012 10:17:16 +0800 Subject: [PATCH] add cucumber test for main process --- Gemfile | 6 ++- Gemfile.lock | 38 ++++++++++++- config/cucumber.yml | 8 +++ config/database.yml | 6 ++- features/main_process.feature | 9 ++++ features/step_definitions/main_process.rb | 11 ++++ features/support/env.rb | 61 +++++++++++++++++++++ lib/tasks/cucumber.rake | 65 +++++++++++++++++++++++ script/cucumber | 10 ++++ 9 files changed, 210 insertions(+), 4 deletions(-) create mode 100644 config/cucumber.yml create mode 100644 features/main_process.feature create mode 100644 features/step_definitions/main_process.rb create mode 100644 features/support/env.rb create mode 100644 lib/tasks/cucumber.rake create mode 100755 script/cucumber diff --git a/Gemfile b/Gemfile index 18fa95b..61bd01b 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ -source 'https://rubygems.org' +source 'http://ruby.taobao.org' gem 'rails', '3.2.8' gem 'slim' @@ -28,4 +28,8 @@ group :development, :test do gem 'rspec-rails' gem 'simplecov' gem 'machinist' + gem 'cucumber-rails' + gem 'capybara' + gem 'database_cleaner' + gem 'selenium-webdriver' end diff --git a/Gemfile.lock b/Gemfile.lock index fdd27e3..0b1444e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,5 +1,5 @@ GEM - remote: https://rubygems.org/ + remote: http://ruby.taobao.org/ specs: actionmailer (3.2.8) actionpack (= 3.2.8) @@ -41,6 +41,15 @@ GEM nokogiri (>= 1.4.4) uuidtools (~> 2.1) builder (3.0.0) + capybara (2.0.0) + mime-types (>= 1.16) + nokogiri (>= 1.3.3) + rack (>= 1.0.0) + rack-test (>= 0.5.4) + selenium-webdriver (~> 2.0) + xpath (~> 1.0.0) + childprocess (0.3.6) + ffi (~> 1.0, >= 1.0.6) chronic (0.7.0) cocaine (0.3.0) code_analyzer (0.3.0) @@ -53,6 +62,16 @@ GEM execjs coffee-script-source (1.3.3) colored (1.2) + cucumber (1.2.1) + builder (>= 2.1.2) + diff-lcs (>= 1.1.3) + gherkin (~> 2.11.0) + json (>= 1.4.6) + cucumber-rails (1.3.0) + capybara (>= 1.1.2) + cucumber (>= 1.1.8) + nokogiri (>= 1.5.0) + database_cleaner (0.9.1) diff-lcs (1.1.3) erubis (2.7.0) execjs (1.4.0) @@ -62,9 +81,12 @@ GEM i18n (~> 0.4) faraday (0.8.4) multipart-post (~> 1.1) + ffi (1.1.5) flay (2.0.0) ruby_parser (~> 3.0.0) sexp_processor (~> 4.0) + gherkin (2.11.5) + json (>= 1.4.6) github (0.1.1) google-api-client (0.5.0) addressable (>= 2.3.2) @@ -93,6 +115,8 @@ GEM launchy (2.1.2) addressable (~> 2.3) libv8 (3.3.10.4) + libwebsocket (0.1.5) + addressable machinist (2.0) mail (2.4.4) i18n (>= 0.4.0) @@ -160,11 +184,17 @@ GEM rspec (~> 2.11.0) ruby_parser (3.0.1) sexp_processor (~> 4.1) + rubyzip (0.9.9) sass (3.2.1) sass-rails (3.2.5) railties (~> 3.2.0) sass (>= 3.1.10) tilt (~> 1.3) + selenium-webdriver (2.26.0) + childprocess (>= 0.2.5) + libwebsocket (~> 0.1.3) + multi_json (~> 1.0) + rubyzip sexp_processor (4.1.2) signet (0.4.3) addressable (>= 2.2.3) @@ -204,14 +234,19 @@ GEM execjs (>= 0.3.0) multi_json (~> 1.0, >= 1.0.2) uuidtools (2.1.3) + xpath (1.0.0) + nokogiri (~> 1.3) PLATFORMS ruby DEPENDENCIES aws-sdk + capybara chronic coffee-rails (~> 3.2.1) + cucumber-rails + database_cleaner execjs faker flay @@ -226,6 +261,7 @@ DEPENDENCIES rails_best_practices (~> 1.11.1) rspec-rails sass-rails (~> 3.2.3) + selenium-webdriver simplecov slim tddium diff --git a/config/cucumber.yml b/config/cucumber.yml new file mode 100644 index 0000000..19b288d --- /dev/null +++ b/config/cucumber.yml @@ -0,0 +1,8 @@ +<% +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'] || 'pretty'} --strict --tags ~@wip" +%> +default: <%= std_opts %> features +wip: --tags @wip:3 --wip features +rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip diff --git a/config/database.yml b/config/database.yml index b1faa4d..4f0fc8b 100644 --- a/config/database.yml +++ b/config/database.yml @@ -18,10 +18,12 @@ development: pool: 5 timeout: 5000 -test: +test: &test adapter: postgresql database: ocelots_test user: ocelots host: localhost pool: 5 - timeout: 5000 \ No newline at end of file + timeout: 5000 +cucumber: + <<: *test \ No newline at end of file diff --git a/features/main_process.feature b/features/main_process.feature new file mode 100644 index 0000000..733bb5b --- /dev/null +++ b/features/main_process.feature @@ -0,0 +1,9 @@ +Feature: Main process of Ocelots + AS A normal user + I WANT TO use main functionality of Ocelots + SO THAT I can know my team members better + +Scenario: Go to home page + Given I am not signed in + When I go to home page + Then I should see sign in buttons diff --git a/features/step_definitions/main_process.rb b/features/step_definitions/main_process.rb new file mode 100644 index 0000000..fb02f1b --- /dev/null +++ b/features/step_definitions/main_process.rb @@ -0,0 +1,11 @@ +Given /^I am not signed in$/ do + +end + +When /^I go to home page$/ do + visit '/' +end + +Then /^I should see sign in buttons$/ do + # TODO: assertion here +end diff --git a/features/support/env.rb b/features/support/env.rb new file mode 100644 index 0000000..e34641a --- /dev/null +++ b/features/support/env.rb @@ -0,0 +1,61 @@ +# 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. + +require 'cucumber/rails' + +# 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 +# steps to use the XPath syntax. +Capybara.default_selector = :css + +# 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. +# +# 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: +# +# 1) Tag your scenario (or feature) with @allow-rescue +# +# 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 + +# You may also want to configure DatabaseCleaner to use different strategies for certain features and scenarios. +# See the DatabaseCleaner documentation for details. Example: +# +# Before('@no-txn,@selenium,@culerity,@celerity,@javascript') do +# # { :except => [:widgets] } may not do what you expect here +# # as tCucumber::Rails::Database.javascript_strategy overrides +# # this setting. +# DatabaseCleaner.strategy = :truncation +# end +# +# Before('~@no-txn', '~@selenium', '~@culerity', '~@celerity', '~@javascript') do +# DatabaseCleaner.strategy = :transaction +# end +# + +# Possible values are :truncation and :transaction +# The :transaction strategy is faster, but might give you threading problems. +# See https://github.com/cucumber/cucumber-rails/blob/master/features/choose_javascript_database_strategy.feature +Cucumber::Rails::Database.javascript_strategy = :truncation + +Capybara.app_host = 'http://localhost:3000' +Capybara.default_driver = :selenium diff --git a/lib/tasks/cucumber.rake b/lib/tasks/cucumber.rake new file mode 100644 index 0000000..83f7947 --- /dev/null +++ b/lib/tasks/cucumber.rake @@ -0,0 +1,65 @@ +# 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. + + +unless ARGV.any? {|a| a =~ /^gems/} # Don't load anything when running the gems:* tasks + +vendored_cucumber_bin = Dir["#{Rails.root}/vendor/{gems,plugins}/cucumber*/bin/cucumber"].first +$LOAD_PATH.unshift(File.dirname(vendored_cucumber_bin) + '/../lib') unless vendored_cucumber_bin.nil? + +begin + require 'cucumber/rake/task' + + namespace :cucumber do + Cucumber::Rake::Task.new({:ok => 'db:test:prepare'}, 'Run features that should pass') do |t| + t.binary = vendored_cucumber_bin # If nil, the gem's binary is used. + t.fork = true # You may get faster startup if you set this to false + t.profile = 'default' + end + + Cucumber::Rake::Task.new({:wip => 'db:test:prepare'}, 'Run features that are being worked on') do |t| + t.binary = vendored_cucumber_bin + t.fork = true # You may get faster startup if you set this to false + t.profile = 'wip' + end + + Cucumber::Rake::Task.new({:rerun => 'db:test:prepare'}, 'Record failing features and run only them if any exist') do |t| + t.binary = vendored_cucumber_bin + t.fork = true # You may get faster startup if you set this to false + t.profile = 'rerun' + end + + desc 'Run all features' + task :all => [:ok, :wip] + + task :statsetup do + require 'rails/code_statistics' + ::STATS_DIRECTORIES << %w(Cucumber\ features features) if File.exist?('features') + ::CodeStatistics::TEST_TYPES << "Cucumber features" if File.exist?('features') + end + end + desc 'Alias for cucumber:ok' + task :cucumber => 'cucumber:ok' + + task :default => :cucumber + + 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 + + task :stats => 'cucumber:statsetup' +rescue LoadError + desc 'cucumber rake task not available (cucumber not installed)' + task :cucumber do + abort 'Cucumber rake task is not available. Be sure to install cucumber as a gem or plugin' + end +end + +end diff --git a/script/cucumber b/script/cucumber new file mode 100755 index 0000000..7fa5c92 --- /dev/null +++ b/script/cucumber @@ -0,0 +1,10 @@ +#!/usr/bin/env ruby + +vendored_cucumber_bin = Dir["#{File.dirname(__FILE__)}/../vendor/{gems,plugins}/cucumber*/bin/cucumber"].first +if vendored_cucumber_bin + load File.expand_path(vendored_cucumber_bin) +else + require 'rubygems' unless ENV['NO_RUBYGEMS'] + require 'cucumber' + load Cucumber::BINARY +end