spreewald 2.2.4 → 2.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -1
- data/.travis.yml +14 -15
- data/CHANGELOG.md +34 -2
- data/Gemfile +1 -2
- data/Gemfile.lock +1 -16
- data/Gemfile.ruby266 +9 -0
- data/Gemfile.ruby266.lock +65 -0
- data/README.md +76 -34
- data/Rakefile +22 -7
- data/examples/paths.rb +36 -0
- data/examples/selectors.rb +28 -0
- data/lib/spreewald/all_steps.rb +4 -1
- data/lib/spreewald/browser_tab_steps.rb +91 -0
- data/lib/spreewald/development_steps.rb +2 -1
- data/lib/spreewald/email_steps.rb +11 -4
- data/lib/spreewald/file_attachment_steps.rb +3 -21
- data/lib/spreewald/time_steps.rb +102 -0
- data/lib/spreewald/timecop_steps.rb +6 -76
- data/lib/spreewald/web_steps.rb +8 -16
- data/lib/spreewald_support/driver_info.rb +8 -0
- data/lib/spreewald_support/tolerance_for_selenium_sync_issues.rb +9 -3
- data/lib/spreewald_support/version.rb +1 -1
- data/support/step_definition.rb +1 -1
- data/support/step_definition_file.rb +2 -0
- data/tests/rails-3_capybara-1/Gemfile +2 -1
- data/tests/rails-3_capybara-1/Gemfile.lock +3 -1
- data/tests/rails-3_capybara-1/app +1 -1
- data/tests/rails-3_capybara-1/config/cucumber.yml +1 -1
- data/tests/rails-3_capybara-1/config/database.yml +1 -1
- data/tests/rails-3_capybara-1/config/routes.rb +1 -0
- data/tests/rails-3_capybara-1/db +1 -1
- data/tests/rails-3_capybara-1/features/shared +1 -1
- data/tests/rails-3_capybara-1/features/support/paths.rb +1 -1
- data/tests/rails-3_capybara-1/features/support/selectors.rb +1 -1
- data/tests/rails-3_capybara-1/public +1 -1
- data/tests/rails-3_capybara-2/Gemfile +2 -1
- data/tests/rails-3_capybara-2/Gemfile.lock +3 -1
- data/tests/rails-3_capybara-2/Rakefile +1 -1
- data/tests/rails-3_capybara-2/app +1 -1
- data/tests/rails-3_capybara-2/config +1 -1
- data/tests/rails-3_capybara-2/config.ru +1 -1
- data/tests/rails-3_capybara-2/db +1 -1
- data/tests/rails-3_capybara-2/features +1 -1
- data/tests/rails-3_capybara-2/public +1 -1
- data/tests/rails-3_capybara-2/script +1 -1
- data/tests/rails-4_capybara-3/Gemfile +1 -1
- data/tests/rails-4_capybara-3/Gemfile.lock +3 -3
- data/tests/rails-4_capybara-3/app +1 -1
- data/tests/rails-4_capybara-3/config/routes.rb +1 -0
- data/tests/rails-4_capybara-3/db +1 -1
- data/tests/rails-4_capybara-3/features/development_steps.feature +1 -1
- data/tests/rails-4_capybara-3/features/email_steps.feature +1 -1
- data/tests/rails-4_capybara-3/features/overriding.feature +1 -1
- data/tests/rails-4_capybara-3/features/step_definitions/overriding_steps.rb +1 -1
- data/tests/rails-4_capybara-3/features/step_definitions/test_steps.rb +1 -1
- data/tests/rails-4_capybara-3/features/support/paths.rb +1 -1
- data/tests/rails-4_capybara-3/features/support/selectors.rb +1 -1
- data/tests/rails-4_capybara-3/features/table_steps.feature +1 -1
- data/tests/rails-4_capybara-3/features/time_steps.feature +1 -0
- data/tests/rails-4_capybara-3/features/web_steps.feature +1 -1
- data/tests/rails-4_capybara-3/public/fixture_files +1 -1
- data/tests/rails-6_capybara-3/.ruby-version +1 -0
- data/tests/rails-6_capybara-3/Gemfile +25 -0
- data/tests/rails-6_capybara-3/Gemfile.lock +264 -0
- data/tests/rails-6_capybara-3/README.md +24 -0
- data/tests/rails-6_capybara-3/Rakefile +6 -0
- data/tests/rails-6_capybara-3/app/controllers +1 -0
- data/tests/rails-6_capybara-3/app/mailers/application_mailer.rb +4 -0
- data/tests/rails-6_capybara-3/app/mailers/spreewald_mailer.rb +30 -0
- data/tests/rails-6_capybara-3/app/models/application_record.rb +3 -0
- data/tests/rails-6_capybara-3/app/views +1 -0
- data/tests/rails-6_capybara-3/bin/bundle +114 -0
- data/tests/rails-6_capybara-3/bin/rails +4 -0
- data/tests/rails-6_capybara-3/bin/rake +4 -0
- data/tests/rails-6_capybara-3/bin/setup +33 -0
- data/tests/rails-6_capybara-3/config.ru +5 -0
- data/tests/rails-6_capybara-3/config/application.rb +35 -0
- data/tests/rails-6_capybara-3/config/boot.rb +3 -0
- data/tests/rails-6_capybara-3/config/cucumber.yml +9 -0
- data/tests/rails-6_capybara-3/config/database.yml +25 -0
- data/tests/rails-6_capybara-3/config/environment.rb +5 -0
- data/tests/rails-6_capybara-3/config/environments/development.rb +48 -0
- data/tests/rails-6_capybara-3/config/environments/test.rb +45 -0
- data/tests/rails-6_capybara-3/config/initializers/application_controller_renderer.rb +8 -0
- data/tests/rails-6_capybara-3/config/initializers/backtrace_silencers.rb +7 -0
- data/tests/rails-6_capybara-3/config/initializers/content_security_policy.rb +28 -0
- data/tests/rails-6_capybara-3/config/initializers/cookies_serializer.rb +5 -0
- data/tests/rails-6_capybara-3/config/initializers/filter_parameter_logging.rb +4 -0
- data/tests/rails-6_capybara-3/config/initializers/inflections.rb +16 -0
- data/tests/rails-6_capybara-3/config/initializers/mime_types.rb +4 -0
- data/tests/rails-6_capybara-3/config/initializers/wrap_parameters.rb +14 -0
- data/tests/rails-6_capybara-3/config/locales/en.yml +33 -0
- data/tests/rails-6_capybara-3/config/routes.rb +1 -0
- data/tests/rails-6_capybara-3/db +1 -0
- data/tests/rails-6_capybara-3/features/development_steps.feature +1 -0
- data/tests/rails-6_capybara-3/features/email_steps.feature +1 -0
- data/tests/rails-6_capybara-3/features/overriding.feature +1 -0
- data/tests/{shared/app/views/emails/send_email.haml → rails-6_capybara-3/features/step_definitions/.gitkeep} +0 -0
- data/tests/rails-6_capybara-3/features/step_definitions/overriding_steps.rb +1 -0
- data/tests/rails-6_capybara-3/features/step_definitions/test_steps.rb +1 -0
- data/tests/rails-6_capybara-3/features/support/env.rb +66 -0
- data/tests/rails-6_capybara-3/features/support/paths.rb +1 -0
- data/tests/rails-6_capybara-3/features/support/selectors.rb +1 -0
- data/tests/rails-6_capybara-3/features/support/selenium.rb +3 -0
- data/tests/rails-6_capybara-3/features/table_steps.feature +1 -0
- data/tests/rails-6_capybara-3/features/time_steps.feature +1 -0
- data/tests/rails-6_capybara-3/features/web_steps.feature +1 -0
- data/tests/{shared/db/migrate/.gitignore → rails-6_capybara-3/lib/tasks/.keep} +0 -0
- data/tests/rails-6_capybara-3/lib/tasks/cucumber.rake +76 -0
- data/tests/rails-6_capybara-3/log/.keep +0 -0
- data/tests/rails-6_capybara-3/public/404.html +67 -0
- data/tests/rails-6_capybara-3/public/422.html +67 -0
- data/tests/rails-6_capybara-3/public/500.html +66 -0
- data/tests/rails-6_capybara-3/public/favicon.ico +0 -0
- data/tests/rails-6_capybara-3/public/fixture_files +1 -0
- data/tests/rails-6_capybara-3/script/cucumber +11 -0
- data/tests/shared/app/controllers/application_controller.rb +9 -0
- data/tests/shared/app/controllers/authenticated_controller.rb +10 -2
- data/tests/shared/app/controllers/emails_controller.rb +25 -7
- data/tests/shared/app/controllers/forms_controller.rb +3 -0
- data/tests/shared/app/controllers/static_pages_controller.rb +23 -0
- data/tests/shared/app/models/mailer.rb +16 -0
- data/tests/shared/app/views/layouts/mailer.html.erb +13 -0
- data/tests/shared/app/views/layouts/mailer.text.erb +1 -0
- data/tests/shared/app/views/mailer/{email.haml → email.text.erb} +0 -0
- data/tests/shared/app/views/mailer/html_email_with_links.haml +6 -0
- data/tests/shared/app/views/mailer/text_email_with_links.text.erb +6 -0
- data/tests/shared/app/views/spreewald_mailer +1 -0
- data/tests/shared/app/views/static_pages/tab_1.haml +3 -0
- data/tests/shared/app/views/static_pages/tab_2.haml +2 -0
- data/tests/shared/app/views/static_pages/tab_3.haml +1 -0
- data/tests/shared/app/views/static_pages/time.html.haml +2 -0
- data/tests/shared/app/views/static_pages/visibility.html.haml +8 -1
- data/tests/shared/app/views/static_pages/within.html.haml +3 -0
- data/tests/shared/config/routes.rb +37 -0
- data/tests/shared/db/migrate/.keep +0 -0
- data/tests/shared/features/shared/browser_tab_steps.feature +103 -0
- data/tests/shared/features/shared/email_steps.feature +18 -0
- data/tests/shared/features/shared/time_steps.feature +31 -0
- data/tests/shared/features/shared/web_steps.feature +17 -0
- metadata +149 -12
- data/tests/rails-3_capybara-1/config/routes.rb +0 -5
- data/tests/rails-4_capybara-3/.gitignore +0 -17
- data/tests/rails-4_capybara-3/config/routes.rb +0 -3
data/examples/paths.rb
CHANGED
@@ -13,6 +13,30 @@ module NavigationHelpers
|
|
13
13
|
when /^the home\s?page$/
|
14
14
|
root_path
|
15
15
|
|
16
|
+
when /^the (page|form) for the (.*?) above$/
|
17
|
+
action_prose = Regexp.last_match(1)
|
18
|
+
model_prose = Regexp.last_match(2)
|
19
|
+
route = "#{(action_prose == 'form') ? 'edit_' : ''}#{model_prose_to_route_segment(model_prose)}_path"
|
20
|
+
model = model_prose_to_class(model_prose)
|
21
|
+
send(route, model.reorder(:id).last!)
|
22
|
+
|
23
|
+
when /^the (page|form) for the (.*?) "(.*?)"$/
|
24
|
+
action_prose = Regexp.last_match(1)
|
25
|
+
model_prose = Regexp.last_match(2)
|
26
|
+
identifier = Regexp.last_match(3)
|
27
|
+
path_to_show_or_edit(action_prose, model_prose, identifier)
|
28
|
+
|
29
|
+
when /^the (.*?) (page|form) for "(.*?)"$/
|
30
|
+
model_prose = Regexp.last_match(1)
|
31
|
+
action_prose = Regexp.last_match(2)
|
32
|
+
identifier = Regexp.last_match(3)
|
33
|
+
path_to_show_or_edit(action_prose, model_prose, identifier)
|
34
|
+
|
35
|
+
when /^the (.*?) form$/
|
36
|
+
model_prose = Regexp.last_match(1)
|
37
|
+
route = "new_#{model_prose_to_route_segment(model_prose)}_path"
|
38
|
+
send(route)
|
39
|
+
|
16
40
|
else
|
17
41
|
begin
|
18
42
|
page_name =~ /^the (.*) page$/
|
@@ -24,6 +48,18 @@ module NavigationHelpers
|
|
24
48
|
end
|
25
49
|
end
|
26
50
|
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def model_prose_to_class(model_prose)
|
55
|
+
model_prose.gsub(' ', '_').classify.constantize
|
56
|
+
end
|
57
|
+
|
58
|
+
def model_prose_to_route_segment(model_prose)
|
59
|
+
model_prose = model_prose.downcase
|
60
|
+
model_prose.gsub(/[\ \/]/, '_')
|
61
|
+
end
|
62
|
+
|
27
63
|
end
|
28
64
|
|
29
65
|
World(NavigationHelpers)
|
data/examples/selectors.rb
CHANGED
@@ -12,6 +12,27 @@ module HtmlSelectorsHelpers
|
|
12
12
|
when "the page"
|
13
13
|
"html > body"
|
14
14
|
|
15
|
+
# Auto-mapper for BEM classes and ARIA labels
|
16
|
+
#
|
17
|
+
# Usage examples:
|
18
|
+
# the main menu -> '.main-menu, [aria-label="main menu"]'
|
19
|
+
# the item box's header -> '.item-box--header, [aria-label="item box's header"]'
|
20
|
+
# the slider's item that is current -> '.slider--item.is-current, [aria-label="slider's item that is current"]'
|
21
|
+
when /^the (.*)$/
|
22
|
+
match = Regexp.last_match(1)
|
23
|
+
match =~ /^(.+?)(?:'s (.+?))?(?: that (.+))?$/
|
24
|
+
|
25
|
+
bem_selector = '.'
|
26
|
+
bem_selector << selectorify(Regexp.last_match(1))
|
27
|
+
bem_selector << '--' << selectorify(Regexp.last_match(2)) if Regexp.last_match(2)
|
28
|
+
bem_selector << '.' << selectorify(Regexp.last_match(3)) if Regexp.last_match(3)
|
29
|
+
|
30
|
+
aria_selector = '[aria-label="'
|
31
|
+
aria_selector << match.gsub('"', '\\"')
|
32
|
+
aria_selector << '"]'
|
33
|
+
|
34
|
+
[bem_selector, aria_selector].join(', ')
|
35
|
+
|
15
36
|
# Add more mappings here.
|
16
37
|
# Here is an example that pulls values out of the Regexp:
|
17
38
|
#
|
@@ -35,6 +56,13 @@ module HtmlSelectorsHelpers
|
|
35
56
|
"Now, go and add a mapping in #{__FILE__}"
|
36
57
|
end
|
37
58
|
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def selectorify(string)
|
63
|
+
string.gsub(/ /, '-')
|
64
|
+
end
|
65
|
+
|
38
66
|
end
|
39
67
|
|
40
68
|
World(HtmlSelectorsHelpers)
|
data/lib/spreewald/all_steps.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
# coding: UTF-8
|
2
2
|
require 'rspec/matchers'
|
3
3
|
|
4
|
+
ALREADY_LOADED_FILES = %w[all_steps timecop_steps]
|
5
|
+
|
4
6
|
Dir[File.join(File.dirname(__FILE__), '*_steps.rb')].each do |f|
|
5
7
|
name = File.basename(f, '.rb')
|
6
|
-
|
8
|
+
|
9
|
+
unless ALREADY_LOADED_FILES.include?(name)
|
7
10
|
require "spreewald/#{name}"
|
8
11
|
end
|
9
12
|
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
Before do
|
2
|
+
@previous_browser_tabs_count = nil
|
3
|
+
end
|
4
|
+
|
5
|
+
# Close all but the first browser tab
|
6
|
+
After do
|
7
|
+
next unless selenium_driver?
|
8
|
+
|
9
|
+
browser.switch_to.window(browser.window_handles.last)
|
10
|
+
while browser.window_handles.size > 1 do
|
11
|
+
browser.close
|
12
|
+
browser.switch_to.window(browser.window_handles.last)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Opens [the page](https://github.com/makandra/spreewald/blob/master/examples/paths.rb) in a new browser tab and switches to it.
|
17
|
+
When /^I open (.+?) in a new browser tab$/ do |page_name|
|
18
|
+
require_selenium!
|
19
|
+
previous_handles_count = browser.window_handles.size
|
20
|
+
relative_target_path = path_to(page_name)
|
21
|
+
|
22
|
+
page.execute_script "window.open('#{relative_target_path}', '_blank')"
|
23
|
+
step "there should be #{previous_handles_count + 1} browser tabs"
|
24
|
+
step "I switch to the new browser tab"
|
25
|
+
end.overridable
|
26
|
+
|
27
|
+
# Closes the current browser tab and switches back to the first tab.
|
28
|
+
When 'I close the browser tab' do
|
29
|
+
require_selenium!
|
30
|
+
# Wait for browser tab to close
|
31
|
+
previous_handles_count = browser.window_handles.size
|
32
|
+
raise 'Cannot close the last remaining browser tab' if previous_handles_count == 1
|
33
|
+
|
34
|
+
browser.close
|
35
|
+
step "there should be #{previous_handles_count - 1} browser tabs"
|
36
|
+
|
37
|
+
# Closing the current tab causes Selenium to continue using a stale invalid tab handle
|
38
|
+
browser.switch_to.window(browser.window_handles.first)
|
39
|
+
end.overridable
|
40
|
+
|
41
|
+
# Waits for the new browser tab to appear, then switches to it.
|
42
|
+
When /^I switch to the new(?:ly opened)? browser tab$/ do
|
43
|
+
require_selenium!
|
44
|
+
step 'there should be at least 2 browser tabs'
|
45
|
+
browser.switch_to.window(browser.window_handles.last)
|
46
|
+
end.overridable
|
47
|
+
|
48
|
+
# Changes the browser context to the second-last browser tab.
|
49
|
+
When /^I switch(?: back)? to the previous browser tab$/ do
|
50
|
+
require_selenium!
|
51
|
+
previous_handle = browser.window_handles[-2] # Second last should be the previous browser tab
|
52
|
+
previous_handle ||= browser.window_handles.first # Fall back if only one tab is left
|
53
|
+
browser.switch_to.window(previous_handle)
|
54
|
+
end.overridable
|
55
|
+
|
56
|
+
# Required for the check whether a new browser tab was opened or not.
|
57
|
+
When 'I may open a new browser tab' do
|
58
|
+
@previous_browser_tabs_count = browser.window_handles.size
|
59
|
+
end.overridable
|
60
|
+
|
61
|
+
# Example (positive expectation):
|
62
|
+
#
|
63
|
+
# When I may open a new browser tab
|
64
|
+
# And I click on "Open link in new browser tab"
|
65
|
+
# Then I should have opened a new browser tab
|
66
|
+
#
|
67
|
+
# Example (negative expectation):
|
68
|
+
#
|
69
|
+
# When I may open a new browser tab
|
70
|
+
# And I click on "Open link in current browser tab"
|
71
|
+
# Then I should not have opened a new browser tab
|
72
|
+
Then /^I should( not)? have opened a new browser tab$/ do |negate|
|
73
|
+
raise "you need to use the 'I may open a new tab' step beforehand" unless @previous_browser_tabs_count
|
74
|
+
expected_browser_tab_count = negate ? @previous_browser_tabs_count : (@previous_browser_tabs_count + 1)
|
75
|
+
|
76
|
+
step "there should be #{expected_browser_tab_count} browser tabs"
|
77
|
+
end.overridable
|
78
|
+
|
79
|
+
Then /^there should be (\d+) browser tabs?$/ do |expected_browser_tabs|
|
80
|
+
patiently do
|
81
|
+
actual_browser_tabs = browser.window_handles.size
|
82
|
+
expect(actual_browser_tabs).to eq(expected_browser_tabs.to_i), "Expected #{expected_browser_tabs} browser tab to be open, but found #{actual_browser_tabs}"
|
83
|
+
end
|
84
|
+
end.overridable
|
85
|
+
|
86
|
+
Then /^there should be at least (\d+) browser tabs?$/ do |expected_browser_tabs|
|
87
|
+
patiently do
|
88
|
+
actual_browser_tabs = browser.window_handles.size
|
89
|
+
expect(actual_browser_tabs).to be >= (expected_browser_tabs.to_i), "Expected at least #{expected_browser_tabs} browser tab to be open, but found #{actual_browser_tabs}"
|
90
|
+
end
|
91
|
+
end.overridable
|
@@ -7,6 +7,7 @@ end.overridable
|
|
7
7
|
|
8
8
|
# nodoc
|
9
9
|
Then 'debugger' do
|
10
|
+
warn 'The step "Then debugger" will be removed in future versions of Spreewald. Please use "Then console" instead.'
|
10
11
|
step 'console' # Alias
|
11
12
|
end.overridable
|
12
13
|
|
@@ -56,7 +57,7 @@ AfterStep('@slow-motion') do
|
|
56
57
|
sleep 2
|
57
58
|
end
|
58
59
|
|
59
|
-
# Waits for keypress after each step
|
60
|
+
# Waits for a keypress after each step
|
60
61
|
AfterStep('@single-step') do
|
61
62
|
print "Single Stepping. Hit enter to continue"
|
62
63
|
STDIN.getc
|
@@ -68,10 +68,17 @@ end.overridable
|
|
68
68
|
When /^I follow the (first|second|third)? ?link in the e?mail$/ do |index_in_words|
|
69
69
|
mail = @mail || ActionMailer::Base.deliveries.last
|
70
70
|
index = { nil => 0, 'first' => 0, 'second' => 1, 'third' => 2 }[index_in_words]
|
71
|
-
url_pattern = %r
|
72
|
-
|
73
|
-
|
74
|
-
|
71
|
+
url_pattern = %r((?:https?://[^/]+)([^"'\s]+))
|
72
|
+
|
73
|
+
paths = if mail.html_part
|
74
|
+
dom = Nokogiri::HTML(mail.html_part.body.to_s)
|
75
|
+
(dom / 'a[href]').map { |a| a['href'].match(url_pattern)[1] }
|
76
|
+
else
|
77
|
+
mail_body = MailFinder.email_text_body(mail).to_s
|
78
|
+
mail_body.scan(url_pattern).flatten(1)
|
79
|
+
end
|
80
|
+
|
81
|
+
visit paths[index]
|
75
82
|
end.overridable
|
76
83
|
|
77
84
|
Then /^no e?mail should have been sent$/ do
|
@@ -1,27 +1,9 @@
|
|
1
|
-
#
|
2
|
-
#
|
3
|
-
# Example (Company has a `file` attribute):
|
4
|
-
#
|
5
|
-
# Given the file "image.png" was attached to the company above
|
6
|
-
#
|
7
|
-
# You may specify the attribute under which the file is stored …
|
8
|
-
#
|
9
|
-
# Example (Company has a `logo` attribute):
|
10
|
-
#
|
11
|
-
# Given the file "image.png" was attached as logo to the company above
|
12
|
-
#
|
13
|
-
# … or both a container class and its attribute name
|
14
|
-
#
|
15
|
-
# Example (Company has many `Image`s, `Image` has a `file` attribute)
|
16
|
-
#
|
17
|
-
# Given the file "image.png" was attached as Image/file to the company above
|
18
|
-
#
|
19
|
-
# To simultaneously set the `updated_at` timestamp:
|
20
|
-
#
|
21
|
-
# Given the file "some_file" was attached to the profile above at "2011-11-11 11:11"
|
1
|
+
# This step is deprecated and will be removed from spreewald. If you still want to use it, copy the code to your project's own steps.
|
22
2
|
Given /^the file "([^"]*)" was attached(?: as (?:([^"]*)\/)?([^"]*))? to the ([^"]*) above(?: at "([^"]*)")?$/ do
|
23
3
|
|path_to_file, container_name, relation_name, model_name, time_string|
|
24
4
|
|
5
|
+
warn "The step 'the file ... was attached to the ... above' will soon be removed from Spreewald, because we want Spreewald to be a library of steps that interact with a user interface and instead of manipulating the database directly. If you wish to further use this step please copy it over to your project's own steps."
|
6
|
+
|
25
7
|
object = model_name.camelize.constantize.last
|
26
8
|
time = Time.parse(time_string) if time_string.present?
|
27
9
|
relation_name ||= 'file'
|
@@ -0,0 +1,102 @@
|
|
1
|
+
# coding: UTF-8
|
2
|
+
|
3
|
+
|
4
|
+
# Steps to travel through time
|
5
|
+
#
|
6
|
+
# This uses [Timecop](https://github.com/jtrupiano/timecop) or Active Support 4.1+ to stub Time.now / Time.current.
|
7
|
+
# The user is responsible for including one of the two gems.
|
8
|
+
#
|
9
|
+
# Please note that the two approaches branch. While ActiveSupport will freeze the time, Timecop will keep it running.
|
10
|
+
# FILE_COMMENT_END
|
11
|
+
|
12
|
+
major_minor_rails_version = defined?(ActiveSupport) ? [ActiveSupport::VERSION::MAJOR, ActiveSupport::VERSION::MINOR] : [0, 0]
|
13
|
+
is_at_least_rails_4_1 = (major_minor_rails_version <=> [4, 1]) != -1
|
14
|
+
|
15
|
+
if defined?(Timecop) || is_at_least_rails_4_1
|
16
|
+
|
17
|
+
module TimeHelpers
|
18
|
+
|
19
|
+
# When you have to make your rails app time zone aware you have to go 100%
|
20
|
+
# otherwise you are better off ignoring time zones at all.
|
21
|
+
# https://makandracards.com/makandra/8723-guide-to-localizing-a-rails-application
|
22
|
+
|
23
|
+
def use_timezones?
|
24
|
+
active_record_loaded = defined?(ActiveRecord::Base)
|
25
|
+
(!active_record_loaded || ActiveRecord::Base.default_timezone != :local) && Time.zone
|
26
|
+
end
|
27
|
+
|
28
|
+
def parse_time(str)
|
29
|
+
if use_timezones?
|
30
|
+
Time.zone.parse(str)
|
31
|
+
else
|
32
|
+
Time.parse(str)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def current_time
|
37
|
+
if use_timezones?
|
38
|
+
Time.current
|
39
|
+
else
|
40
|
+
Time.now
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
if defined?(Timecop)
|
45
|
+
# Emulate ActiveSupport time helper methods with Timecop - don't rename these methods
|
46
|
+
def travel(duration)
|
47
|
+
Timecop.travel(current_time + duration)
|
48
|
+
end
|
49
|
+
|
50
|
+
def travel_to(date_or_time)
|
51
|
+
Timecop.travel(date_or_time)
|
52
|
+
end
|
53
|
+
|
54
|
+
def travel_back
|
55
|
+
Timecop.return
|
56
|
+
end
|
57
|
+
else
|
58
|
+
require 'active_support/testing/time_helpers'
|
59
|
+
include ActiveSupport::Testing::TimeHelpers
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
World(TimeHelpers)
|
65
|
+
|
66
|
+
# Example:
|
67
|
+
#
|
68
|
+
# Given the date is 2012-02-10
|
69
|
+
# Given the time is 2012-02-10 13:40
|
70
|
+
When /^the (?:date|time) is "?(\d{4}-\d{2}-\d{2}(?: \d{1,2}:\d{2})?)"?$/ do |time|
|
71
|
+
travel_to parse_time(time)
|
72
|
+
end.overridable
|
73
|
+
|
74
|
+
# Example:
|
75
|
+
#
|
76
|
+
# Given the time is 13:40
|
77
|
+
When /^the time is "?(\d{1,2}:\d{2})"?$/ do |time_without_date|
|
78
|
+
travel_to parse_time(time_without_date) # date will be today
|
79
|
+
end.overridable
|
80
|
+
|
81
|
+
# Example:
|
82
|
+
#
|
83
|
+
# When it is 10 minutes later
|
84
|
+
# When it is a few hours earlier
|
85
|
+
When /^it is (\d+|an?|some|a few) (seconds?|minutes?|hours?|days?|weeks?|months?|years?) (later|earlier)$/ do |amount, unit, direction|
|
86
|
+
amount = case amount
|
87
|
+
when 'a', 'an'
|
88
|
+
1
|
89
|
+
when 'some', 'a few'
|
90
|
+
10
|
91
|
+
else
|
92
|
+
amount.to_i
|
93
|
+
end
|
94
|
+
amount = -amount if direction == 'earlier'
|
95
|
+
travel amount.send(unit)
|
96
|
+
end.overridable
|
97
|
+
|
98
|
+
After do
|
99
|
+
travel_back
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
@@ -1,79 +1,9 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
|
4
|
-
# Steps to travel through time using [Timecop](https://github.com/jtrupiano/timecop).
|
5
|
-
#
|
6
|
-
# See [this article](https://makandracards.com/makandra/1222-useful-cucumber-steps-to-travel-through-time-with-timecop) for details.
|
1
|
+
# nodoc
|
7
2
|
# FILE_COMMENT_END
|
8
3
|
|
4
|
+
warn <<-WARNING
|
5
|
+
Warning: The file spreewald/timecop_steps.rb is deprecated. It was moved to
|
6
|
+
spreewald/time_steps.rb. Please require the new file instead.
|
7
|
+
WARNING
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
module TimecopHarness
|
13
|
-
|
14
|
-
# When you have to make your rails app time zone aware you have to go 100%
|
15
|
-
# otherwise you are better off ignoring time zones at all.
|
16
|
-
# https://makandracards.com/makandra/8723-guide-to-localizing-a-rails-application
|
17
|
-
|
18
|
-
def use_timezones?
|
19
|
-
active_record_loaded = defined?(ActiveRecord::Base)
|
20
|
-
(!active_record_loaded || ActiveRecord::Base.default_timezone != :local) && Time.zone
|
21
|
-
end
|
22
|
-
|
23
|
-
def parse_time(str)
|
24
|
-
if use_timezones?
|
25
|
-
Time.zone.parse(str)
|
26
|
-
else
|
27
|
-
Time.parse(str)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def current_time
|
32
|
-
if use_timezones?
|
33
|
-
Time.current
|
34
|
-
else
|
35
|
-
Time.now
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
end
|
40
|
-
|
41
|
-
World(TimecopHarness)
|
42
|
-
|
43
|
-
# Example:
|
44
|
-
#
|
45
|
-
# Given the date is 2012-02-10
|
46
|
-
# Given the time is 2012-02-10 13:40
|
47
|
-
When /^the (?:date|time) is "?(\d{4}-\d{2}-\d{2}(?: \d{1,2}:\d{2})?)"?$/ do |time|
|
48
|
-
Timecop.travel(parse_time(time))
|
49
|
-
end.overridable
|
50
|
-
|
51
|
-
# Example:
|
52
|
-
#
|
53
|
-
# Given the time is 13:40
|
54
|
-
When /^the time is "?(\d{1,2}:\d{2})"?$/ do |time_without_date|
|
55
|
-
Timecop.travel(parse_time(time_without_date)) # date will be today
|
56
|
-
end.overridable
|
57
|
-
|
58
|
-
# Example:
|
59
|
-
#
|
60
|
-
# When it is 10 minutes later
|
61
|
-
# When it is a few hours earlier
|
62
|
-
When /^it is (\d+|a|some|a few) (seconds?|minutes?|hours?|days?|weeks?|months?|years?) (later|earlier)$/ do |amount, unit, direction|
|
63
|
-
amount = case amount
|
64
|
-
when 'a'
|
65
|
-
1
|
66
|
-
when 'some', 'a few'
|
67
|
-
10
|
68
|
-
else
|
69
|
-
amount.to_i
|
70
|
-
end
|
71
|
-
amount = -amount if direction == 'earlier'
|
72
|
-
Timecop.travel(current_time + amount.send(unit))
|
73
|
-
end.overridable
|
74
|
-
|
75
|
-
After do
|
76
|
-
Timecop.return
|
77
|
-
end
|
78
|
-
|
79
|
-
end
|
9
|
+
require 'spreewald/time_steps'
|