capybara-dommy 0.15.0 → 0.17.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a0c6dc91853e4d3fb29cbaf6893f47a466bd173bd4ed33e62212687e3c886319
4
- data.tar.gz: 3cb3bd57dcfeb15a43ab978a8604126905339d0a2615d14d2946b0353a3a3286
3
+ metadata.gz: 1bf9f2ae3612c8a254469d622b264c02c7fb599dda4d7763a336eeb54e3cb68b
4
+ data.tar.gz: 17b6fac267734c67dc7fd29569117b7508376801ca5a396e7e64b46b3a0c130b
5
5
  SHA512:
6
- metadata.gz: 32a291da5967dd94de5a8d5be25efe6ac38a9f8e4dd55916e22e31e9a71b21b77991ba2e8d7f36def3c3a9d0edd705a37eb055fec6109c4204381234e3d9cf55
7
- data.tar.gz: bfc22602fefec6663ea26ebcceb48a7a78b7741438b7de920de27c4f8d3c0d42534b43ba717257476552b567aaed241da4537b2137cd70a1a2784994b0f1c16e
6
+ metadata.gz: 82ee3050338f46b47479974542ec54230e8fb560cc39da3c273ab1c5dc5892d787e41af0aae8814c2c91cd2d84a72cd7f6ad244122a61fdb7574f8ae455e67df
7
+ data.tar.gz: b712d579a86b2cadef1dc43d36b39df894acb62b2010ddb04790b815142c5b6e1df5e0e802c9d006d7090abc166742ce907bd69d8da946523ab4f5f8c17c2cfb
data/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.17.0 — 2026-10-20
4
+
5
+ Versioned in lockstep with [`dommy`](https://github.com/takahashim/dommy) 0.17.0; no change of its own.
6
+
7
+ ## 0.16.0 — 2026-10-10
8
+
9
+ ### Breaking changes
10
+
11
+ - **Under JavaScript, a Capybara wait (`default_max_wait_time`, `wait:`) is a span of the page's virtual time**: a retry moves the clock to the page's next timer within it instead of sleeping, and a query that nothing within the wait can satisfy fails at once. dommy-examples' debounced live search runs its system specs at 41 ms an example instead of 296 ms, and `have_no_css` on an element that stays fails in milliseconds instead of after the wait. Only a fetch still running on a `network_executor` worker and an open WebSocket or EventSource are waited for in real time. A wait for something outside the page and its clock — a background job, another process — now fails at once instead of polling in real time; wait for it in the test, or reach it through the app.
12
+
13
+ ### Added
14
+
15
+ - `drag_to` under JavaScript: a press inside a draggable element runs an HTML drag-and-drop (`dragstart` through `drop` and `dragend`), anything else moves the pointer to the target and releases it there (`mousemove`, `mouseup`), with `delay` (0.05 s) of virtual time between the steps.
16
+ - `current_window.size` and `current_window.resize_to(width, height)`: the one window starts at 1280x720, and its size is the viewport `@media` and `matchMedia` see, kept across `reset!`.
17
+
18
+ ### Changed
19
+
20
+ - With `Capybara.raise_server_errors = false`, an exception the app raises gives the page a 500 response with Puma's error text instead of failing the test; an exception not in `Capybara.server_errors` gives the same 500.
21
+ - A node whose element left the tree (a Turbo Stream replaced it, a framework re-rendered it) is stale, as under Selenium, so Capybara finds it again instead of reading the detached element.
22
+
3
23
  ## 0.15.0 — 2026-10-04
4
24
 
5
25
  ### Changed
data/README.md CHANGED
@@ -30,9 +30,11 @@ speed and simplicity of a Rack-style driver.
30
30
 
31
31
  - The default driver does not execute JavaScript. Use the JavaScript-enabled
32
32
  variant for embedded QuickJS execution.
33
- - Screenshots and browser windows are not supported. Native alerts, confirms,
34
- and prompts are supported by the JavaScript-enabled variant through
35
- Capybara's modal helpers.
33
+ - Screenshots and additional browser windows are not supported. The one
34
+ window there is can be resized (`current_window.resize_to`), which resizes
35
+ the viewport `@media` and `matchMedia` see. Native alerts, confirms, and
36
+ prompts are supported by the JavaScript-enabled variant through Capybara's
37
+ modal helpers.
36
38
  - Constructable stylesheets can be built (`new CSSStyleSheet()`), but
37
39
  `adoptedStyleSheets` is not implemented — assigning one applies no style.
38
40
  Component libraries that feature-detect (`'adoptedStyleSheets' in
@@ -64,23 +66,41 @@ Dommy does not implement yet. Turn it off with
64
66
  `config.raise_js_errors = false`, or suppress it for one block with
65
67
  `page.driver.allow_js_errors { ... }`.
66
68
 
67
- **There are two clocks.** Dommy's timers run on a virtual clock that Capybara's
68
- retry loop advances a frame at a time, so a debounce or a `setTimeout` resolves
69
- in a few polls rather than in real time. Nothing advances it outside that loop:
70
- `sleep` does not, and Rails' `travel_to` does not reach JavaScript's `Date`.
69
+ **There are two clocks, and a wait is measured on the page's.** Dommy's
70
+ timers run on a virtual clock, which only Capybara's commands move. Each query
71
+ first moves it by a frame (16 ms); a wait (`default_max_wait_time`, `wait:`)
72
+ is then a span of that clock, not of real time. When a query fails, the retry
73
+ moves the clock straight to the page's next timer, as long as that is within
74
+ the wait, so a 300 ms debounce costs one retry, not 300 ms. When nothing within
75
+ the wait can change the page, the query fails at once: `have_no_css` on an
76
+ element that stays, or a `have_css` that will never match, does not wait 2
77
+ seconds. The clock is the whole page's: an iframe's timers run on it too. Only
78
+ what the clock does not drive is waited for in real time, for at most the
79
+ wait: a fetch still running on a `network_executor` worker (before any later
80
+ timer, as its response would come first in a browser), and an open WebSocket or
81
+ EventSource, which the app can push to at any moment. Nothing advances the clock outside
82
+ Capybara's commands: `sleep` does not, and Rails' `travel_to` does not reach
83
+ JavaScript's `Date`.
71
84
 
72
85
  **The app runs in the test process.** There is no server thread and no port, so
73
86
  `use_transactional_tests` works unchanged and an exception in the app is raised
74
- directly in the test. In exchange, `Capybara.server` and anything built on a
75
- separate app thread has no meaning here.
87
+ directly in the test, at the request that raised it rather than at the next
88
+ Capybara command. `Capybara.raise_server_errors` and `Capybara.server_errors`
89
+ still decide which exceptions those are: with `raise_server_errors` off, or for
90
+ an exception not listed in `server_errors`, the page gets the 500 response Puma
91
+ would send. In exchange, `Capybara.server` and anything built on a separate app
92
+ thread has no meaning here.
76
93
 
77
94
  **There is no layout.** Visibility comes from HTML-level rules and stylesheet
78
95
  `display` / `visibility` / `opacity`, never from geometry, so `obscured?`,
79
96
  scroll position and element size are unavailable.
80
97
 
81
- **Input is synthesised.** Events are dispatched from Ruby rather than by the OS,
82
- so `isTrusted` is false and hover, drag and special keys are limited. An
83
- unanswered `confirm` returns false rather than blocking.
98
+ **Input is synthesised.** Events are dispatched from Ruby rather than by the OS.
99
+ They are trusted, as a user's input is, but the pointer has no real position:
100
+ mouse coordinates are 0 (each element's box center for a drag), and `drag_to`
101
+ moves straight from the element pressed to the target, with `delay` seconds of
102
+ virtual time between the steps. An unanswered `confirm` returns false rather
103
+ than blocking.
84
104
 
85
105
  **Frames are fetched, not live.** Switching to a frame re-requests its URL; the
86
106
  frame's own scripts do not run.
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Capybara
4
+ module Dommy
5
+ # What stands in for the server Capybara would boot for a browser driver.
6
+ # Capybara runs the app under Puma, which answers any exception with a 500;
7
+ # its middleware also records the exceptions listed in `server_errors`, and
8
+ # the session re-raises those in the test when `raise_server_errors` is on.
9
+ # Here the app runs in the test process, so a reportable error is raised
10
+ # right where it happens, and every other one becomes the 500 Puma sends.
11
+ # A spec that turns raise_server_errors off to visit a page the app fails
12
+ # on then sees the error page, as it does under a browser.
13
+ class AppServer
14
+ # Puma's own response to an exception the app did not handle.
15
+ ERROR_RESPONSE_BODY = "An unhandled lowlevel error occurred. The application logs may have details.\n"
16
+
17
+ # `options` returns the owning session's Capybara config, or nil for a
18
+ # standalone driver. It is read per request: specs flip
19
+ # raise_server_errors after the driver exists.
20
+ def initialize(app, &options)
21
+ @app = app
22
+ @options = options
23
+ end
24
+
25
+ # Puma rescues everything a request can raise; Interrupt, SystemExit and
26
+ # NoMemoryError are not the app's errors, so they still propagate.
27
+ def call(env)
28
+ @app.call(env)
29
+ rescue StandardError, ScriptError => e
30
+ raise if reportable?(e)
31
+
32
+ [500, {"content-type" => "text/plain; charset=utf-8"}, [ERROR_RESPONSE_BODY]]
33
+ end
34
+
35
+ private
36
+
37
+ # A driver used without a Capybara session reports everything, as
38
+ # Capybara's defaults (raise_server_errors, server_errors = [Exception]) do.
39
+ def reportable?(error)
40
+ options = @options.call
41
+ return true if options.nil?
42
+
43
+ options.raise_server_errors && options.server_errors.any? { |klass| error.is_a?(klass) }
44
+ end
45
+ end
46
+ end
47
+ end
@@ -8,8 +8,9 @@ module Capybara
8
8
  # navigation / query / reset! parts of the Capybara::Driver::Base contract;
9
9
  # element interaction lives in Capybara::Dommy::Node. The JS-enabled mode
10
10
  # additionally supplies deterministic native dialog responses for Capybara's
11
- # alert/confirm/prompt helpers. Screenshot and window methods remain with
12
- # Driver::Base (which raises Capybara::NotSupportedByDriverError).
11
+ # alert/confirm/prompt helpers. There is one window, which can be resized;
12
+ # opening, switching and closing windows remain with Driver::Base (which
13
+ # raises Capybara::NotSupportedByDriverError).
13
14
  class Driver < Capybara::Driver::Base
14
15
  VISIBILITY_MODES = %i[all html none].freeze
15
16
 
@@ -18,6 +19,13 @@ module Capybara
18
19
  # screenshot helper reads it back for its inline / artifact output.
19
20
  BLANK_PNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==".unpack1("m").freeze
20
21
 
22
+ # The handle of the one window there is.
23
+ WINDOW_HANDLE = "dommy"
24
+
25
+ # The frame each query moves the clock by before it reads the page:
26
+ # the time a browser would have had between two Capybara commands.
27
+ FRAME_MS = 16
28
+
21
29
  attr_reader :app, :visibility
22
30
 
23
31
  # --- Deterministic-time seam (used by JS runtimes) ---
@@ -68,7 +76,7 @@ module Capybara
68
76
  # A JS session needs the virtual clock pumped inside Capybara's
69
77
  # synchronize loop, so waiting expectations converge on timer/fetch
70
78
  # driven updates. A host-installed pump (the documented seam) wins.
71
- @time_pump ||= -> { @rack_session&.advance_time(16) } if @javascript
79
+ @time_pump ||= -> { @rack_session&.advance_time(FRAME_MS) } if @javascript
72
80
  end
73
81
 
74
82
  # Whether this driver runs page JavaScript (`javascript: true`, backed by
@@ -103,7 +111,8 @@ module Capybara
103
111
  host = effective_host
104
112
  if @rack_session.nil? || @rack_session_host != host
105
113
  @rack_session&.dispose
106
- @rack_session = ::Dommy::Rack::Session.new(@app, **@session_options.merge(default_host: host))
114
+ @rack_session = ::Dommy::Rack::Session.new(app_server, **@session_options.merge(default_host: host))
115
+ @rack_session.resize_viewport(*@window_size) if @window_size
107
116
  @rack_session_host = host
108
117
  end
109
118
  @rack_session
@@ -225,16 +234,43 @@ module Capybara
225
234
  document&.title
226
235
  end
227
236
 
237
+ # --- Window ---
238
+ #
239
+ # One browser window, whose size is the page's viewport (there is no
240
+ # browser chrome, so the outer and inner sizes are the same). Like a
241
+ # browser window it keeps its size across reset! and app_host changes,
242
+ # which build a new rack session.
243
+
244
+ def current_window_handle = WINDOW_HANDLE
245
+ def window_handles = [WINDOW_HANDLE]
246
+
247
+ def window_size(_handle)
248
+ @window_size || default_window_size
249
+ end
250
+
251
+ def resize_window_to(_handle, width, height)
252
+ @window_size = [Integer(width), Integer(height)]
253
+ rack_session.resize_viewport(*@window_size)
254
+ nil
255
+ end
256
+
228
257
  # --- Focus / keyboard ---
229
258
 
230
259
  def active_element
231
260
  Node.new(self, document.active_element)
232
261
  end
233
262
 
234
- # Session-level send_keys. Without JavaScript only focus navigation is
235
- # meaningful, so :tab (the key Capybara's focused: specs use) moves
236
- # focus through the tab order; other keys are ignored.
263
+ # Session-level send_keys: the keys go to the focused element (the body
264
+ # when nothing is focused). Under JavaScript they are dispatched as
265
+ # real keyboard events with their default actions — :tab moves the
266
+ # focus through the sequential navigation order. Without JavaScript
267
+ # only focus navigation is meaningful, so :tab moves the focus and
268
+ # other keys are ignored.
237
269
  def send_keys(*keys)
270
+ if javascript?
271
+ target = document.__internal_focused_element__ || document.body
272
+ return rack_session.send_keys_to(target, *keys) if target
273
+ end
238
274
  keys.each { |key| focus_next_tabbable if key == :tab }
239
275
  end
240
276
 
@@ -258,6 +294,63 @@ module Capybara
258
294
  !@time_pump.nil?
259
295
  end
260
296
 
297
+ # --- Waiting on the virtual clock (see VirtualWait) ---
298
+
299
+ # One Capybara wait: the virtual time it may still move the clock by.
300
+ class Wait
301
+ attr_reader :remaining_ms
302
+
303
+ def initialize(seconds)
304
+ @remaining_ms = (seconds.to_f * 1000).round
305
+ end
306
+
307
+ def spend(ms)
308
+ @remaining_ms -= ms
309
+ end
310
+ end
311
+
312
+ def begin_wait(seconds)
313
+ @wait = Wait.new(seconds)
314
+ end
315
+
316
+ def end_wait
317
+ @wait = nil
318
+ end
319
+
320
+ # After a failed attempt, what lets the page move on:
321
+ #
322
+ # - :moved — a completion a worker handed back was delivered, or the
323
+ # clock moved to the next timer, due within what is left of the wait:
324
+ # try again now.
325
+ # - :outside — only something outside the clock can change the page: a
326
+ # fetch still running on a worker, or an open WebSocket / EventSource
327
+ # the app can push to. Only waiting in real time sees it arrive.
328
+ # - nil — nothing within the wait can change the page.
329
+ #
330
+ # A fetch in flight comes before the next timer, as its response would
331
+ # in a browser, where the network takes milliseconds: moving the clock
332
+ # past it first would let a timeout race the response and win. An open
333
+ # connection comes after: it may never carry anything, and the page's
334
+ # timers must not wait on it.
335
+ def wait_on(wait)
336
+ session = @rack_session
337
+ return nil unless session
338
+
339
+ if session.completion_pending?
340
+ session.advance_time(0)
341
+ return :moved
342
+ end
343
+ return :outside if session.fetch_in_flight?
344
+
345
+ delay = session.next_timer_delay
346
+ if delay && delay <= wait.remaining_ms
347
+ wait.spend(delay)
348
+ session.advance_time(delay)
349
+ return :moved
350
+ end
351
+ :outside if session.open_connections?
352
+ end
353
+
261
354
  def needs_server?
262
355
  false
263
356
  end
@@ -328,6 +421,7 @@ module Capybara
328
421
  # otherwise sit in the ledger until some later command happened to check.
329
422
  def pump!
330
423
  @time_pump&.call
424
+ @wait&.spend(FRAME_MS)
331
425
  rack_session.check_js_errors! if @javascript
332
426
  end
333
427
 
@@ -335,6 +429,15 @@ module Capybara
335
429
  @frame_stack ||= []
336
430
  end
337
431
 
432
+ def default_window_size
433
+ env = ::Dommy::Internal::CSS::MediaQuery::Environment.default
434
+ [env.viewport_width, env.viewport_height]
435
+ end
436
+
437
+ def app_server
438
+ @app_server ||= AppServer.new(@app) { owning_session_options }
439
+ end
440
+
338
441
  # A frame's document: its `srcdoc` when present (Dommy builds it from
339
442
  # the attribute, so there is nothing to fetch), else its `src` resolved
340
443
  # against the enclosing frame's URL so nested relative srcs load
@@ -362,7 +465,11 @@ module Capybara
362
465
  srcdoc = iframe_element.get_attribute("srcdoc")
363
466
  return nil if srcdoc.nil?
364
467
 
365
- return iframe_element.content_document if iframe_element.respond_to?(:content_document) && iframe_element.content_document
468
+ # The frame's own srcdoc document once its (asynchronous) navigation
469
+ # has happened; until then, the one that navigation will show.
470
+ current = iframe_element.content_document if iframe_element.respond_to?(:content_document)
471
+ return current if current && current.url == "about:srcdoc"
472
+ return iframe_element.__internal_build_blank_content_document__ if iframe_element.respond_to?(:__internal_build_blank_content_document__)
366
473
 
367
474
  Dommy.parse(srcdoc).document
368
475
  end
@@ -376,31 +483,11 @@ module Capybara
376
483
  ""
377
484
  end
378
485
 
379
- # Sequential focus navigation: elements with a positive tabindex first
380
- # (ascending, document order within a value), then the remaining
381
- # focusables in document order. The page's tab cycle starts over when
382
- # the current active element is not in the order (e.g. body).
383
- FOCUSABLE_SELECTOR = "a[href], button, input, select, textarea, [tabindex]"
384
-
486
+ # Sequential focus navigation (HTML §6.6.5, Dommy's model): positive
487
+ # tabindex values first, then tree order, through shadow trees, slots
488
+ # and popovers, skipping what is disabled, inert or not rendered.
385
489
  def focus_next_tabbable
386
- ordered = tab_order
387
- return if ordered.empty?
388
-
389
- current = document.active_element
390
- index = ordered.index { |el| el == current }
391
- target = ordered[index ? index + 1 : 0]
392
- target&.focus
393
- end
394
-
395
- def tab_order
396
- candidates = document.query_selector_all(FOCUSABLE_SELECTOR).to_a.reject do |el|
397
- el.get_attribute("tabindex").to_s.start_with?("-") ||
398
- el.has_attribute?("disabled") ||
399
- el.get_attribute("type").to_s.downcase == "hidden" ||
400
- !visible?(el)
401
- end
402
- positive, natural = candidates.each_with_index.partition { |el, _i| el.get_attribute("tabindex").to_i.positive? }
403
- positive.sort_by { |el, i| [el.get_attribute("tabindex").to_i, i] }.map(&:first) + natural.map(&:first)
490
+ ::Dommy::Internal::SequentialFocusNavigation.navigate(document, :forward)
404
491
  end
405
492
 
406
493
  # Capybara's app_host (set per-example) wins over default_host; falls
@@ -231,6 +231,29 @@ module Capybara
231
231
  nil
232
232
  end
233
233
 
234
+ # Drag this element onto `element` with the mouse. Under JavaScript the
235
+ # press and what follows run as in a browser: an HTML drag-and-drop when
236
+ # the press lands in a draggable element (dragstart through dragend),
237
+ # the pointer moving over and releasing on `element` otherwise or with
238
+ # `html5: false`. `delay` seconds of virtual time pass between the steps,
239
+ # as Capybara's Selenium driver waits, so a page that defers its
240
+ # dragover work to a timer sees it run before the drop. Without
241
+ # JavaScript only the virtual :hover state moves to `element`.
242
+ def drag_to(element, html5: nil, delay: 0.05, drop_modifiers: [])
243
+ unless Array(drop_modifiers).empty?
244
+ raise Capybara::NotSupportedByDriverError, "capybara-dommy does not support drop_modifiers"
245
+ end
246
+
247
+ target = element.native
248
+ if driver.javascript?
249
+ pause = -> { driver.rack_session.advance_time((delay * 1000).round) }
250
+ ::Dommy::Interaction::EventSynthesis.drag_and_drop(native, target, pause: pause, html5: html5 != false)
251
+ end
252
+ target.owner_document.__internal_set_hovered_element__(target)
253
+ driver.drain_js if driver.javascript?
254
+ nil
255
+ end
256
+
234
257
  # The shadow root this element hosts (`attachShadow`), as a Capybara node
235
258
  # scoped to that tree — Capybara's `shadow_root`/`within` support. nil
236
259
  # when the element has no shadow root.
@@ -323,12 +346,21 @@ module Capybara
323
346
  end
324
347
  end
325
348
 
349
+ # Stale as Selenium's node is: the element belongs to a page that is
350
+ # gone (a navigation), or it left the tree (a Turbo Stream replaced it,
351
+ # a framework re-rendered it). Capybara then finds the element again
352
+ # by the query it came from, so a spec holding a node across such a
353
+ # change reads the page, not a detached copy of it.
326
354
  def stale_check
327
- return if native.document.equal?(driver.document)
355
+ return if native.document.equal?(driver.document) && connected?(native)
328
356
 
329
357
  raise StaleElementReferenceError, "element is no longer attached to the document"
330
358
  end
331
359
 
360
+ def connected?(node)
361
+ node.respond_to?(:is_connected?) ? node.is_connected? : true
362
+ end
363
+
332
364
  def submit_owning_form
333
365
  form = form_for(native)
334
366
  driver.submit_form(form, submitter: native) if form
@@ -408,7 +440,7 @@ module Capybara
408
440
  end
409
441
 
410
442
  def set_text_value(value)
411
- string = value.to_s
443
+ string = form_control_value(value)
412
444
  if text_or_password? && attribute_present?("maxlength")
413
445
  string = string[0, native.get_attribute("maxlength").to_i].to_s
414
446
  end
@@ -424,6 +456,21 @@ module Capybara
424
456
  end
425
457
  end
426
458
 
459
+ # Capybara accepts Ruby date/time objects for temporal controls. Convert
460
+ # them to HTML values before the element runs its sanitization algorithm.
461
+ def form_control_value(value)
462
+ return value.to_s unless input_field? && value.respond_to?(:strftime)
463
+
464
+ format = case native.type
465
+ when "date" then "%Y-%m-%d"
466
+ when "month" then "%Y-%m"
467
+ when "week" then "%G-W%V"
468
+ when "time" then "%H:%M:%S.%L"
469
+ when "datetime-local" then "%Y-%m-%dT%H:%M:%S.%L"
470
+ end
471
+ format ? value.strftime(format) : value.to_s
472
+ end
473
+
427
474
  # Set a text field's value. Under JavaScript this types like a user:
428
475
  # focus, value, then input + change events, so Stimulus/React handlers
429
476
  # observe the edit; the JS-less driver keeps the bare value write
@@ -449,13 +496,7 @@ module Capybara
449
496
  end
450
497
 
451
498
  def set_range(value)
452
- min = (native.get_attribute("min") || 0).to_f
453
- max = (native.get_attribute("max") || 100).to_f
454
- step = (native.get_attribute("step") || 1).to_f
455
- v = value.to_f.clamp(min, max)
456
- v = (((v - min) / step).round * step) + min
457
- v = v.clamp(min, max)
458
- native.value = (v == v.to_i ? v.to_i : v).to_s
499
+ native.value = value.to_s
459
500
  end
460
501
 
461
502
  def attribute_present?(name)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Capybara
4
4
  module Dommy
5
- VERSION = "0.15.0"
5
+ VERSION = "0.17.0"
6
6
  end
7
7
  end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Capybara
4
+ module Dommy
5
+ # Capybara waits for a page by retrying a failed query, sleeping between
6
+ # attempts until `default_max_wait_time` of real time has passed. That is
7
+ # how a real browser, which changes on its own, is waited for. A Dommy
8
+ # page with JavaScript changes only when its virtual clock moves, and the
9
+ # driver is what moves it — so for such a page the wait is a span of the
10
+ # page's own time: Capybara's 2 seconds are 2 seconds of the clock, which
11
+ # the page is let run through, timer by timer, until the query passes.
12
+ #
13
+ # Prepended to Capybara::Node::Base, this takes over `synchronize` for a
14
+ # JavaScript-enabled Dommy driver (other drivers keep Capybara's loop).
15
+ # After a failed attempt the driver lets the page move on (Driver#wait_on):
16
+ # a completion a worker handed back is delivered, or the clock moves
17
+ # straight to the page's next timer when it is due within what is left of
18
+ # the wait. A 300 ms debounce therefore costs one retry, not 300 ms. When
19
+ # nothing within the wait can change the page, the query is tried once
20
+ # more after a reload (a node may have gone stale) and then fails at once,
21
+ # not after the wait has passed in real time. Only what the clock does not
22
+ # drive — a fetch still running on a network worker, an open WebSocket or
23
+ # EventSource the app can push to — is waited for in real time, for at
24
+ # most the wait.
25
+ module VirtualWait
26
+ # A bound on the retries of one wait, against a page that keeps the
27
+ # clock busy for nothing (a timer that re-arms itself at no delay
28
+ # without being clamped).
29
+ MAX_ATTEMPTS = 10_000
30
+
31
+ def synchronize(seconds = nil, errors: nil)
32
+ return super unless driver.is_a?(Capybara::Dommy::Driver) && driver.javascript?
33
+ return yield if session.synchronized
34
+
35
+ seconds = session_options.default_max_wait_time if [nil, true].include?(seconds)
36
+ seconds = 0 unless seconds
37
+ session.synchronized = true
38
+ wait = driver.begin_wait(seconds)
39
+ real_time = Capybara::Helpers.timer(expire_in: seconds)
40
+ reloaded = false
41
+ attempts = 0
42
+ begin
43
+ yield
44
+ rescue StandardError => e
45
+ session.raise_server_error!
46
+ raise e unless catch_error?(e, errors)
47
+ raise e if (attempts += 1) > MAX_ATTEMPTS
48
+
49
+ step = driver.wait_on(wait)
50
+ if step == :moved
51
+ reloaded = false
52
+ elsif step == :outside && !real_time.expired?
53
+ sleep session_options.default_retry_interval
54
+ elsif !reloaded
55
+ reloaded = true
56
+ else
57
+ raise e
58
+ end
59
+ reload if session_options.automatic_reload
60
+ retry
61
+ ensure
62
+ session.synchronized = false
63
+ driver.end_wait
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ Capybara::Node::Base.prepend(Capybara::Dommy::VirtualWait)
@@ -9,8 +9,10 @@ require_relative "dommy/errors"
9
9
  require_relative "dommy/configuration"
10
10
  require_relative "dommy/text_extractor"
11
11
  require_relative "dommy/modal"
12
+ require_relative "dommy/app_server"
12
13
  require_relative "dommy/node"
13
14
  require_relative "dommy/driver"
15
+ require_relative "dommy/virtual_wait"
14
16
 
15
17
  module Capybara
16
18
  module Dommy
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capybara-dommy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.0
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - takahashim
@@ -29,28 +29,28 @@ dependencies:
29
29
  requirements:
30
30
  - - "~>"
31
31
  - !ruby/object:Gem::Version
32
- version: 0.15.0
32
+ version: 0.17.0
33
33
  type: :runtime
34
34
  prerelease: false
35
35
  version_requirements: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: 0.15.0
39
+ version: 0.17.0
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: dommy-rack
42
42
  requirement: !ruby/object:Gem::Requirement
43
43
  requirements:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
- version: 0.15.0
46
+ version: 0.17.0
47
47
  type: :runtime
48
48
  prerelease: false
49
49
  version_requirements: !ruby/object:Gem::Requirement
50
50
  requirements:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
- version: 0.15.0
53
+ version: 0.17.0
54
54
  description: |
55
55
  capybara-dommy is a Capybara driver backed by Dommy and dommy-rack. It drives
56
56
  Rack/Rails apps through the Capybara DSL without a real browser or JavaScript,
@@ -66,6 +66,7 @@ files:
66
66
  - README.md
67
67
  - Rakefile
68
68
  - lib/capybara/dommy.rb
69
+ - lib/capybara/dommy/app_server.rb
69
70
  - lib/capybara/dommy/configuration.rb
70
71
  - lib/capybara/dommy/driver.rb
71
72
  - lib/capybara/dommy/errors.rb
@@ -74,6 +75,7 @@ files:
74
75
  - lib/capybara/dommy/rails.rb
75
76
  - lib/capybara/dommy/text_extractor.rb
76
77
  - lib/capybara/dommy/version.rb
78
+ - lib/capybara/dommy/virtual_wait.rb
77
79
  - sig/capybara/dommy.rbs
78
80
  homepage: https://github.com/takahashim/dommy
79
81
  licenses: