capybara-dommy 0.13.0 → 0.14.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: 79e1b015c05f0dea6b8e6d1081f023347ba49f5d6fa5ff0a1bad242116948596
4
- data.tar.gz: a07065688fbaf41ed4661f5d3872021342310ec018a4d82d7001a1245288b679
3
+ metadata.gz: dc52d93866c788bf33f9ac11fceea40cab43bce8bad33c4e253c33f8c11ec3f8
4
+ data.tar.gz: 89d8a40cc27b5c24782cc6100c2d73d46b236d90f8db819fe3ff945ea36da5a6
5
5
  SHA512:
6
- metadata.gz: 609dbbccf830c63bf7122c7410380695ef7ca03c50eef1a623029585feabea5e3d5f5ee91811c33dcdae2ccdf13e4e85d3945689d20c9eec36e208eeec030532
7
- data.tar.gz: 47f7ec672ceda6bb2c9cfa35b104ed9238f7479459270689902b0e753da9e03c456adc5b0f46399aba8b1e5c494d648934edd415030b250509a35ea1b2b28582
6
+ metadata.gz: 935125dffe09162effdfee36f5b8c897c5b7f9b83eb5f9d84cf27049ec455ca4bfe5d6b330ef2c1445c02bb178961e29c89b143133f7264bc445c7c5736c4e05
7
+ data.tar.gz: 506cab775b1c1425fa83488b35754b0ef331aca13cf6e7e01f8fe57524281cdcf1713e5f24527712bb533a521ed1785e4c98177945d75a57ef2ff963d24b72d7
data/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.14.0 — 2026-10-01
4
+
5
+ ### Added
6
+
7
+ - `right_click` and `double_click`, which fire `contextmenu` and `dblclick` after the full pointer and mouse sequence under JavaScript. Without JavaScript `double_click` is a `click`.
8
+ - `shadow_root` returns the element's shadow tree as a node, so `find(...).shadow_root.find(...)` searches inside it.
9
+ - `execute_script` / `evaluate_script` pass their arguments to the script, a Capybara node arriving as its element.
10
+ - A frame with `srcdoc` loads that document, at `about:srcdoc`.
11
+ - `save_screenshot` writes a blank PNG at the path and the page's HTML and visible text next to it, so Rails' failure screenshot no longer raises and hides the real failure.
12
+
13
+ ### Changed
14
+
15
+ - `hover` under JavaScript fires `mouseover` and `mouseenter` on the element (and `mouseout` / `mouseleave` on the one it leaves), not only the `:hover` state.
16
+ - `send_keys` without JavaScript applies `:backspace`, `:delete`, `:home` and `:end` at the caret, and `:enter` inserts a newline in a textarea and submits an input's form.
17
+
3
18
  ## 0.13.0 — 2026-09-23
4
19
 
5
20
  ### Added
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "fileutils"
4
+
3
5
  module Capybara
4
6
  module Dommy
5
7
  # A Capybara driver backed by Dommy::Rack::Session. Implements the
@@ -11,6 +13,11 @@ module Capybara
11
13
  class Driver < Capybara::Driver::Base
12
14
  VISIBILITY_MODES = %i[all html none].freeze
13
15
 
16
+ # A 1x1 transparent PNG. There are no pixels to paint, but the path a
17
+ # screenshot is asked to save must still hold a valid image: Rails'
18
+ # screenshot helper reads it back for its inline / artifact output.
19
+ BLANK_PNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==".unpack1("m").freeze
20
+
14
21
  attr_reader :app, :visibility
15
22
 
16
23
  # --- Deterministic-time seam (used by JS runtimes) ---
@@ -157,6 +164,22 @@ module Capybara
157
164
  rack_session.headers || {}
158
165
  end
159
166
 
167
+ # There is no rendering surface to capture, so a "screenshot" saves the
168
+ # page itself: the serialized HTML and its visible text next to `path`,
169
+ # plus a valid blank image at `path`. Rails' take_failed_screenshot calls
170
+ # this for every failed system test, and letting it raise (the
171
+ # Driver::Base default) would mask the real failure.
172
+ def save_screenshot(path, **_options)
173
+ path = path.to_s
174
+ dir = ::File.dirname(path)
175
+ base = ::File.basename(path, ".*")
176
+ ::FileUtils.mkdir_p(dir)
177
+ ::File.binwrite(path, BLANK_PNG)
178
+ ::File.write(::File.join(dir, "#{base}.html"), html.to_s)
179
+ ::File.write(::File.join(dir, "#{base}.txt"), screenshot_text)
180
+ path
181
+ end
182
+
160
183
  # --- Query (returns Capybara::Dommy::Node arrays) ---
