123456789_123456789_123456789_123456789_123456789_

Class: Selenium::WebDriver::AccessibilityNode

Relationships & Source Files
Namespace Children
Modules:
Inherits: Object
Defined in: rb/lib/selenium/webdriver/common/accessibility_snapshot.rb

Overview

One node of an accessibility snapshot (ADR 18205), as returned by Driver#accessibility_snapshot and Element#accessibility_snapshot.

The tree is computed in the page from the DOM following WAI-ARIA, html-aam and accname. It is not the browser's own accessibility tree and can differ from it at the edges.

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(role:, name:, description: '', value: nil, url: nil, states: {}, element: nil, children: []) ⇒ AccessibilityNode

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/accessibility_snapshot.rb', line 66

def initialize(role:, name:, description: '', value: nil, url: nil, states: {}, element: nil, children: [])
  @role = role
  @name = name
  @description = description
  @value = value
  @url = url
  @states = states
  @element = element
  @children = children
end

Class Method Details

.from_wire(data, elements, &frame_children)

Builds the node tree from the atom's wire form: the parsed JSON of the tree, in which #element is an index into elements and, on a frame node, frame an index the block resolves to the frame's own tree (its document, or nothing when it cannot be reached).

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/accessibility_snapshot.rb', line 103

def self.from_wire(data, elements, &frame_children)
  children = if data.key?('frame')
               yield data['frame']
             else
               data.fetch('children').map { |child| from_wire(child, elements, &frame_children) }
             end
  element_index = data['element']
  new(
    role: data.fetch('role'),
    name: data.fetch('name'),
    description: data.fetch('description', ''),
    value: data['value'],
    url: data['url'],
    states: data.fetch('states').dup,
    element: element_index && elements.fetch(element_index),
    children: children
  )
end

Instance Attribute Details

#children ⇒ Array<AccessibilityNode> (rw)

Returns:

  • (Array<AccessibilityNode>) —

    the child nodes, [] for leaves

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/accessibility_snapshot.rb', line 64

attr_accessor :children

#description ⇒ String (readonly)

Returns:

  • (String) —

    the computed accessible description, '' when there is none

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/accessibility_snapshot.rb', line 43

attr_reader :description

#element ⇒ Element? (readonly)

Returns:

  • (Element, nil) —

    the element the node is backed by, usable for further commands, or nil for text leaves. An element inside a child frame can be used through classic commands only once the driver has switched to that frame.

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/accessibility_snapshot.rb', line 61

attr_reader :element

#name ⇒ String (readonly)

Returns:

  • (String) —

    the computed accessible name, '' when there is none

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/accessibility_snapshot.rb', line 40

attr_reader :name

#role ⇒ String (readonly)

Returns:

  • (String) —

    the WAI-ARIA role token; 'text' for text leaves and 'iframe' for frames, under which the frame's own tree is attached

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/accessibility_snapshot.rb', line 37

attr_reader :role

#states ⇒ Hash{String => Boolean, Integer, String} (readonly)

Returns:

  • (Hash{String => Boolean, Integer, String}) —

    checked, pressed, expanded, selected, level, disabled and focused, each present only when it applies; new keys may be added in any release, so ignore keys you do not recognise

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/accessibility_snapshot.rb', line 56

attr_reader :states

#url ⇒ String? (readonly)

Returns:

  • (String, nil) —

    the resolved href of a link whose element has one; nil for every other node. Not rendered in the text form.

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/accessibility_snapshot.rb', line 51

attr_reader :url

#value ⇒ String? (readonly)

Returns:

  • (String, nil) —

    the current value for textbox, searchbox, combobox and range roles; nil where the role has none. A password field's value is masked.

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/accessibility_snapshot.rb', line 47

attr_reader :value

Instance Method Details

#line (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/accessibility_snapshot.rb', line 167

def line
  return "- text: #{name}" if role == 'text'

  out = "- #{role}"
  out += " #{quote(name)}" unless name.empty?
  out += state_tokens
  out += ": #{one_line(value)}" if value && !value.empty?
  out
end

#one_line(text) (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/accessibility_snapshot.rb', line 194

def one_line(text)
  text.gsub('\\') { '\\\\' }.gsub("\r") { '\\r' }.gsub("\n") { '\\n' }
end

#quote(text) (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/accessibility_snapshot.rb', line 190

def quote(text)
  "\"#{text.gsub('\\') { '\\\\' }.gsub('"') { '\\"' }}\""
end

#state_tokens (private)

A true state bare, a false one left out, any other value spelled.

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/accessibility_snapshot.rb', line 178

def state_tokens
  tokens = STATE_ORDER.filter_map do |key|
    next unless states.key?(key)

    state = states[key]
    next if state == false

    state == true ? " [#{key}]" : " [#{key}=#{state}]"
  end
  tokens.join
end

#to_text ⇒ String

Renders the tree as text, one node per line.

The line grammar is - role "name" [state] [state=value]: value, children indented two spaces, text leaves written as - text: content. An empty name is left out, as is an absent or empty value; a true state is written bare, a false one left out, any other value spelled. A line break or backslash in a value is escaped so a node never spans two lines. The description and element references are not rendered.

Returns:

  • (String) —

    the text form, without a trailing newline

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/accessibility_snapshot.rb', line 90

def to_text
  lines('').join("\n")
end