[rb] Implement accessibility snapshots (ADR 18205)

Aperta
#18,347 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
javascript, ruby

Direzione di ricerca

Start with ADR 18205 and the reference behavior in issues #18307 and #18308, then inspect the Ruby bridge, snapshotter, atom build target, and RBS files named here. Implement the Ruby API and cover it with the listed unit and integration specs in Classic and BiDi runs; done means Steep and the specified snapshot cases pass. This is a broad feature spanning bridges, generated protocol calls, frames, and browser tests.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

C-rb I-enhancement

Part of #18345, the ADR 18205 tracker. This is the Ruby plan we will implement if people are happy with it. Please comment here before the PR goes up.

Python (#18307) and JavaScript (#18308) set the reference behaviour, listed in the tracker. This issue only covers how Ruby does it.

Public API

Ruby has no role interfaces, so, as the record says, the method goes on Driver and Element directly, next to #screenshot:

tree = driver.accessibility_snapshot
puts tree.to_text

form = driver.find_element(id: 'login').accessibility_snapshot(interesting_only: false)
  • Signature: accessibility_snapshot(interesting_only: true) -> AccessibilityNode. It takes a keyword argument rather than an options object, which matches print_page(**options) and the rest of the Ruby API.
  • Raises Error::StaleElementReferenceError for a stale or detached root.
Selenium::WebDriver::AccessibilityNode

This is an immutable value. It uses Data.define (the gem needs Ruby >= 3.3), with keyword construction and frozen collections:

AccessibilityNode = Data.define(:role, :name, :description, :value, :url, :states, :element, :children) do
  def each_node(&) = ...     # self, then descendants, depth-first; an Enumerator without a block
  def to_text = ...          # ADR §1 text form
  def checked? = ...         # and pressed?, expanded?, selected?, disabled?, focused?
  def level = states[:level]
  alias_method :to_s, :to_text
end
  • role, name and description are String. value and url are String or nil. element is an Element or nil. children is a frozen Array.
  • states is a frozen Hash with symbol keys, as the rest of the Ruby API uses: {checked: :mixed, level: 2, disabled: true}. "mixed" becomes :mixed, so node.states[:checked] == :mixed reads naturally. Unknown future keys come through as symbols too, and callers ignore them, as the record's compatibility rule says.
  • The predicates are Ruby extras on top of states. Each one is true only when the state is true, so checked? is false for :mixed, and callers who need the tri-state read states[:checked]. each_node returns an Enumerator when called without a block, so the tree can be searched with the whole Enumerable API. It is an enumerator rather than include Enumerable on the node, because Enumerable#to_h would hide Data#to_h:
driver.accessibility_snapshot.each_node
      .find { |n| n.role == 'button' && n.name == 'Submit' }
      &.element&.click

headings = driver.accessibility_snapshot.each_node.select { _1.role == 'heading' }.map { [_1.level, _1.name] }

These extras are additive under the record's compatibility rule. They can be dropped if we want strict parity with Python and JavaScript.

Internals

The bridges choose the path, so there is no if bidi? in the driver:

  • Remote::Bridge#accessibility_snapshot(root, interesting_only:) is the Classic path. It calls execute_script with the atom source. For frames it calls switch_to_frame(iframe_element), then switch_to_parent_frame in an ensure.
  • Remote::BiDiBridge#accessibility_snapshot is the BiDi path. It uses the generated BiDi::Protocol::Script#call_function and BiDi::Protocol::BrowsingContext#get_tree over the bridge's existing connection (as browsing_context/web_extension already do). It does not use driver.bidi/send_cmd, which ADR 17670 is moving away from. If the current browsing context id cannot be found, it falls back to super.
  • AccessibilitySnapshotter (@api private) holds the shared code: building nodes from the wire (with a block for frame children, AccessibilityNode.from_wire(data, elements) { |frame| ... }), mapping element indexes to Element, and the stitching and unreachable-frame rules. Frames that cannot be reached log WebDriver.logger.debug(..., id: :accessibility_snapshot) and become childless iframe nodes.
  • Atom: a target in rb/lib/selenium/webdriver/atoms/BUILD.bazel copies //javascript/atoms/fragments:accessibility-snapshot-wire-typescript.js into the gem, read once with Atoms#read_atom.
  • RBS: rb/sig/lib/selenium/webdriver/common/accessibility_node.rbs, plus additions to driver.rbs, element.rbs, remote/bridge.rbs and remote/bidi_bridge.rbs. Steep must pass.
  • YARD on the public methods describes the output as computed and not the browser's own tree.

Tests

  • rb/spec/unit/selenium/webdriver/common/accessibility_node_spec.rb (no browser): checks to_text against every corpus .json/.txt pair, plus escaping, state order, symbol keys, the predicates and each_node order.
  • rb/spec/integration/selenium/webdriver/accessibility_snapshot_spec.rb: iterates over index.json, pruned and unpruned, preferring .webkit expectations on Safari. It runs in both Classic and WEBDRIVER_BIDI runs, so both bridges are covered. It also ports the other cases from the Python suite (switched frame root, context restored, frames stitched, subtree in a frame, stale and detached roots, usable element references, snapshot is a copy). Any browser exceptions use pending_if/skip_if with a reason:.

Not changing

Nothing is deprecated, because this replaces no existing call. Using driver.bidi directly is no longer needed for this feature.


Generated by Claude Code

Lingua principale
Java
Stelle
34.6k
Fork
8.7k
Merge medio
1g 16h
PR unite (30g)
107

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di SeleniumHQ/selenium

Tutte le issue di SeleniumHQ/selenium

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.