cyperful 0.1.4 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 61dce63e0bff592ded11835cf202b241d717acaaf99331db8f447d3d7dc2aba9
4
- data.tar.gz: a2ec8532880c523e33ce15d377a1fc28b6832354306c784fa3dea7a38200302b
3
+ metadata.gz: 341d51e03119eed5f4354673d25c81f2630ee73bb48e29312a797db76b389f80
4
+ data.tar.gz: f52e880307fa10e3529ebaac44d165e860bcb94e5913d9c0808732881ac96e63
5
5
  SHA512:
6
- metadata.gz: 0cd35d0e7f3ee879367682ef2780fd6904d879a2d0a6c95a936191c3054b9cbfe60f682d4b911804565050b4da8a75744f5f8f58ef41d8fe82c02e1de84278de
7
- data.tar.gz: 5755db30366159e25279fde63c4e2ccc2f94c934dee4538b237b8e3aee05774ff0182ee8e006d21aaf525ed0b8bdb75675a2fdbeb2879d0ab8fe66d92ecbd4ff
6
+ metadata.gz: 5baea001193479787f029a38b800fc98fdd5eee46450eefdf6a874c16b890181f49c110c97fe1ae668d160d1c925996ce3d40fe8013dedfa342f015a0cdaa6b5
7
+ data.tar.gz: d72eaed23330c7ebcc2c865685c0ee4c1854911f5680c5b7dc42d951d8ba5d9c77cb8e89a4a3609d33523f4cfd0b47c52685d3a15257ffbd9a3057b7de308806
@@ -0,0 +1,19 @@
1
+ module Cyperful
2
+ # @abstract
3
+ class AbstractCommand < StandardError
4
+ # don't print normal error/backtrace
5
+ def to_s
6
+ command_name =
7
+ self.class.name.split("::").last.chomp("Command").underscore
8
+ "(Captured cyperful command: #{command_name})"
9
+ end
10
+ def backtrace
11
+ []
12
+ end
13
+ end
14
+
15
+ class ResetCommand < AbstractCommand
16
+ end
17
+ class ExitCommand < AbstractCommand
18
+ end
19
+ end
@@ -18,7 +18,7 @@ class Cyperful::Driver
18
18
 
19
19
  @source_filepath =
20
20
  Object.const_source_location(test_class.name).first ||
21
- (raise "Could not find source file for #{test_class.name}")
21
+ raise("Could not find source file for #{test_class.name}")
22
22
 
23
23
  reset_steps
24
24
 
@@ -36,11 +36,11 @@ class Cyperful::Driver
36
36
 
37
37
  # Sanity check
38
38
  unless @step_pausing_queue.empty?
39
- raise "step_pausing_queue is not empty during setup"
39
+ raise "Unexpected: step_pausing_queue must be empty during setup"
40
40
  end
41
41
 
42
42
  # Wait for the user to click "Start"
43
- step_pausing_dequeue
43
+ step_pausing_dequeue if @pause_at_step == true
44
44
  end
45
45
 
46
46
  def step_pausing_dequeue
@@ -81,6 +81,11 @@ class Cyperful::Driver
81
81
 
82
82
  @pause_at_step = true
83
83
 
84
+ run_options = self.class.pop_run_options!
85
+ if run_options.key?(:pause_at_step)
86
+ @pause_at_step = run_options[:pause_at_step]
87
+ end
88
+
84
89
  @test_result = nil
85
90
 
86
91
  # reset SCREENSHOTS_DIR
@@ -88,10 +93,39 @@ class Cyperful::Driver
88
93
  FileUtils.mkdir_p(SCREENSHOTS_DIR)
89
94
  end
90
95
 
96
+ @next_run_options = {}
97
+ def self.next_run_options=(options)
98
+ @next_run_options = options
99
+ end
100
+ def self.pop_run_options!
101
+ opts = @next_run_options
102
+ @next_run_options = {}
103
+ opts
104
+ end
105
+
106
+ private def reload_const(class_name, source_path)
107
+ Object.send(:remove_const, class_name) if Object.const_defined?(class_name)
108
+ load(source_path) # reload the file
109
+ unless Object.const_defined?(class_name)
110
+ raise "Failed to reload test class: #{class_name}"
111
+ end
112
+ Object.const_get(class_name)
113
+ end
114
+
91
115
  def queue_reset