161
184
 
162
185
  def find_css(query, **_options)
@@ -248,18 +271,21 @@ module Capybara
248
271
  # When raise_on_unsupported_js is false these become no-ops, so tests
249
272
  # that incidentally call them don't fail.
250
273
 
274
+ # Arguments become the script's `arguments`; a Capybara node argument
275
+ # crosses as its Dommy element (a JS proxy), so
276
+ # `execute_script("arguments[0].scrollIntoView()", node)` works. A runtime
277
+ # that cannot pass arguments raises (see Session#execute_script), rather
278
+ # than silently dropping them.
251
279
  def execute_script(script, *args)
252
280
  return unsupported_js!("execute_script") unless @javascript
253
- raise ArgumentError, "script arguments are not supported" unless args.empty?
254
281
 
255
- rack_session.execute_script(script)
282
+ rack_session.execute_script(script, *unwrap_script_args(args))
256
283
  end
257
284
 
258
285
  def evaluate_script(script, *args)
259
286
  return unsupported_js!("evaluate_script") unless @javascript
260
- raise ArgumentError, "script arguments are not supported" unless args.empty?
261
287
 
262
- rack_session.evaluate_script(script)
288
+ rack_session.evaluate_script(script, *unwrap_script_args(args))
263
289
  end
264
290
 
265
291
  def evaluate_async_script(_script, *_args)
@@ -309,9 +335,15 @@ module Capybara
309
335
  @frame_stack ||= []
310
336
  end
311
337
 
312
- # Fetch an iframe's document, resolving its src against the enclosing
313
- # frame's URL so nested frames with relative srcs load correctly.
338
+ # A frame's document: its `srcdoc` when present (Dommy builds it from
339
+ # the attribute, so there is nothing to fetch), else its `src` resolved
340
+ # against the enclosing frame's URL so nested relative srcs load
341
+ # correctly.
314
342
  def load_frame(iframe_element)
343
+ if (doc = srcdoc_document(iframe_element))
344
+ return {document: doc, url: frame_url}
345
+ end
346
+
315
347
  src = iframe_element.get_attribute("src").to_s
316
348
  raise Capybara::Dommy::Error, "iframe has no src" if src.empty?
317
349
 
@@ -323,6 +355,27 @@ module Capybara
323
355
  {document: doc, url: url}
324
356
  end
325
357
 
358
+ # The document Dommy already built for a `srcdoc` frame (its URL is
359
+ # about:srcdoc, with the base URL inherited from the enclosing
360
+ # document); nil when the frame has no `srcdoc`.
361
+ def srcdoc_document(iframe_element)
362
+ srcdoc = iframe_element.get_attribute("srcdoc")
363
+ return nil if srcdoc.nil?
364
+
365
+ return iframe_element.content_document if iframe_element.respond_to?(:content_document) && iframe_element.content_document
366
+
367
+ Dommy.parse(srcdoc).document
368
+ end
369
+
370
+ def screenshot_text
371
+ doc = document
372
+ return "" unless doc
373
+
374
+ TextExtractor.new(self).visible_text(doc.body)
375
+ rescue StandardError
376
+ ""
377
+ end
378
+
326
379
  # Sequential focus navigation: elements with a positive tabindex first
327
380
  # (ascending, document order within a value), then the remaining
328
381
  # focusables in document order. The page's tab cycle starts over when
@@ -355,7 +408,7 @@ module Capybara
355
408
  # standalone driver (no owning Capybara session) still works.
356
409
  def effective_host
357
410
  options = owning_session_options
358
- (options && (options.app_host || options.default_host)) || @session_options[:default_host]
411
+ options&.app_host || options&.default_host || @session_options[:default_host]
359
412
  end
360
413
 
361
414
  def owning_session_options
