cucumber-rails 1.4.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.github/ISSUE_TEMPLATE.md +52 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +42 -0
- data/.gitignore +3 -0
- data/.rspec +4 -1
- data/.rubocop.yml +68 -0
- data/.travis.yml +53 -26
- data/Appraisals +27 -63
- data/{History.md → CHANGELOG.md} +299 -19
- data/CONTRIBUTING.md +18 -27
- data/Gemfile +2 -4
- data/LICENSE +1 -1
- data/README.md +73 -29
- data/Rakefile +18 -14
- data/bin/install_geckodriver.sh +19 -0
- data/bin/install_webpacker.sh +9 -0
- data/config/cucumber.yml +5 -3
- data/cucumber-rails.gemspec +37 -25
- data/dev_tasks/cucumber.rake +3 -3
- data/dev_tasks/rspec.rake +3 -6
- data/dev_tasks/yard/default/layout/html/footer.erb +1 -1
- data/dev_tasks/yard/default/layout/html/layout.erb +5 -5
- data/dev_tasks/yard/default/layout/html/logo.erb +1 -1
- data/dev_tasks/yard/default/layout/html/setup.rb +6 -1
- data/dev_tasks/yard.rake +7 -14
- data/features/allow_rescue.feature +17 -12
- data/features/annotations.feature +20 -0
- data/features/capybara_javascript_drivers.feature +42 -32
- data/features/choose_javascript_database_strategy.feature +38 -57
- data/features/configuration.feature +48 -0
- data/features/database_cleaner.feature +20 -20
- data/features/disable_automatic_database_cleaning.feature +13 -19
- data/features/emulate_javascript.feature +65 -48
- data/features/install_cucumber_rails.feature +6 -5
- data/features/no_database.feature +8 -15
- data/features/raising_errors.feature +10 -4
- data/features/rerun_profile.feature +18 -8
- data/features/rest_api.feature +13 -13
- data/features/step_definitions/cucumber_rails_steps.rb +52 -66
- data/features/support/aruba.rb +5 -0
- data/features/support/cucumber_rails_helper.rb +85 -0
- data/features/support/env.rb +4 -35
- data/features/support/hooks.rb +8 -0
- data/gemfiles/rails_4_2.gemfile +10 -0
- data/gemfiles/rails_5_0.gemfile +10 -0
- data/gemfiles/rails_5_1.gemfile +10 -0
- data/gemfiles/rails_5_2.gemfile +10 -0
- data/gemfiles/rails_6_0.gemfile +9 -0
- data/lib/cucumber/rails/action_dispatch.rb +21 -0
- data/lib/cucumber/rails/application.rb +17 -8
- data/lib/cucumber/rails/capybara/javascript_emulation.rb +47 -39
- data/lib/cucumber/rails/capybara/select_dates_and_times.rb +8 -6
- data/lib/cucumber/rails/capybara.rb +2 -0
- data/lib/cucumber/rails/database.rb +36 -17
- data/lib/cucumber/rails/hooks/active_record.rb +14 -12
- data/lib/cucumber/rails/hooks/allow_rescue.rb +2 -0
- data/lib/cucumber/rails/hooks/database_cleaner.rb +6 -4
- data/lib/cucumber/rails/hooks/mail.rb +4 -4
- data/lib/cucumber/rails/hooks.rb +2 -0
- data/lib/cucumber/rails/rspec.rb +5 -3
- data/lib/cucumber/rails/world.rb +26 -9
- data/lib/cucumber/rails.rb +21 -18
- data/lib/generators/cucumber/{install/USAGE → USAGE} +2 -2
- data/lib/generators/cucumber/{install/install_generator.rb → install_generator.rb} +28 -15
- data/lib/generators/cucumber/{install/templates → templates}/config/cucumber.yml.erb +4 -3
- data/lib/generators/cucumber/{install/templates → templates}/script/cucumber +1 -0
- data/lib/generators/cucumber/{install/templates → templates}/support/_rails_each_run.rb.erb +4 -4
- data/lib/generators/cucumber/{install/templates → templates}/support/_rails_prefork.rb.erb +0 -0
- data/lib/generators/cucumber/{install/templates → templates}/support/capybara.rb +2 -0
- data/lib/generators/cucumber/{install/templates → templates}/support/edit_warning.txt +0 -0
- data/lib/generators/cucumber/{install/templates → templates}/support/rails.rb.erb +0 -0
- data/lib/generators/cucumber/{install/templates → templates}/support/rails_spork.rb.erb +2 -2
- data/lib/generators/cucumber/{install/templates → templates}/tasks/cucumber.rake.erb +19 -8
- data/spec/cucumber/rails/database_spec.rb +46 -33
- data/spec/generators/cucumber/install_generator_spec.rb +55 -0
- data/spec/spec_helper.rb +14 -2
- metadata +228 -142
- data/Gemfile.appraisal +0 -3
- data/features/step_definitions/mongo_steps.rb +0 -3
- data/features/support/bundler_pre_support.rb +0 -28
- data/features/support/fixtures/bundler-1.0.21.gem +0 -0
- data/features/support/fixtures/bundler-1.1.rc.gem +0 -0
- data/features/support/legacy_web_steps_support.rb +0 -290
- data/gemfiles/capybara_1_1.gemfile +0 -17
- data/gemfiles/rails_3_0.gemfile +0 -16
- data/gemfiles/rails_3_1.gemfile +0 -17
- data/gemfiles/rails_3_2.gemfile +0 -17
- data/gemfiles/rails_4_0.gemfile +0 -20
- data/lib/cucumber/rails/action_controller.rb +0 -12
- data/spec/generators/cucumber/install/install_generator_spec.rb +0 -47
@@ -1,16 +1,22 @@
|
|
1
1
|
Feature: Raise Errors
|
2
2
|
|
3
3
|
Scenario: Raise error for undefined route
|
4
|
-
Given I have created a new Rails app
|
5
|
-
|
4
|
+
Given I have created a new Rails app with no database and installed cucumber-rails
|
5
|
+
When I write to "features/home.feature" with:
|
6
6
|
"""
|
7
7
|
Feature: Tests
|
8
8
|
Scenario: Tests
|
9
|
-
When I go to the
|
9
|
+
When I go to the home page
|
10
|
+
"""
|
11
|
+
And I write to "features/home_steps.rb" with:
|
12
|
+
"""
|
13
|
+
When('I go to the home page') do
|
14
|
+
visit('/')
|
15
|
+
end
|
10
16
|
"""
|
11
17
|
And I run `bundle exec cucumber`
|
12
18
|
Then it should fail with:
|
13
19
|
"""
|
14
20
|
1 scenario (1 failed)
|
15
21
|
1 step (1 failed)
|
16
|
-
"""
|
22
|
+
"""
|
@@ -1,4 +1,5 @@
|
|
1
1
|
Feature: Rerun profile
|
2
|
+
|
2
3
|
In order to concentrate on failing features
|
3
4
|
As a Rails developer working with Cucumber
|
4
5
|
I want to rerun only failing features
|
@@ -8,6 +9,7 @@ Feature: Rerun profile
|
|
8
9
|
And a file named "rerun.txt" with:
|
9
10
|
"""
|
10
11
|
features/rerun_test.feature:2
|
12
|
+
features/rerun_test.feature:5
|
11
13
|
"""
|
12
14
|
And a file named "features/rerun_test.feature" with:
|
13
15
|
"""
|
@@ -15,23 +17,31 @@ Feature: Rerun profile
|
|
15
17
|
Scenario: failing before
|
16
18
|
Given fixed now
|
17
19
|
|
20
|
+
Scenario: still failing
|
21
|
+
Given broken
|
22
|
+
|
18
23
|
Scenario: always passing
|
19
24
|
Given passing
|
20
25
|
"""
|
21
26
|
And a file named "features/step_definitions/rerun_steps.rb" with:
|
22
27
|
"""
|
23
|
-
Given
|
24
|
-
puts
|
28
|
+
Given('fixed now') do
|
29
|
+
puts 'All fixed now'
|
30
|
+
end
|
31
|
+
|
32
|
+
Given('broken') do
|
33
|
+
raise "I'm broken"
|
25
34
|
end
|
26
35
|
|
27
|
-
Given
|
36
|
+
Given('passing') do
|
28
37
|
puts "I've always been passing"
|
29
38
|
end
|
30
39
|
"""
|
31
|
-
When I
|
32
|
-
Then it should
|
40
|
+
When I run `bundle exec cucumber -p rerun`
|
41
|
+
Then it should fail with:
|
33
42
|
"""
|
34
|
-
|
35
|
-
|
43
|
+
2 scenarios (1 failed, 1 passed)
|
44
|
+
2 steps (1 failed, 1 passed)
|
36
45
|
"""
|
37
|
-
And the file "rerun.txt" should not contain "features/rerun_test.feature:2"
|
46
|
+
And the file "rerun.txt" should not contain "features/rerun_test.feature:2"
|
47
|
+
And the file "rerun.txt" should contain "features/rerun_test.feature:5"
|
data/features/rest_api.feature
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
Feature: REST API
|
2
2
|
|
3
3
|
Scenario: Compare JSON
|
4
|
-
Given I have created a new Rails app
|
5
|
-
|
4
|
+
Given I have created a new Rails app and installed cucumber-rails
|
5
|
+
When I write to "app/controllers/posts_controller.rb" with:
|
6
6
|
"""
|
7
7
|
class PostsController < ApplicationController
|
8
8
|
def index
|
9
|
-
render :
|
9
|
+
render json: {'hello' => 'world'}.to_json
|
10
10
|
end
|
11
11
|
end
|
12
12
|
"""
|
13
13
|
And I write to "config/routes.rb" with:
|
14
14
|
"""
|
15
|
-
|
15
|
+
TestApp::Application.routes.draw do
|
16
16
|
resources :posts
|
17
17
|
end
|
18
18
|
"""
|
@@ -28,20 +28,20 @@ Feature: REST API
|
|
28
28
|
}
|
29
29
|
\"\"\"
|
30
30
|
"""
|
31
|
-
And I write to "features/step_definitions/
|
31
|
+
And I write to "features/step_definitions/post_steps.rb" with:
|
32
32
|
"""
|
33
|
-
When
|
33
|
+
When('the client requests GET /{word}') do |path|
|
34
34
|
get(path)
|
35
35
|
end
|
36
36
|
|
37
|
-
Then
|
38
|
-
JSON.parse(last_response.body).
|
37
|
+
Then('the response should be JSON:') do |json|
|
38
|
+
expect(JSON.parse(last_response.body)).to eq(JSON.parse(json))
|
39
39
|
end
|
40
40
|
"""
|
41
41
|
And I run `bundle exec rake db:migrate`
|
42
42
|
And I run `bundle exec rake cucumber`
|
43
|
-
Then
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
43
|
+
Then the feature run should pass with:
|
44
|
+
"""
|
45
|
+
1 scenario (1 passed)
|
46
|
+
2 steps (2 passed)
|
47
|
+
"""
|
@@ -1,83 +1,69 @@
|
|
1
|
-
|
2
|
-
def rails_new(options={})
|
3
|
-
options[:name] ||= 'test_app'
|
4
|
-
run_simple "bundle exec rails new #{options[:name]} --skip-test-unit #{options[:args]}"
|
5
|
-
assert_passing_with('README')
|
6
|
-
cd options[:name]
|
7
|
-
end
|
1
|
+
# frozen_string_literal: true
|
8
2
|
|
9
|
-
|
10
|
-
if options.include?(:not_in_test_group)
|
11
|
-
gem "cucumber-rails", :path => "#{File.expand_path('.')}"
|
12
|
-
else
|
13
|
-
gem "cucumber-rails", :group => :test, :require => false, :path => "#{File.expand_path('.')}"
|
14
|
-
end
|
15
|
-
gem "capybara", :group => :test
|
16
|
-
gem "rspec-rails", :group => :test
|
17
|
-
gem "database_cleaner", :group => :test unless options.include?(:no_database_cleaner)
|
18
|
-
gem 'factory_girl', :group => :test unless options.include?(:no_factory_girl)
|
19
|
-
gem "selenium-webdriver", :group => :test
|
20
|
-
run_simple "bundle exec rails generate cucumber:install"
|
21
|
-
end
|
22
|
-
|
23
|
-
def gem(name, options)
|
24
|
-
line = %{gem "#{name}", #{options.inspect}\n}
|
25
|
-
append_to_file('Gemfile', line)
|
26
|
-
end
|
27
|
-
|
28
|
-
def prepare_aruba_report
|
29
|
-
if(ENV['ARUBA_REPORT_DIR'])
|
30
|
-
@aruba_report_start = Time.new
|
31
|
-
sleep(1)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def fixture(path)
|
36
|
-
File.expand_path(File.dirname(__FILE__) + "./../support/fixtures/#{path}")
|
37
|
-
end
|
38
|
-
end
|
39
|
-
World(CucumberRailsHelper)
|
40
|
-
|
41
|
-
Given /^I have created a new Rails app and installed cucumber\-rails, accidentally outside of the test group in my Gemfile$/ do
|
3
|
+
Given('I have created a new Rails app and installed cucumber-rails, accidentally outside of the test group in my Gemfile') do
|
42
4
|
rails_new
|
43
5
|
install_cucumber_rails :not_in_test_group
|
44
|
-
create_web_steps
|
45
|
-
prepare_aruba_report
|
46
|
-
end
|
47
|
-
|
48
|
-
Given /^I have created a new Rails app "([^"]*)" and installed cucumber\-rails$/ do |app_name|
|
49
|
-
rails_new :name => app_name
|
50
|
-
install_cucumber_rails
|
51
|
-
create_web_steps
|
52
|
-
prepare_aruba_report
|
53
6
|
end
|
54
7
|
|
55
|
-
Given
|
8
|
+
Given('I have created a new Rails app and installed cucumber-rails') do
|
56
9
|
rails_new
|
57
10
|
install_cucumber_rails
|
58
|
-
create_web_steps
|
59
|
-
prepare_aruba_report
|
60
11
|
end
|
61
12
|
|
62
|
-
Given
|
63
|
-
rails_new :
|
64
|
-
install_cucumber_rails :no_database_cleaner, :
|
13
|
+
Given('I have created a new Rails app with no database and installed cucumber-rails') do
|
14
|
+
rails_new args: '--skip-active-record'
|
15
|
+
install_cucumber_rails :no_database_cleaner, :no_factory_bot
|
65
16
|
overwrite_file('features/support/env.rb', "require 'cucumber/rails'\n")
|
66
|
-
create_web_steps
|
67
17
|
end
|
68
18
|
|
69
|
-
Given
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
19
|
+
Given('I have a {string} ActiveRecord model object') do |name|
|
20
|
+
run_command_and_stop("bundle exec rails g model #{name}")
|
21
|
+
run_command_and_stop('bundle exec rake db:migrate RAILS_ENV=test')
|
22
|
+
end
|
23
|
+
|
24
|
+
Given('I remove the {string} gem from the Gemfile') do |gem_name|
|
25
|
+
content = File.open(expand_path('Gemfile'), 'r').readlines
|
26
|
+
new_content = []
|
27
|
+
|
28
|
+
content.each do |line|
|
29
|
+
next if /gem ["|']#{gem_name}["|'].*/.match?(line)
|
30
|
+
|
31
|
+
new_content << line
|
32
|
+
end
|
33
|
+
|
34
|
+
overwrite_file('Gemfile', new_content.join("\r\n"))
|
35
|
+
end
|
36
|
+
|
37
|
+
Given('I force selenium to run Firefox in headless mode') do
|
38
|
+
selenium_config = %{
|
39
|
+
Capybara.register_driver :selenium do |app|
|
40
|
+
http_client = Selenium::WebDriver::Remote::Http::Default.new
|
41
|
+
http_client.read_timeout = 180
|
42
|
+
|
43
|
+
browser_options = Selenium::WebDriver::Firefox::Options.new
|
44
|
+
browser_options.args << '--headless'
|
45
|
+
Capybara::Selenium::Driver.new(
|
46
|
+
app,
|
47
|
+
browser: :firefox,
|
48
|
+
options: browser_options,
|
49
|
+
http_client: http_client
|
50
|
+
)
|
51
|
+
end
|
52
|
+
|
53
|
+
Capybara.server = :webrick
|
54
|
+
}
|
55
|
+
|
56
|
+
step 'I append to "features/support/env.rb" with:', selenium_config
|
74
57
|
end
|
75
58
|
|
76
|
-
|
77
|
-
|
78
|
-
run_simple("bundle exec rake db:migrate RAILS_ENV=test")
|
59
|
+
When('I run the cukes') do
|
60
|
+
run_command_and_stop('bundle exec cucumber')
|
79
61
|
end
|
80
62
|
|
81
|
-
|
82
|
-
|
63
|
+
# Copied from Aruba
|
64
|
+
Then('the feature run should pass with:') do |string|
|
65
|
+
step 'the output should not contain " failed)"'
|
66
|
+
step 'the output should not contain " undefined)"'
|
67
|
+
step 'the exit status should be 0'
|
68
|
+
step 'the output should contain:', string
|
83
69
|
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CucumberRailsHelper
|
4
|
+
def rails_new(options = {})
|
5
|
+
validate_rails_new_success(run_rails_new_command(options))
|
6
|
+
clear_bundle_env_vars(options[:name])
|
7
|
+
end
|
8
|
+
|
9
|
+
def install_cucumber_rails(*options)
|
10
|
+
add_conditional_gems(options)
|
11
|
+
|
12
|
+
add_gem 'capybara', group: :test
|
13
|
+
add_gem 'selenium-webdriver', '~> 3.11', group: :test
|
14
|
+
add_gem 'rspec-expectations', '~> 3.7', group: :test
|
15
|
+
add_gem 'database_cleaner', '>= 1.8.0', group: :test unless options.include?(:no_database_cleaner)
|
16
|
+
add_gem 'factory_bot', '>= 3.2', group: :test unless options.include?(:no_factory_bot)
|
17
|
+
|
18
|
+
run_command_and_stop 'bundle install'
|
19
|
+
run_command_and_stop 'bundle exec rails webpacker:install' if rails6?
|
20
|
+
run_command_and_stop 'bundle exec rails generate cucumber:install'
|
21
|
+
end
|
22
|
+
|
23
|
+
def add_gem(name, *args)
|
24
|
+
line = convert_gem_opts_to_string(name, *args)
|
25
|
+
gem_regexp = /gem ["']#{name}["'].*$/
|
26
|
+
gemfile_content = File.read(expand_path('Gemfile'))
|
27
|
+
|
28
|
+
if gemfile_content&.match?(gem_regexp)
|
29
|
+
updated_gemfile_content = gemfile_content.gsub(gem_regexp, line)
|
30
|
+
overwrite_file('Gemfile', updated_gemfile_content)
|
31
|
+
else
|
32
|
+
append_to_file('Gemfile', line)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def run_rails_new_command(options)
|
39
|
+
options[:name] ||= 'test_app'
|
40
|
+
flags = '--skip-bundle --skip-test-unit --skip-spring --skip-bootsnap'
|
41
|
+
flags += ' --skip-webpack-install' if rails6?
|
42
|
+
run_command "bundle exec rails new #{options[:name]} #{flags} #{options[:args]}"
|
43
|
+
end
|
44
|
+
|
45
|
+
def validate_rails_new_success(result)
|
46
|
+
expect(result).to have_output(/README/)
|
47
|
+
expect(last_command_started).to be_successfully_executed
|
48
|
+
end
|
49
|
+
|
50
|
+
def clear_bundle_env_vars(dir)
|
51
|
+
cd dir
|
52
|
+
delete_environment_variable 'RUBYOPT'
|
53
|
+
delete_environment_variable 'BUNDLE_BIN_PATH'
|
54
|
+
delete_environment_variable 'BUNDLE_GEMFILE'
|
55
|
+
end
|
56
|
+
|
57
|
+
def rails6?
|
58
|
+
`bundle exec rails -v`.start_with?('Rails 6')
|
59
|
+
end
|
60
|
+
|
61
|
+
def add_conditional_gems(options)
|
62
|
+
if options.include?(:not_in_test_group)
|
63
|
+
add_gem 'cucumber-rails', path: File.expand_path('.').to_s
|
64
|
+
else
|
65
|
+
add_gem 'cucumber-rails', group: :test, require: false, path: File.expand_path('.').to_s
|
66
|
+
end
|
67
|
+
|
68
|
+
if rails6?
|
69
|
+
add_gem 'sqlite3', '~> 1.4'
|
70
|
+
else
|
71
|
+
add_gem 'sqlite3', '~> 1.3.13'
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def convert_gem_opts_to_string(name, *args)
|
76
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
77
|
+
parts = ["'#{name}'"]
|
78
|
+
parts << args.map(&:inspect) if args.any?
|
79
|
+
parts << options.inspect[1..-2] if options.any?
|
80
|
+
new_parts = parts.flatten.map { |part| part.gsub(/:(\w+)=>/, '\1: ') }
|
81
|
+
"gem #{new_parts.join(', ')}\n"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
World(CucumberRailsHelper)
|
data/features/support/env.rb
CHANGED
@@ -1,37 +1,6 @@
|
|
1
|
-
|
2
|
-
require 'rubygems'
|
3
|
-
require 'bundler/setup'
|
4
|
-
require 'rspec/expectations'
|
5
|
-
require 'aruba/cucumber'
|
6
|
-
|
7
|
-
Before do
|
8
|
-
@aruba_timeout_seconds = 120 # A long time needed some times
|
9
|
-
unset_bundler_env_vars
|
10
|
-
end
|
1
|
+
# frozen_string_literal: true
|
11
2
|
|
12
|
-
|
13
|
-
# Override reporting behaviour so we don't document all files, only the ones
|
14
|
-
# that have been created after @aruba_report_start (a Time object). This is
|
15
|
-
# given a value after the Rails app is generated (see cucumber_rails_steps.rb)
|
16
|
-
module Aruba
|
17
|
-
module Reporting
|
18
|
-
def children(dir)
|
19
|
-
children = Dir["#{dir}/*"].sort
|
20
|
-
|
21
|
-
# include
|
22
|
-
children = children.select do |child|
|
23
|
-
File.directory?(child) ||
|
24
|
-
(@aruba_report_start && File.stat(child).mtime > @aruba_report_start)
|
25
|
-
end
|
26
|
-
|
27
|
-
# exclude
|
28
|
-
children = children.reject do |child|
|
29
|
-
child =~ /Gemfile/ ||
|
30
|
-
child =~ /\.log$/
|
31
|
-
end
|
3
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
|
32
4
|
|
33
|
-
|
34
|
-
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
5
|
+
require 'rspec/expectations'
|
6
|
+
require 'aruba/cucumber'
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
ActionController::Base.class_eval do
|
4
|
+
cattr_accessor :allow_rescue
|
5
|
+
end
|
6
|
+
|
7
|
+
module Cucumber
|
8
|
+
module Rails
|
9
|
+
module ActionDispatch
|
10
|
+
module ShowExceptions
|
11
|
+
def call(env)
|
12
|
+
env['action_dispatch.show_detailed_exceptions'] = !ActionController::Base.allow_rescue
|
13
|
+
env['action_dispatch.show_exceptions'] = !env['action_dispatch.show_detailed_exceptions']
|
14
|
+
super(env)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
ActionDispatch::ShowExceptions.prepend(Cucumber::Rails::ActionDispatch::ShowExceptions)
|
@@ -1,17 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'rails/application'
|
2
4
|
|
3
5
|
# Make sure the ActionDispatch::ShowExceptions middleware is always enabled,
|
4
6
|
# regardless of what is in config/environments/test.rb
|
5
7
|
# Instead we are overriding ActionDispatch::ShowExceptions to be able to
|
6
8
|
# toggle whether or not exceptions are raised.
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
|
10
|
+
module Cucumber
|
11
|
+
module Rails
|
12
|
+
module Application
|
13
|
+
def initialize!
|
14
|
+
ad = config.action_dispatch
|
15
|
+
|
16
|
+
def ad.show_exceptions
|
17
|
+
true
|
18
|
+
end
|
19
|
+
|
20
|
+
super
|
21
|
+
end
|
14
22
|
end
|
15
|
-
__cucumber_orig_initialize__
|
16
23
|
end
|
17
24
|
end
|
25
|
+
|
26
|
+
Rails::Application.prepend(Cucumber::Rails::Application)
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Cucumber
|
2
4
|
module Rails
|
3
5
|
module Capybara
|
@@ -8,10 +10,12 @@ module Cucumber
|
|
8
10
|
alias_method :click, :click_with_javascript_emulation
|
9
11
|
end
|
10
12
|
end
|
11
|
-
|
12
|
-
def click_with_javascript_emulation
|
13
|
+
|
14
|
+
def click_with_javascript_emulation(*)
|
13
15
|
if link_with_non_get_http_method?
|
14
|
-
::Capybara::RackTest::Form.new(
|
16
|
+
::Capybara::RackTest::Form.new(
|
17
|
+
driver, js_form(element_node.document, self[:href], emulated_method)
|
18
|
+
).submit(self)
|
15
19
|
else
|
16
20
|
click_without_javascript_emulation
|
17
21
|
end
|
@@ -44,61 +48,65 @@ module Cucumber
|
|
44
48
|
js_form['action'] = action
|
45
49
|
js_form['method'] = method
|
46
50
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
input['value'] = emulated_method
|
52
|
-
js_form.add_child(input)
|
53
|
-
end
|
51
|
+
add_hidden_method_input(document, js_form) unless same?(emulated_method, method)
|
52
|
+
|
53
|
+
# rails will wipe the session if the CSRF token is not sent with non-GET requests
|
54
|
+
add_hidden_csrf_input(document, js_form) if csrf? && !get?(emulated_method)
|
54
55
|
|
55
|
-
# rails will wipe the session if the CSRF token is not sent
|
56
|
-
# with non-GET requests
|
57
|
-
if csrf? && emulated_method.downcase != "get"
|
58
|
-
input = document.create_element('input')
|
59
|
-
input['type'] = 'hidden'
|
60
|
-
input['name'] = csrf_param
|
61
|
-
input['value'] = csrf_token
|
62
|
-
js_form.add_child(input)
|
63
|
-
end
|
64
|
-
|
65
56
|
js_form
|
66
57
|
end
|
67
58
|
|
59
|
+
def same?(emulated_method, method)
|
60
|
+
emulated_method.casecmp(method).zero?
|
61
|
+
end
|
62
|
+
|
63
|
+
def add_hidden_method_input(document, js_form)
|
64
|
+
input = document.create_element('input')
|
65
|
+
input['type'] = 'hidden'
|
66
|
+
input['name'] = '_method'
|
67
|
+
input['value'] = emulated_method
|
68
|
+
js_form.add_child(input)
|
69
|
+
end
|
70
|
+
|
71
|
+
def get?(emulated_method)
|
72
|
+
same?(emulated_method, 'get')
|
73
|
+
end
|
74
|
+
|
75
|
+
def add_hidden_csrf_input(document, js_form)
|
76
|
+
input = document.create_element('input')
|
77
|
+
input['type'] = 'hidden'
|
78
|
+
input['name'] = csrf_param
|
79
|
+
input['value'] = csrf_token
|
80
|
+
js_form.add_child(input)
|
81
|
+
end
|
82
|
+
|
68
83
|
def link_with_non_get_http_method?
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
tag_name == 'a' && element_node['onclick'] && element_node['onclick'] =~ /var f = document\.createElement\('form'\); f\.style\.display = 'none';/
|
73
|
-
end
|
84
|
+
tag_name == 'a' &&
|
85
|
+
element_node['data-method'] &&
|
86
|
+
element_node['data-method'] =~ /(?:delete|put|post)/
|
74
87
|
end
|
75
88
|
|
76
89
|
def emulated_method
|
77
|
-
|
78
|
-
element_node['data-method']
|
79
|
-
else
|
80
|
-
element_node['onclick'][/m\.setAttribute\('value', '([^']*)'\)/, 1]
|
81
|
-
end
|
90
|
+
element_node['data-method']
|
82
91
|
end
|
83
92
|
|
84
93
|
def element_node
|
85
|
-
|
86
|
-
self.native
|
87
|
-
else
|
88
|
-
warn "DEPRECATED: cucumber-rails loves you, just not your version of Capybara. Please update Capybara to >= 0.4.0"
|
89
|
-
self.node
|
90
|
-
end
|
94
|
+
native
|
91
95
|
end
|
92
96
|
end
|
93
97
|
end
|
94
98
|
end
|
95
99
|
end
|
96
100
|
|
97
|
-
|
98
|
-
|
101
|
+
module Capybara
|
102
|
+
module RackTest
|
103
|
+
class Node
|
104
|
+
include ::Cucumber::Rails::Capybara::JavascriptEmulation
|
105
|
+
end
|
106
|
+
end
|
99
107
|
end
|
100
108
|
|
101
|
-
Before('
|
109
|
+
Before('not @no-js-emulation') do
|
102
110
|
# Enable javascript emulation
|
103
111
|
::Capybara::RackTest::Node.class_eval do
|
104
112
|
alias_method :click, :click_with_javascript_emulation
|