92
- # FIXME: there may be other tests that are "queued" to run `at_exit`,
93
- # so they'll run before this test restarts.
94
- at_exit { Minitest.run_one_method(@test_class, @test_name) }
116
+ at_exit do
117
+ # reload test-suite code on reset (for `setup_file_listener`)
118
+ # TODO: also reload dependent files
119
+ # NOTE: run_on_method will fail if test_name also changed
120
+ @test_class = reload_const(@test_class.name, @source_filepath)
121
+
122
+ # TODO
123
+ # if Cyperful.config.reload_source_files && defined?(Rails)
124
+ # Rails.application.reloader.reload!
125
+ # end
126
+
127
+ Minitest.run_one_method(@test_class, @test_name)
128
+ end
95
129
  end
96
130
 
97
131
  # subscribe to the execution of each line of code in the test.
@@ -111,28 +145,41 @@ class Cyperful::Driver
111
145
  @tracepoint.enable
112
146
  end
113
147
 
148
+ private def test_directory
149
+ @source_filepath.match(%r{^/.+/(?:test|spec)\b})&.[](0) ||
150
+ raise("Could not determine test directory for #{@source_filepath}")
151
+ end
152
+
114
153
  # Every time a file changes the `test/` directory, reset this test
115
154
  # TODO: add an option to auto-run on reload
116
155
  def setup_file_listener
117
- # TODO: we need to somehow reload the source files
118
-
119
- # test_dir = @source_filepath.match(%r{^/.+/(?:test|spec)\b})[0]
120
-
121
- # @file_listener&.stop
122
- # @file_listener =
123
- # Listen.to(test_dir) do |_modified, _added, _removed|
124
- # puts "Test files changed, resetting test..."
125
-
126
- # @pause_at_step = true
127
- # @step_pausing_queue.enq(:reset)
128
- # end
129
- # @file_listener.start
156
+ # TODO
157
+ # if Cyperful.config.reload_source_files
158
+ # @source_file_listener = Listen.to(rails_directory) ...
159
+ # end
160
+
161
+ if Cyperful.config.reload_test_files
162
+ @file_listener&.stop
163
+ @file_listener =
164
+ Listen.to(test_directory) do |_modified, _added, _removed|
165
+ puts "Test files changed, resetting test..."
166
+
167
+ # keep the same pause state after the reload
168
+ self.class.next_run_options = { pause_at_step: @pause_at_step }
169
+
170
+ @pause_at_step = true # pause current test immediately
171
+ @step_pausing_queue.enq(:reset)
172
+ end
173
+ @file_listener.start
174
+ end
130
175
  end
131
176
 
132
177
  def print_steps
133
- puts "#{@steps.length} steps:"
134
- @steps.each do |step|
135
- puts " #{step[:method]}: #{step[:line]}:#{step[:column]}"
178
+ puts "Found #{@steps.length} steps:"
179
+ @steps.each_with_index do |step, i|
180
+ puts " #{
181
+ (i + 1).to_s.rjust(2)
182
+ }. #{step[:method]}: #{step[:line]}:#{step[:column]}"
136
183
  end
137
184
  puts
138
185
  end
@@ -190,11 +237,39 @@ class Cyperful::Driver
190
237
  @ui_server.notify(steps_updated_data)
191
238
  end
192
239
 
240
+ # called at the start of each step
241
+ def pause_on_step(step)
242
+ @current_step = step
243
+
244
+ # using `print` so we can append the step's status (see `finish_current_step`)
245
+ print("STEP #{(step[:index] + 1).to_s.rjust(2)}: #{step[:as_string]}")
246
+
247
+ if @pause_at_step == true || @pause_at_step == step[:index]
248
+ @current_step[:paused_at] = (Time.now.to_f * 1000.0).to_i
249
+ @current_step[:status] = "paused"
250
+ notify_updated_steps
251
+
252
+ # async wait for `continue_next_step`
253
+ step_pausing_dequeue
254
+ end
255
+
256
+ @current_step[:status] = "running"
257
+ @current_step[:start_at] = (Time.now.to_f * 1000.0).to_i
258
+ notify_updated_steps
259
+ end
260
+
261
+ # called at the end of each step
193
262
  private def finish_current_step(error = nil)
194
263
  if @current_step
195
264
  @current_step[:end_at] = (Time.now.to_f * 1000.0).to_i
196
265
  @current_step[:status] = !error ? "passed" : "failed"
197
266
 
267
+ puts(
268
+ " (#{@current_step[:end_at] - @current_step[:start_at]}ms)#{
269
+ error ? " FAILED" : ""
270
+ }",
271
+ )
272
+
198
273
  # take screenshot after the step has finished