@@ -368,6 +421,19 @@ module Capybara
368
421
  (elements || []).map { |element| Node.new(self, element) }
369
422
  end
370
423
 
424
+ # A script argument: a Capybara node becomes the Dommy element it wraps
425
+ # (so it crosses to JS as a proxy), anything else passes through. Arrays
426
+ # are mapped so a list of nodes works too.
427
+ def unwrap_script_args(args)
428
+ args.map do |arg|
429
+ case arg
430
+ when Node then arg.native
431
+ when Array then unwrap_script_args(arg)
432
+ else arg
433
+ end
434
+ end
435
+ end
436
+
371
437
  def unsupported_js!(name)
372
438
  return nil unless @raise_on_unsupported_js
373
439
 
@@ -70,16 +70,17 @@ module Capybara
70
70
  # `disabled` only applies to form-associated elements; on anything else
71
71
  # (e.g. a link that incorrectly carries the attribute) it has no effect.
72
72
  DISABLEABLE_ELEMENTS = %w[button fieldset input optgroup option select textarea].freeze
73
+ OPTION_ELEMENTS = %w[option optgroup].freeze
73
74
 
74
75
  def disabled?
75
76
  return false unless DISABLEABLE_ELEMENTS.include?(tag_name)
76
77
  return true if native.has_attribute?("disabled")
77
78
 
78
- if %w[option optgroup].include?(tag_name)
79
+ if OPTION_ELEMENTS.include?(tag_name)
79
80
  owner = native.xpath(OPTION_OWNER_XPATH).first
80
81
  owner ? self.class.new(driver, owner).disabled? : false
81
82
  else
82
- !native.xpath(DISABLED_BY_FIELDSET_XPATH).empty?
83
+ native.xpath(DISABLED_BY_FIELDSET_XPATH).any?
83
84
  end
84
85
  end
85
86
 
@@ -115,7 +116,7 @@ module Capybara
115
116
 
116
117
  return unless native.respond_to?(:value=)
117
118
 
118
- state = {chars: native.value.to_s.chars, caret: native.value.to_s.length, shift: false}
119
+ state = {chars: native.value.to_s.chars, caret: native.value.to_s.length, shift: false, submit: false}
119
120
  args.each do |arg|
120
121
  if arg.is_a?(Array)
121
122
  # A chord like [:shift, 'o'] holds its modifiers only for the
@@ -129,6 +130,9 @@ module Capybara
129
130
  end
130
131
  native.focus if native.respond_to?(:focus)
131
132
  native.value = state[:chars].join
133
+ # Enter's default action on an input is its form's implicit submission;
134
+ # a textarea received a newline instead (see #apply_key).
135
+ submit_owning_form if state[:submit]
132
136
  end
133
137
 
134
138
  # Computed styles for Capybara's matches_style? / style: filters,
@@ -157,6 +161,27 @@ module Capybara
157
161
  end
158
162
  end
159
163
 
164
+ # A secondary-button click. Under JavaScript the full pointer/mouse
165
+ # sequence plus contextmenu runs through the driver; without it, only the
166
+ # virtual :hover state changes (no handlers exist to observe the event).
167
+ def right_click
168
+ if driver.javascript?
169
+ ::Dommy::Interaction::EventSynthesis.right_click(native)
170
+ driver.drain_js
171
+ end
172
+ nil
173
+ end
174
+
175
+ def double_click
176
+ if driver.javascript?
177
+ ::Dommy::Interaction::EventSynthesis.double_click(native)
178
+ driver.drain_js
179
+ else
180
+ click
181
+ end
182
+ nil
183
+ end
184
+
160
185
  def set(value, **_options)
161
186
  return if disabled? || readonly?
162
187
 
@@ -190,15 +215,32 @@ module Capybara
190
215
  native.selected = false
191
216
  end
192
217
 
193
- # Move the (virtual) pointer over this element: :hover rules and
194
- # `matches(":hover")` then apply to it and its ancestors. No
195
- # mouseover/mouseout events are dispatched (nothing observes them
196
- # without JavaScript).
218
+ # Move the pointer over this element: the virtual :hover state moves (so
219
+ # :hover rules and `matches(":hover")` apply to it and its ancestors), and
220
+ # under JavaScript the mouseover/mouseenter sequence is dispatched so
221
+ # Stimulus/framework hover handlers run. The previously hovered element
222
+ # (if any) is left first, so its mouseout/mouseleave fire.
197
223
  def hover
