acceptance_test2 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/lib/acceptance_test/version')
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "acceptance_test2"
7
+ spec.summary = %q{Simplifies congiguration and run of acceptance tests.}
8
+ spec.description = %q{Description: simplifies congiguration and run of acceptance tests.}
9
+ spec.email = "alexander.shvets@gmail.com"
10
+ spec.authors = ["Alexander Shvets"]
11
+ spec.homepage = "http://github.com/shvets/acceptance_test2"
12
+
13
+ spec.files = `git ls-files`.split($\)
14
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.require_paths = ["lib"]
16
+ spec.version = AcceptanceTest::VERSION
17
+ spec.license = "MIT"
18
+
19
+ <%= project_dependencies %>
20
+
21
+ end
@@ -0,0 +1,221 @@
1
+ require 'singleton'
2
+ require 'capybara'
3
+
4
+ class CapybaraHelper
5
+ include Singleton
6
+
7
+ DEFAULT_DRIVER = :selenium
8
+ DEFAULT_BROWSER = :chrome
9
+ DEFAULT_WAIT_TIME = 2
10
+
11
+ attr_reader :headless_mode, :video_mode
12
+
13
+ def initialize
14
+ Capybara.run_server = false
15
+
16
+ if ENV['HEADLESS']
17
+ begin
18
+ require 'headless'
19
+
20
+ @headless_mode = true
21
+ rescue LoadError
22
+ @headless_mode = false
23
+
24
+ puts "Headless mode is not supported on this OS."
25
+ end
26
+ end
27
+
28
+ @video_mode = ENV['VIDEO'] ? true : false
29
+
30
+ @parallel_tests_mode = ENV['TEST_ENV_NUMBER'] ? true : false
31
+ end
32
+
33
+ def before_test(app_host:, driver: DEFAULT_DRIVER, browser: DEFAULT_BROWSER, wait_time: DEFAULT_WAIT_TIME)
34
+ @old_driver = Capybara.current_driver
35
+
36
+ driver_name = register_driver driver: driver, browser: browser, selenium_url: nil, capabilities: nil
37
+
38
+ use_driver(driver_name)
39
+
40
+ # driver_manager.setup_browser_binary config[:browser].to_sym, config[:browser_binaries]
41
+
42
+ Capybara.app_host = app_host
43
+
44
+ @old_default_max_wait_time = Capybara.default_max_wait_time
45
+
46
+ Capybara.configure do |conf|
47
+ conf.default_max_wait_time = wait_time
48
+
49
+ # conf.match = :first
50
+ #
51
+ # conf.ignore_hidden_elements = false
52
+ end
53
+
54
+ if headless_mode
55
+ headless_params = {}
56
+ #headless_params[:display] = @parallel_tests_mode ? 100 + test_number : 1
57
+ headless_params[:reuse] = @parallel_tests_mode ? true : false
58
+ headless_params[:dimensions] = "1280x900x24"
59
+
60
+ headless_params[:video] = {
61
+ frame_rate: 12,
62
+ provider: :ffmpeg,
63
+ codec: :libx264,
64
+ pid_file_name: "/tmp/.headless_ffmpeg_#{test_number}.pid",
65
+ tmp_file_name: "/tmp/.headless_ffmpeg_#{test_number}.mp4",
66
+ extra: ['-pix_fmt yuv420p', '-b 6144k', '-b 6144k'],
67
+ log_file_path: STDERR
68
+ } if video_mode
69
+
70
+ @headless = Headless.new headless_params
71
+
72
+ @headless.start
73
+
74
+ #page.driver.browser.manage.window.resize_to(1280, 900)
75
+
76
+ @headless.video.start_capture if video_mode
77
+ end
78
+
79
+ driver_name
80
+ end
81
+
82
+ def after_test metadata: nil, exception: nil
83
+ Capybara.default_driver = @old_driver
84
+ Capybara.current_driver = @old_driver
85
+ Capybara.javascript_driver = @old_driver
86
+
87
+ Capybara.app_host = nil
88
+
89
+ Capybara.configure do |conf|
90
+ conf.default_max_wait_time = @old_default_max_wait_time
91
+ end
92
+
93
+ if headless_mode
94
+
95
+ #p metadata[:file_path]
96
+
97
+ #if exception
98
+ #File.basename(e.metadata[:file_path]
99
+
100
+ name = 'report'
101
+
102
+ @headless.video.stop_and_save "video_#{test_number}_#{name}.mp4" if video_mode
103
+ # else
104
+ # @headless.video.stop_and_discard if video_mode
105
+ # end
106
+
107
+ @headless.destroy
108
+ end
109
+ end
110
+
111
+ def test_number
112
+ @parallel_tests_mode ? + ENV['TEST_ENV_NUMBER'].to_i : 1
113
+ end
114
+
115
+ def take_screenshot
116
+ if headless_mode
117
+ #@headless.take_screenshot
118
+ end
119
+ end
120
+
121
+ private
122
+
123
+ def register_driver driver:, browser: DEFAULT_BROWSER, selenium_url: nil, capabilities: nil
124
+ driver_name = build_driver_name(driver: driver, browser: browser, selenium_url: selenium_url)
125
+
126
+ # case driver
127
+ # # when :poltergeist
128
+ # # require 'capybara/poltergeist'
129
+ #
130
+ # when :webkit
131
+ # require "capybara-webkit"
132
+ #
133
+ # when :firefox_with_firebug
134
+ # require 'capybara/firebug'
135
+ #
136
+ # else
137
+ # ;
138
+ # end
139
+
140
+ # if driver == :poltergeist
141
+ # properties = {}
142
+ # properties[:debug] = false
143
+ #
144
+ # Capybara.register_driver :poltergeist do |app|
145
+ # Capybara::Poltergeist::Driver.new(app, properties)
146
+ # end
147
+ # elsif driver == :webkit
148
+ # ;
149
+ # else
150
+ if driver == :selenium
151
+ properties = {}
152
+
153
+ if not selenium_url
154
+ properties[:browser] = browser
155
+ else
156
+ properties[:browser] = :remote
157
+ properties[:url] = selenium_url
158
+
159
+ case browser
160
+ when :firefox
161
+ # profile = Selenium::WebDriver::Firefox::Profile.new
162
+ # profile.enable_firebug
163
+ # capabilities = Selenium::WebDriver::Remote::Capabilities.firefox(:firefox_profile => profile)
164
+
165
+ caps = Selenium::WebDriver::Remote::Capabilities.firefox
166
+ when :chrome
167
+ caps = Selenium::WebDriver::Remote::Capabilities.chrome
168
+ else
169
+ ;
170
+ end
171
+
172
+ desired_capabilities =
173
+ if caps
174
+ capabilities.each do |key, value|
175
+ caps[key] = value
176
+ end if capabilities
177
+
178
+ caps
179
+ elsif capabilities
180
+ capabilities
181
+ end
182
+
183
+ properties[:desired_capabilities] = desired_capabilities if desired_capabilities
184
+ end
185
+
186
+ Capybara.register_driver driver_name do |app|
187
+ Capybara::Selenium::Driver.new(app, properties)
188
+ end
189
+ end
190
+
191
+ driver_name
192
+ end
193
+
194
+ def build_driver_name driver:, browser:, selenium_url: nil
195
+ case driver
196
+ when :webkit
197
+ :webkit
198
+ # when :poltergeist
199
+ # :poltergeist
200
+ when :selenium
201
+ name = ""
202
+ name += driver ? "#{driver}_" : "#{Capybara.default_driver}_"
203
+
204
+ name += "#{browser}_" if browser
205
+ name += "remote" if selenium_url
206
+ name = name[0..name.size-2] if name[name.size-1] == "_"
207
+
208
+ name.to_sym
209
+ else
210
+ :unsupported
211
+ end
212
+ end
213
+
214
+ def use_driver driver
215
+ if driver and Capybara.drivers[driver]
216
+ # Capybara.default_driver = driver
217
+ Capybara.current_driver = driver
218
+ Capybara.javascript_driver = driver
219
+ end
220
+ end
221
+ end
@@ -0,0 +1,20 @@
1
+ require 'minitest/spec'
2
+ require 'capybara'
3
+
4
+ class AcceptanceSpec < MiniTest::Spec
5
+ include Capybara::DSL
6
+ include Capybara::Assertions
7
+
8
+ before do
9
+ if metadata[:js]
10
+ Capybara.current_driver = Capybara.javascript_driver
11
+ else
12
+ Capybara.current_driver = Capybara.default_driver
13
+ end
14
+ end
15
+
16
+ def teardown
17
+ Capybara.reset_session!
18
+ Capybara.use_default_driver
19
+ end
20
+ end
@@ -0,0 +1,177 @@
1
+ require 'rspec'
2
+ require 'rspec/expectations'
3
+
4
+ require 'capybara/rspec'
5
+ require 'capybara/dsl'
6
+
7
+ require 'singleton'
8
+
9
+ require 'acceptance_test/capybara/capybara_helper'
10
+
11
+ class RspecHelper
12
+ include Singleton
13
+
14
+ def configure(app_host:, driver: CapybaraHelper::DEFAULT_DRIVER, browser: CapybaraHelper::DEFAULT_BROWSER,
15
+ wait_time: CapybaraHelper::DEFAULT_WAIT_TIME)
16
+ register_extensions
17
+
18
+ RSpec.configure do |rspec_config|
19
+ params = {app_host: app_host, driver: driver, browser: browser, wait_time: wait_time}
20
+
21
+ RspecHelper.instance.configure_rspec rspec_config, params
22
+ end
23
+ end
24
+
25
+ def create_shared_context(name)
26
+ register_extensions
27
+
28
+ RSpec.shared_context name do |app_host:, driver: CapybaraHelper::DEFAULT_DRIVER, browser: CapybaraHelper::DEFAULT_BROWSER,
29
+ wait_time: CapybaraHelper::DEFAULT_WAIT_TIME|
30
+ self.define_singleton_method(:include_context, lambda do
31
+ params = {app_host: app_host, driver: driver, browser: browser, wait_time: wait_time}
32
+
33
+ RspecHelper.instance.configure_rspec self, params
34
+ end)
35
+
36
+ include_context
37
+ end
38
+ end
39
+
40
+ def configure_rspec(rspec_config, app_host:, driver: CapybaraHelper::DEFAULT_DRIVER, browser: CapybaraHelper::DEFAULT_BROWSER,
41
+ wait_time: CapybaraHelper::DEFAULT_WAIT_TIME)
42
+ rspec_config.around(:each) do |example|
43
+ params = {metadata: example.metadata, app_host: app_host, driver: driver, browser: browser, wait_time: wait_time}
44
+
45
+ RspecHelper.instance.before_test params
46
+
47
+ example.run
48
+
49
+ RspecHelper.instance.after_test metadata: example.metadata, exception: example.exception
50
+ end
51
+ end
52
+
53
+ def before_test metadata:, app_host:, driver:, browser:, wait_time:
54
+ old_driver = Capybara.current_driver
55
+
56
+ selected_driver = RspecHelper.instance.get_driver(metadata, driver: driver)
57
+ selected_browser = RspecHelper.instance.get_browser(metadata, browser: browser)
58
+
59
+ params = {app_host: app_host, driver: selected_driver, browser: selected_browser, wait_time: wait_time}
60
+
61
+ CapybaraHelper.instance.before_test params
62
+
63
+ # puts "Using driver: #{Capybara.current_driver}."
64
+ # puts "Default wait time: #{Capybara.default_max_wait_time }."
65
+
66
+ new_driver = Capybara.current_driver
67
+
68
+ Capybara.current_session.instance_variable_set(:@mode, new_driver)
69
+
70
+ if old_driver != new_driver
71
+ metadata.delete(old_driver)
72
+ metadata[new_driver] = true
73
+ end
74
+ end
75
+
76
+ def after_test(metadata: metadata, exception: exception)
77
+ CapybaraHelper.instance.after_test metadata: metadata, exception: exception
78
+
79
+ # driver = driver(example.metadata)
80
+ #
81
+ # if driver and exception and page and not [:webkit].include? driver
82
+ # screenshot_maker.basedir = File.expand_path(config[:screenshots_dir])
83
+ #
84
+ # screenshot_maker.make page, example.metadata
85
+ #
86
+ # puts example.metadata[:full_description]
87
+ # puts "Screenshot: #{screenshot_maker.screenshot_url(example.metadata)}"
88
+ # end
89
+ #
90
+ end
91
+
92
+ def register_extensions
93
+ RSpec.configure do |conf|
94
+ conf.filter_run_excluding :exclude => true
95
+ end
96
+
97
+ RSpec::configure do |config|
98
+ config.include RSpec::Matchers::DSL
99
+ config.include Capybara::RSpecMatchers
100
+ config.include Capybara::DSL
101
+ end
102
+
103
+ # RSpec::Core::ExampleGroup.send :include, Capybara::DSL
104
+ end
105
+
106
+ def get_driver(metadata, driver:)
107
+ #driver = ENV['DRIVER'].nil? ? nil : ENV['DRIVER'].to_sym
108
+
109
+ driver_name = driver
110
+
111
+ driver_name = metadata[:driver] unless driver_name
112
+
113
+ supported_drivers.each do |supported_driver|
114
+ driver_name = supported_driver if metadata[supported_driver]
115
+ break if driver
116
+ end if driver.nil?
117
+
118
+ driver_name = :selenium unless driver_name
119
+
120
+ driver_name
121
+ end
122
+
123
+ def get_browser(metadata, browser:)
124
+ #browser_name = ENV['BROWSER'].nil? ? nil : ENV['BROWSER'].to_sym
125
+
126
+ browser_name = browser
127
+
128
+ browser_name = metadata[:browser] unless browser_name
129
+
130
+ supported_browsers.each do |supported_browser|
131
+ browser_name = supported_browser if metadata[supported_browser]
132
+ break if browser
133
+ end if browser.nil?
134
+
135
+ browser_name = CapybaraHelper::DEFAULT_BROWSER unless browser_name
136
+
137
+ browser_name
138
+ end
139
+
140
+ def supported_drivers
141
+ [:selenium, :webkit]
142
+ end
143
+
144
+ def supported_browsers
145
+ [:chrome, :firefox]
146
+ end
147
+
148
+ def run_on_specs_finished command
149
+ clazz = Object.const_set("CustomFormatter", Class.new)
150
+
151
+ clazz.class_eval do
152
+ define_method :initialize do |_| ; end
153
+
154
+ define_method :close do |_|
155
+ system command
156
+
157
+ # require 'launchy'
158
+ #
159
+ # ENV['BROWSER'] = '/Applications/Google\ Chrome.app'
160
+ #
161
+ # Launchy.open "output/wikipedia-turnip-report.html"
162
+ end
163
+ end
164
+
165
+ RSpec::Core::Formatters.register clazz, :close
166
+
167
+ RSpec::configure do |config|
168
+ config.formatter = "CustomFormatter"
169
+ end
170
+
171
+ # RSpec.configure do |config|
172
+ # config.after(:type => :feature) do
173
+ # system cmd
174
+ # end
175
+ # end
176
+ end
177
+ end
@@ -0,0 +1,46 @@
1
+ require 'singleton'
2
+
3
+ require 'turnip/capybara'
4
+
5
+ class TurnipHelper
6
+ include Singleton
7
+
8
+ def configure(title: 'Turnip', report_file:, steps_dir:)
9
+ # configure turnip formatter
10
+
11
+ require 'turnip_formatter'
12
+
13
+ RSpec.configure do |config|
14
+ config.add_formatter RSpecTurnipFormatter, report_file
15
+ end
16
+
17
+ TurnipFormatter.configure do |config|
18
+ config.title = title
19
+ end
20
+
21
+ # configure gnawrnip
22
+
23
+ require 'gnawrnip'
24
+
25
+ Gnawrnip.configure do |c|
26
+ c.make_animation = true
27
+ c.max_frame_size = 1024 # pixel
28
+ end
29
+
30
+ Gnawrnip.ready!
31
+
32
+ load_steps [steps_dir]
33
+ end
34
+
35
+ def load_steps support_dirs
36
+ support_dirs.each do |support_dir|
37
+ Dir["#{support_dir}/**/steps/*_steps.rb"].each do |name|
38
+ ext = File.extname(name)
39
+ name = name[support_dir.length+1..name.length-ext.length-1]
40
+
41
+ require name
42
+ end
43
+ end
44
+ end
45
+
46
+ end