Class: Capybara::Dommy::Node

Inherits:
Capybara::Driver::Node
  • Object
show all
Defined in:
lib/capybara/dommy/node.rb

Overview

Wraps a Dommy::Element as a Capybara driver node. Modeled on Capybara::RackTest::Node: field setting mutates the Dommy::Element in the live document, while link clicks and form submission delegate to the dommy-rack session (which re-reads the same document at submit time).

Constant Summary collapse

OPTION_OWNER_XPATH =
"./parent::*[self::optgroup | self::select | self::datalist]"
DISABLED_BY_FIELDSET_XPATH =
"./parent::fieldset[./@disabled] | " \
"./ancestor::*[(not(./self::legend) or ./preceding-sibling::legend)][./parent::fieldset[./@disabled]]"
DISABLEABLE_ELEMENTS =

‘disabled` only applies to form-associated elements; on anything else (e.g. a link that incorrectly carries the attribute) it has no effect.

%w[button fieldset input optgroup option select textarea].freeze
NON_READONLY_TYPES =

readonly does not apply to these input types, so they are never readonly even if the attribute is present (matches RackTest).

%w[hidden range color checkbox radio file submit image reset button].freeze

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Object



19
20
21
# File 'lib/capybara/dommy/node.rb', line 19

def [](name)
  native.get_attribute(name.to_s)
end

#all_textObject



33
34
35
# File 'lib/capybara/dommy/node.rb', line 33

def all_text
  text_extractor.all_text(native)
end

#checked?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/capybara/dommy/node.rb', line 45

def checked?
  native.respond_to?(:checked) && native.checked
end

#click(_keys = [], **_options) ⇒ Object

— Interaction —



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/capybara/dommy/node.rb', line 89

def click(_keys = [], **_options)
  if link?
    click_link_node
  elsif submits?
    submit_owning_form
  elsif checkable?
    set(!checked?)
  elsif tag_name == "label"
    click_label
  elsif (details = native.closest("details"))
    toggle_details(details)
  end
end

#disabled?Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
67
# File 'lib/capybara/dommy/node.rb', line 57

def disabled?
  return false unless DISABLEABLE_ELEMENTS.include?(tag_name)
  return true if native.has_attribute?("disabled")

  if %w[option optgroup].include?(tag_name)
    owner = native.xpath(OPTION_OWNER_XPATH).first
    owner ? self.class.new(driver, owner).disabled? : false
  else
    !native.xpath(DISABLED_BY_FIELDSET_XPATH).empty?
  end
end

#find_css(locator, **_options) ⇒ Object

— Scoped queries (for ‘within`) —



137
138
139
# File 'lib/capybara/dommy/node.rb', line 137

def find_css(locator, **_options)
  native.query_selector_all(locator).map { |element| self.class.new(driver, element) }
end

#find_xpath(locator, **_options) ⇒ Object



141
142
143
# File 'lib/capybara/dommy/node.rb', line 141

def find_xpath(locator, **_options)
  native.xpath(locator).map { |element| self.class.new(driver, element) }
end

#pathObject



79
80
81
# File 'lib/capybara/dommy/node.rb', line 79

def path
  native.path
end

#readonly?Boolean

Returns:

  • (Boolean)


73
74
75
76
77
# File 'lib/capybara/dommy/node.rb', line 73

def readonly?
  return false if input_field? && NON_READONLY_TYPES.include?(field_type)

  native.has_attribute?("readonly")
end

#select_optionObject



119
120
121
122
123
124
125
# File 'lib/capybara/dommy/node.rb', line 119

def select_option
  return if disabled?

  select_el = select_node
  deselect_all(select_el) unless select_el&.multiple
  native.selected = true
end

#selected?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/capybara/dommy/node.rb', line 49

def selected?
  native.respond_to?(:selected) && native.selected
end

#set(value, **_options) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/capybara/dommy/node.rb', line 103

def set(value, **_options)
  return if disabled? || readonly?

  if radio?
    set_radio
  elsif checkbox?
    set_checkbox(value)
  elsif range?
    set_range(value)
  elsif file?
    set_file(value)
  elsif input_field? || textarea?
    set_text_value(value)
  end
end

#style(_styles) ⇒ Object



83
84
85
# File 'lib/capybara/dommy/node.rb', line 83

def style(_styles)
  {}
end

#tag_nameObject



15
16
17
# File 'lib/capybara/dommy/node.rb', line 15

def tag_name
  native.tag_name.downcase
end

#unselect_optionObject



127
128
129
130
131
132
133
# File 'lib/capybara/dommy/node.rb', line 127

def unselect_option
  unless select_node&.multiple
    raise Capybara::UnselectNotAllowed, "Cannot unselect option from a non-multiple select box"
  end

  native.selected = false
end

#valueObject



23
24
25
26
27
28
29
30
31
# File 'lib/capybara/dommy/node.rb', line 23

def value
  if select?
    native.multiple ? native.selected_options.map(&:value) : native.value
  elsif checkable?
    native.has_attribute?("value") ? native.get_attribute("value") : "on"
  elsif native.respond_to?(:value)
    native.value
  end
end

#visible?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/capybara/dommy/node.rb', line 41

def visible?
  driver.visible?(native)
end

#visible_textObject



37
38
39
# File 'lib/capybara/dommy/node.rb', line 37

def visible_text
  text_extractor.visible_text(native)
end