Class: Capybara::RackTest::Node
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Driver::Node
|
|
Instance Chain:
|
|
Inherits: |
Capybara::Driver::Node
|
Defined in: | lib/capybara/rack_test/node.rb |
Constant Summary
-
BLOCK_ELEMENTS =
# File 'lib/capybara/rack_test/node.rb', line 9%w[p h1 h2 h3 h4 h5 h6 ol ul pre address blockquote dl div fieldset form hr noscript table].freeze
-
DISABLED_BY_FIELDSET_XPATH =
# File 'lib/capybara/rack_test/node.rb', line 312XPath.generate do |x| x.parent(:fieldset)[ XPath.attr(:disabled) ] + x.ancestor[ ~x.self(:legend) | x.preceding_sibling(:legend) ][ x.parent(:fieldset)[ x.attr(:disabled) ] ] end.to_s.freeze
-
OPTION_OWNER_XPATH =
# File 'lib/capybara/rack_test/node.rb', line 311XPath.parent(:optgroup, :select, :datalist).to_s.freeze
Node::WhitespaceNormalizer
- Included
BREAKING_SPACES, EMPTY_LINES, LEADING_SPACES, LEFT_TO_RIGHT_MARK, LINE_SEPERATOR, NON_BREAKING_SPACE, PARAGRAPH_SEPERATOR, REMOVED_CHARACTERS, RIGHT_TO_LEFT_MARK, SQUEEZED_SPACES, TRAILING_SPACES, ZERO_WIDTH_SPACE
Class Method Summary
Driver::Node
- Inherited
Instance Attribute Summary
- #checked? ⇒ Boolean readonly
- #disabled? ⇒ Boolean readonly
- #readonly? ⇒ Boolean readonly
- #selected? ⇒ Boolean readonly
- #visible? ⇒ Boolean readonly
- #checkable? ⇒ Boolean readonly private
- #link? ⇒ Boolean readonly private
- #submits? ⇒ Boolean readonly private
Driver::Node
- Inherited
Instance Method Summary
- #[](name)
- #all_text
- #click(keys = [], **options)
- #find_css(locator, **_hints)
- #find_xpath(locator, **_hints)
- #path
- #select_option
- #set(value, **options)
- #style(_styles)
- #tag_name
- #unselect_option
- #value
- #visible_text
- #attribute_is_not_blank?(attribute) ⇒ Boolean private
- #click_label private
- #deselect_options private
- #follow_link private
- #form private
-
#select_node
private
a reference to the select node if this is an option node.
- #set_checkbox(value) private
- #set_input(value) private
- #set_radio(_value) private
- #set_range(value) private
- #stale_check private
- #string_node private
- #toggle_details(details = nil) private
- #type private
Node::WhitespaceNormalizer
- Included
#normalize_spacing | Normalizes the spacing of a node’s text to be similar to what matchers might expect. |
#normalize_visible_spacing | Variant on |
Driver::Node
- Inherited
Constructor Details
This class inherits a constructor from Capybara::Driver::Node
Instance Attribute Details
#checkable? ⇒ Boolean
(readonly, private)
[ GitHub ]
#checked? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'lib/capybara/rack_test/node.rb', line 87
def checked? string_node.checked? end
#disabled? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'lib/capybara/rack_test/node.rb', line 95
def disabled? return true if string_node.disabled? if %w[option optgroup].include? tag_name find_xpath(OPTION_OWNER_XPATH)[0].disabled? else !find_xpath(DISABLED_BY_FIELDSET_XPATH).empty? end end
#link? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'lib/capybara/rack_test/node.rb', line 269
def link? tag_name == 'a' && !self[:href].nil? end
#readonly? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'lib/capybara/rack_test/node.rb', line 105
def readonly? # readonly attribute not valid on these input types return false if input_field? && %w[hidden range color checkbox radio file submit image reset button].include?(type) super end
#selected? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'lib/capybara/rack_test/node.rb', line 91
def selected? string_node.selected? end
#submits? ⇒ Boolean
(readonly, private)
[ GitHub ]
#visible? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'lib/capybara/rack_test/node.rb', line 83
def visible? string_node.visible? end
Instance Method Details
#[](name)
[ GitHub ]# File 'lib/capybara/rack_test/node.rb', line 19
def [](name) string_node[name] end
#all_text
[ GitHub ]# File 'lib/capybara/rack_test/node.rb', line 11
def all_text normalize_spacing(native.text) end
#attribute_is_not_blank?(attribute) ⇒ Boolean
(private)
# File 'lib/capybara/rack_test/node.rb', line 238
def attribute_is_not_blank?(attribute) self[attribute] && !self[attribute].empty? end
#click(keys = [], **options)
# File 'lib/capybara/rack_test/node.rb', line 61
def click(keys = [], ** ) .delete(:offset) raise ArgumentError, 'The RackTest driver does not support click options' unless keys.empty? && .empty? if link? follow_link elsif submits? associated_form = form Capybara::RackTest::Form.new(driver, associated_form).submit(self) if associated_form elsif checkable? set(!checked?) elsif tag_name == 'label' click_label elsif (details = native.xpath('.//ancestor-or-self::details').last) toggle_details(details) end end
#click_label (private)
[ GitHub ]# File 'lib/capybara/rack_test/node.rb', line 248
def click_label labelled_control = if native[:for] find_xpath("//input[@id='#{native[:for]}']") else find_xpath('.//input') end.first labelled_control.set(!labelled_control.checked?) if checkbox_or_radio?(labelled_control) end
#deselect_options (private)
[ GitHub ]# File 'lib/capybara/rack_test/node.rb', line 165
def select_node.find_xpath('.//option[@selected]').each { |node| node.native.remove_attribute('selected') } end
#find_css(locator, **_hints)
[ GitHub ]# File 'lib/capybara/rack_test/node.rb', line 120
def find_css(locator, **_hints) native.css(locator, Capybara::RackTest::CSSHandlers.new).map { |el| self.class.new(driver, el) } end
#find_xpath(locator, **_hints)
[ GitHub ]# File 'lib/capybara/rack_test/node.rb', line 116
def find_xpath(locator, **_hints) native.xpath(locator).map { |el| self.class.new(driver, el) } end
#follow_link (private)
[ GitHub ]# File 'lib/capybara/rack_test/node.rb', line 242
def follow_link method = self['data-method'] || self['data-turbo-method'] if driver. [:respect_data_method] method ||= :get driver.follow(method, self[:href].to_s) end
#form (private)
[ GitHub ]# File 'lib/capybara/rack_test/node.rb', line 182
def form if native[:form] native.xpath("//form[@id='#{native[:form]}']") else native.ancestors('form') end.first end
#path
[ GitHub ]# File 'lib/capybara/rack_test/node.rb', line 112
def path native.path end
#select_node (private)
a reference to the select node if this is an option node
# File 'lib/capybara/rack_test/node.rb', line 174
def select_node find_xpath('./ancestor::select[1]').first end
#select_option
[ GitHub ]# File 'lib/capybara/rack_test/node.rb', line 48
def select_option return if disabled? unless select_node.multiple? native['selected'] = 'selected' end
#set(value, **options)
[ GitHub ]# File 'lib/capybara/rack_test/node.rb', line 31
def set(value, ** ) return if disabled? || readonly? warn "Options passed to Node#set but the RackTest driver doesn't support any - ignoring" unless .empty? if value.is_a?(Array) && !multiple? raise TypeError, "Value cannot be an Array when 'multiple' attribute is not present. Not a #{value.class}" end if radio? then set_radio(value) elsif checkbox? then set_checkbox(value) elsif range? then set_range(value) elsif input_field? then set_input(value) elsif textarea? then native['_capybara_raw_value'] = value.to_s end end
#set_checkbox(value) (private)
[ GitHub ]#set_input(value) (private)
[ GitHub ]# File 'lib/capybara/rack_test/node.rb', line 212
def set_input(value) # rubocop:disable Naming/AccessorMethodName if text_or_password? && attribute_is_not_blank?(:maxlength) # Browser behavior for maxlength="0" is inconsistent, so we stick with # Firefox, allowing no input value = value.to_s[0...self[:maxlength].to_i] end if value.is_a?(Array) # Assert multiple attribute is present value.each do |val| new_native = native.clone new_native.remove_attribute('value') native.add_next_sibling(new_native) new_native['value'] = val.to_s end native.remove else value.to_s.tap do |set_value| if set_value.end_with?("\n") && form&.css('input, textarea')&.count == 1 native['value'] = set_value.to_s.chop Capybara::RackTest::Form.new(driver, form).submit(self) else native['value'] = set_value end end end end
#set_radio(_value) (private)
[ GitHub ]# File 'lib/capybara/rack_test/node.rb', line 190
def set_radio(_value) # rubocop:disable Naming/AccessorMethodName other_radios_xpath = XPath.generate { |xp| xp.anywhere(:input)[xp.attr(:name) == self[:name]] }.to_s driver.dom.xpath(other_radios_xpath).each { |node| node.remove_attribute('checked') } native['checked'] = 'checked' end
#set_range(value) (private)
[ GitHub ]# File 'lib/capybara/rack_test/node.rb', line 204
def set_range(value) # rubocop:disable Naming/AccessorMethodName min, max, step = (native['min'] || 0).to_f, (native['max'] || 100).to_f, (native['step'] || 1).to_f value = value.to_f value = value.clamp(min, max) value = (((value - min) / step).round * step) + min native['value'] = value.clamp(min, max) end
#stale_check (private)
# File 'lib/capybara/rack_test/node.rb', line 161
def stale_check raise Capybara::RackTest::Errors::StaleElementReferenceError unless native.document == driver.dom end
#string_node (private)
[ GitHub ]#style(_styles)
# File 'lib/capybara/rack_test/node.rb', line 23
def style(_styles) raise NotImplementedError, 'The rack_test driver does not process CSS' end
#tag_name
[ GitHub ]# File 'lib/capybara/rack_test/node.rb', line 79
def tag_name native.node_name end
#toggle_details(details = nil) (private)
[ GitHub ]# File 'lib/capybara/rack_test/node.rb', line 258
def toggle_details(details = nil) details ||= native.xpath('.//ancestor-or-self::details').last return unless details if details.has_attribute?('open') details.remove_attribute('open') else details.set_attribute('open', 'open') end end
#type (private)
[ GitHub ]# File 'lib/capybara/rack_test/node.rb', line 178
def type native[:type] end
#unselect_option
# File 'lib/capybara/rack_test/node.rb', line 55
def unselect_option raise Capybara::UnselectNotAllowed, 'Cannot unselect option from single select box.' unless select_node.multiple? native.remove_attribute('selected') end
#value
[ GitHub ]# File 'lib/capybara/rack_test/node.rb', line 27
def value string_node.value end
#visible_text
[ GitHub ]# File 'lib/capybara/rack_test/node.rb', line 15
def visible_text normalize_visible_spacing(displayed_text) end