224
+ previous = previous_hovered
225
+ if driver.javascript?
226
+ ::Dommy::Interaction::EventSynthesis.unhover(previous, to: native) if previous && !previous.equal?(native)
227
+ ::Dommy::Interaction::EventSynthesis.hover(native, from: previous)
228
+ end
198
229
  native.owner_document.__internal_set_hovered_element__(native)
230
+ driver.drain_js if driver.javascript?
199
231
  nil
200
232
  end
201
233
 
234
+ # The shadow root this element hosts (`attachShadow`), as a Capybara node
235
+ # scoped to that tree — Capybara's `shadow_root`/`within` support. nil
236
+ # when the element has no shadow root.
237
+ def shadow_root
238
+ return nil unless native.respond_to?(:shadow_root)
239
+
240
+ root = native.shadow_root
241
+ root && self.class.new(driver, root)
242
+ end
243
+
202
244
  # --- Scoped queries (for `within`) ---
203
245
 
204
246
  def find_css(locator, **_options)
@@ -233,6 +275,15 @@ module Capybara
233
275
 
234
276
  private
235
277
 
278
+ # The element currently hovering, if any (nil when the pointer is off
279
+ # any element). Used to fire its mouseleave before a new hover.
280
+ def previous_hovered
281
+ doc = native.owner_document
282
+ return nil unless doc.respond_to?(:__internal_hovered_element__)
283
+
284
+ doc.__internal_hovered_element__
285
+ end
286
+
236
287
  def apply_key(state, key)
237
288
  case key
238
289
  when String
@@ -243,10 +294,30 @@ module Capybara
243
294
  when :space
244
295
  state[:chars].insert(state[:caret], " ")
245
296
  state[:caret] += 1
297
+ when :backspace
298
+ return if state[:caret].zero?
299
+
300
+ state[:chars].delete_at(state[:caret] - 1)
301
+ state[:caret] -= 1
302
+ when :delete
303
+ state[:chars].delete_at(state[:caret]) if state[:caret] < state[:chars].length
246
304
  when :left
247
305
  state[:caret] = [state[:caret] - 1, 0].max
248
306
  when :right
249
307
  state[:caret] = [state[:caret] + 1, state[:chars].length].min
308
+ when :home
309
+ state[:caret] = 0
310
+ when :end
311
+ state[:caret] = state[:chars].length
312
+ when :enter
313
+ # Newline in a textarea; elsewhere (an input) the form's implicit
314
+ # submission runs once the value is committed.
315
+ if textarea?
316
+ state[:chars].insert(state[:caret], "\n")
317
+ state[:caret] += 1
318
+ elsif input_field?
319
+ state[:submit] = true
320
+ end
250
321
  when :shift
251
322
  state[:shift] = true
252
323
  end
@@ -32,7 +32,7 @@ module Capybara
32
32
  private
33
33
 
34
34
  def displayed_text(element)
35
- element.child_nodes.map do |child|
35
+ element.child_nodes.filter_map do |child|
36
36
  if text_node?(child)
37
37
  # Whitespace inside a text node (incl. newlines) collapses to
38
38
  # spaces; only block boundaries introduce line breaks.
@@ -42,8 +42,6 @@ module Capybara
42
42
 
43
43
  inner = displayed_text(child)
44
44
  block_element?(child) ? "\n#{inner}\n" : inner
45
- else
46
- ""
47
45
  end
48
46
  end.join
49
47
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Capybara
4
4
  module Dommy
5
- VERSION = "0.13.0"
5
+ VERSION = "0.14.0"
6
6
  end
7
7
  end
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.13.0
4
+ version: 0.14.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.13.0
32
+ version: 0.14.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.13.0
39
+ version: 0.14.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.13.0
46
+ version: 0.14.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.13.0
53
+ version: 0.14.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,
@@ -95,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  requirements: []
98
- rubygems_version: 3.6.9
98
+ rubygems_version: 4.0.20
99
99
  specification_version: 4
100
100
  summary: A Dommy-backed Capybara driver
101
101
  test_files: []