199
274
  # path = File.join(SCREENSHOTS_DIR, "#{@current_step[:index]}.png")
200
275
 
@@ -210,25 +285,6 @@ class Cyperful::Driver
210
285
  notify_updated_steps
211
286
  end
212
287
 
213
- def pause_on_step(step)
214
- @current_step = step
215
-
216
- puts "STEP: #{step[:as_string]}"
217
-
218
- if @pause_at_step == true || @pause_at_step == step[:index]
219
- @current_step[:paused_at] = (Time.now.to_f * 1000.0).to_i
220
- @current_step[:status] = "paused"
221
- notify_updated_steps
222
-
223
- # async wait for `continue_next_step`
224
- step_pausing_dequeue
225
- end
226
-
227
- @current_step[:status] = "running"
228
- @current_step[:start_at] = (Time.now.to_f * 1000.0).to_i
229
- notify_updated_steps
230
- end
231
-
232
288
  private def continue_next_step
233
289
  @step_pausing_queue.enq(:next)
234
290
  end
@@ -283,7 +339,15 @@ class Cyperful::Driver
283
339
  [abs_url, display_url]
284
340
  end
285
341
 
286
- WATCHER_JS = File.read(File.join(Cyperful::ROOT_DIR, "watcher.js"))
342
+ def self.load_frame_agent_js
343
+ return @frame_agent_js if defined?(@frame_agent_js)
344
+
345
+ @frame_agent_js =
346
+ File.read(File.join(Cyperful::ROOT_DIR, "public/frame-agent.js")).sub(
347
+ "__CYPERFUL_CONFIG__",
348
+ { CYPERFUL_ORIGIN: "http://localhost:#{Cyperful.config.port}" }.to_json,
349
+ )
350
+ end
287
351
 
288
352
  private def skip_multi_sessions
289
353
  unless Capybara.current_session == @session
@@ -307,10 +371,10 @@ class Cyperful::Driver
307
371
 
308
372
  @session.execute_script("window.location.href = #{abs_url.to_json}")
309
373
 
310
- # inject the watcher script into the page being tested.
374
+ # inject the frame-agent script into the page being tested.
311
375
  # this script will notify the Cyperful UI for events like:
312
376
  # console logs, network requests, client navigations, errors, etc.
313
- @session.execute_script(WATCHER_JS) # ~9ms empirically
377
+ @session.execute_script(Cyperful::Driver.load_frame_agent_js) # ~9ms empirically
314
378
 
315
379
  true
316
380
  end
@@ -323,7 +387,7 @@ class Cyperful::Driver
323
387
  end
324
388
 
325
389
  def setup_api_server
326
- @ui_server = Cyperful::UiServer.new(port: 3004)
390
+ @ui_server = Cyperful::UiServer.new(port: Cyperful.config.port)
327
391
 
328
392
  @cyperful_origin = @ui_server.url_origin
329
393
 
@@ -363,7 +427,7 @@ class Cyperful::Driver
363
427
  @tracepoint = nil
364
428
 
365
429
  if error&.is_a?(Cyperful::ResetCommand)
366
- puts "\nPlease ignore the error, we're just resetting the test ;)"
430
+ puts "\nResetting test (ignore any error logs)..."
367
431
 
368
432
  @ui_server.notify(nil) # `break` out of the `loop` (see `UiServer#socket_open`)
369
433
 
@@ -395,6 +459,7 @@ class Cyperful::Driver
395
459
  @ui_server.notify(nil) # `break` out of the `loop` (see `UiServer#socket_open`)
396
460
 
397
461
  puts "Cyperful teardown complete. Waiting for command..."
462
+ # NOTE: this will raise an `Interrupt` if the user Ctrl+C's here
398
463
  command = @step_pausing_queue.deq
399
464
  queue_reset if command == :reset
400
465
  ensure
@@ -1,3 +1,5 @@
1
+ require "action_dispatch/system_testing/driver"
2
+
1
3
  # we need to override the some Capybara::Session methods because they
2
4
  # control the top-level browser window, but we want them
3
5
  # to control the iframe instead
@@ -51,13 +53,48 @@ module Cyperful::SystemTestHelper
51
53
  Cyperful.teardown(error)
52
54
  super
53
55
  end
56
+
57
+ # Disable default screenshot on failure b/c we handle them ourselves.
58
+ # https://github.com/rails/rails/blob/main/actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb#L156
59
+ def take_failed_screenshot
60
+ nil
61
+ end
62
+ end
63
+
64
+ module PrependSystemTestingDriver
65
+ def initialize(...)
66
+ super(...)
67
+
68
+ prev_capabilities = @capabilities
69
+ @capabilities =
70
+ proc do |driver_opts|
71
+ prev_capabilities&.call(driver_opts)
72
+
73
+ next unless driver_opts.respond_to?(:add_argument)
74
+
75
+ # this assumes Selenium and Chrome:
76
+
77
+ # so user isn't prompted when we start recording video w/ MediaStream
78
+ driver_opts.add_argument("--auto-accept-this-tab-capture")
79
+ driver_opts.add_argument("--use-fake-ui-for-media-stream")
80
+
81
+ # make sure we're not in headless mode
82
+ driver_opts.args.delete("--headless")
83
+ driver_opts.args.delete("--headless=new")
84
+ end
85
+ end
54
86
  end
87
+ ActionDispatch::SystemTesting::Driver.prepend(PrependSystemTestingDriver)
88
+
89
+ # if defined?(Minitest::Test)
90
+ # Minitest::Test::PASSTHROUGH_EXCEPTIONS << Cyperful::AbstractCommand
91
+ # end
55
92
 
56
93
  # we need to allow the iframe to be embedded in the cyperful server
57
94
  # TODO: use Rack middleware instead to support non-Rails apps
58
95
  if defined?(Rails)
59
96
  Rails.application.config.content_security_policy do |policy|
60
- policy.frame_ancestors(:self, "localhost:3004")
97
+ policy.frame_ancestors(:self, "localhost:#{Cyperful.config.port}")
61
98
  end
62
99
  else
63
100
  warn "Cyperful: Rails not detected, skipping content_security_policy fix.\nThe Cyperful UI may not work correctly."
@@ -74,7 +74,7 @@ class Cyperful::TestParser
74
74
  test_string = node.children[0].children[2].children[0]
75
75
 
76
76
  # https://github.com/rails/rails/blob/66676ce499a32e4c62220bd05f8ee2cdf0e15f0c/activesupport/lib/active_support/testing/declarative.rb#L14C23-L14C61
77
- test_method = "test_#{test_string.gsub(/\s+/, "_")}".to_sym
77
+ test_method = :"test_#{test_string.gsub(/\s+/, "_")}"
78
78
 
79
79
  block_node = node.children[2]
80
80
  [test_method, block_node]
@@ -106,6 +106,18 @@ class Cyperful::UiServer
106
106
 
107
107
  res.status = 204
108
108
  end
109
+
110
+ @server.mount_proc("/api/config") do |req, res|
111
+ if req.request_method != "GET"
112
+ res.body = "Only POST allowed"
113
+ res.status = 405
114
+ next
115
+ end
116
+
117
+ res.body = Cyperful.config.to_h.to_json
118
+ res["Content-Type"] = "application/json"
119
+ res.status = 200
120
+ end
109
121
  end
110
122
 
111
123
  def start_async
data/lib/cyperful.rb CHANGED
@@ -6,6 +6,27 @@ module Cyperful
6
6
 
7
7
  @current = nil
8
8
 
9
+ class Config < Struct.new(
10
+ :port,
11
+ :auto_run_on_reload,
12
+ :reload_test_files,
13
+ # :reload_source_files, # not implemented yet
14
+ :history_recording, # EXPERIMENTAL
15
+ keyword_init: true,
16
+ )
17
+ def initialize
18
+ super(
19
+ port: 3004,
20
+ auto_run_on_reload: true,
21
+ reload_test_files: true,
22
+ history_recording: true,
23
+ )
24
+ end
25
+ end
26
+ def self.config
27
+ @config ||= Config.new
28
+ end
29
+
9
30
  def self.current
10
31
  @current
11
32
  end
@@ -34,13 +55,7 @@ module Cyperful
34
55
  end
35
56
  end
36
57
 
37
- class Cyperful::AbstractCommand < StandardError
38
- end
39
- class Cyperful::ResetCommand < Cyperful::AbstractCommand
40
- end
41
- class Cyperful::ExitCommand < Cyperful::AbstractCommand
42
- end
43
-
58
+ require "cyperful/commands"
44
59
  require "cyperful/test_parser"
45
60
  require "cyperful/ui_server"
46
61
  require "cyperful/driver"