Class: Capybara::Node::Simple

Inherits:
Object
  • Object
show all
Includes:
Minitest::Expectations, DocumentMatchers, Finders, Matchers
Defined in:
lib/capybara/node/simple.rb,
lib/capybara/minitest/spec.rb

Overview

A Simple is a simpler version of Base which includes only Finders and Matchers and does not include Actions. This type of node is returned when using Capybara.string.

It is useful in that it does not require a session, an application or a driver, but can still use Capybara's finders and matchers on any string that contains HTML.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Minitest::Expectations

#must_have_all_of_selectors, #must_have_ancestor, #must_have_any_of_selectors, #must_have_button, #must_have_checked_field, #must_have_content, #must_have_css, #must_have_current_path, #must_have_element, #must_have_field, #must_have_link, #must_have_none_of_selectors, #must_have_select, #must_have_selector, #must_have_sibling, #must_have_style, #must_have_table, #must_have_text, #must_have_title, #must_have_unchecked_field, #must_have_xpath, #must_match_style, #wont_have_button, #wont_have_checked_field, #wont_have_content, #wont_have_css, #wont_have_current_path, #wont_have_element, #wont_have_field, #wont_have_link, #wont_have_select, #wont_have_selector, #wont_have_table, #wont_have_text, #wont_have_title, #wont_have_unchecked_field, #wont_have_xpath

Methods included from DocumentMatchers

#assert_no_title, #assert_title, #has_no_title?, #has_title?

Methods included from Matchers

#assert_all_of_selectors, #assert_ancestor, #assert_any_of_selectors, #assert_matches_selector, #assert_matches_style, #assert_no_ancestor, #assert_no_selector, #assert_no_sibling, #assert_no_text, #assert_none_of_selectors, #assert_not_matches_selector, #assert_selector, #assert_sibling, #assert_style, #assert_text, #has_ancestor?, #has_button?, #has_checked_field?, #has_css?, #has_element?, #has_field?, #has_link?, #has_no_ancestor?, #has_no_button?, #has_no_checked_field?, #has_no_css?, #has_no_element?, #has_no_field?, #has_no_link?, #has_no_select?, #has_no_selector?, #has_no_sibling?, #has_no_table?, #has_no_text?, #has_no_unchecked_field?, #has_no_xpath?, #has_select?, #has_selector?, #has_sibling?, #has_style?, #has_table?, #has_text?, #has_unchecked_field?, #has_xpath?, #matches_css?, #matches_selector?, #matches_style?, #matches_xpath?, #not_matches_css?, #not_matches_selector?, #not_matches_xpath?

Methods included from Finders

#all, #ancestor, #find, #find_button, #find_by_id, #find_field, #find_link, #first, #sibling

Constructor Details

#initialize(native) ⇒ Simple

Returns a new instance of Simple.



22
23
24
25
# File 'lib/capybara/node/simple.rb', line 22

def initialize(native)
  native = Capybara::HTML(native) if native.is_a?(String)
  @native = native
end

Instance Attribute Details

#nativeObject (readonly)

Returns the value of attribute native.



20
21
22
# File 'lib/capybara/node/simple.rb', line 20

def native
  @native
end

Instance Method Details

#==(other) ⇒ Object



194
195
196
# File 'lib/capybara/node/simple.rb', line 194

def ==(other)
  eql?(other) || (other.respond_to?(:native) && native == other.native)
end

#[](name) ⇒ String

Retrieve the given attribute

element[:title] # => HTML title attribute

Parameters:

  • name (Symbol)

    The attribute name to retrieve

Returns:

  • (String)

    The value of the attribute



45
46
47
48
49
50
51
52
53
54
# File 'lib/capybara/node/simple.rb', line 45

def [](name)
  attr_name = name.to_s
  if attr_name == 'value'
    value
  elsif (tag_name == 'input') && (native[:type] == 'checkbox') && (attr_name == 'checked')
    native['checked'] == 'checked'
  else
    native[attr_name]
  end
end

#allow_reload!Object



159
160
161
# File 'lib/capybara/node/simple.rb', line 159

def allow_reload!(*)
  # no op
end

#checked?Boolean

Whether or not the element is checked.

Returns:

  • (Boolean)

    Whether the element is checked



123
124
125
# File 'lib/capybara/node/simple.rb', line 123

def checked?
  native.has_attribute?('checked')
end

#disabled?Boolean

Whether or not the element is disabled.

Returns:

  • (Boolean)

    Whether the element is disabled



132
133
134
135
# File 'lib/capybara/node/simple.rb', line 132

def disabled?
  native.has_attribute?('disabled') &&
    %w[button input select textarea optgroup option menuitem fieldset].include?(tag_name)
end

#find_css(css, **_options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



175
176
177
# File 'lib/capybara/node/simple.rb', line 175

def find_css(css, **_options)
  native.css(css)
end

#find_xpath(xpath, **_options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



180
181
182
# File 'lib/capybara/node/simple.rb', line 180

def find_xpath(xpath, **_options)
  native.xpath(xpath)
end

#initial_cacheObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



190
191
192
# File 'lib/capybara/node/simple.rb', line 190

def initial_cache
  {}
end

#inspectObject



170
171
172
# File 'lib/capybara/node/simple.rb', line 170

def inspect
  %(#<Capybara::Node::Simple tag="#{tag_name}" path="#{path}">)
end

#multiple?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/capybara/node/simple.rb', line 147

def multiple?
  native.has_attribute?('multiple')
end

#pathString

An XPath expression describing where on the page the element can be found

Returns:

  • (String)

    An XPath expression



70
71
72
# File 'lib/capybara/node/simple.rb', line 70

def path
  native.path
end

#readonly?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/capybara/node/simple.rb', line 151

def readonly?
  native.has_attribute?('readonly')
end

#selected?Boolean

Whether or not the element is selected.

Returns:

  • (Boolean)

    Whether the element is selected



143
144
145
# File 'lib/capybara/node/simple.rb', line 143

def selected?
  native.has_attribute?('selected')
end

#session_optionsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



185
186
187
# File 'lib/capybara/node/simple.rb', line 185

def session_options
  Capybara.session_options
end

#synchronize(_seconds = nil) ⇒ Object



155
156
157
# File 'lib/capybara/node/simple.rb', line 155

def synchronize(_seconds = nil)
  yield # simple nodes don't need to wait
end

#tag_nameString

Returns The tag name of the element.

Returns:

  • (String)

    The tag name of the element



60
61
62
# File 'lib/capybara/node/simple.rb', line 60

def tag_name
  native.node_name
end

#text(_type = nil, normalize_ws: false) ⇒ String

Returns The text of the element.

Returns:

  • (String)

    The text of the element



31
32
33
34
# File 'lib/capybara/node/simple.rb', line 31

def text(_type = nil, normalize_ws: false)
  txt = native.text
  normalize_ws ? txt.gsub(/[[:space:]]+/, ' ').strip : txt
end

#titleString

Returns The title of the document.

Returns:

  • (String)

    The title of the document



166
167
168
# File 'lib/capybara/node/simple.rb', line 166

def title
  native.title
end

#valueString

Returns The value of the form element.

Returns:

  • (String)

    The value of the form element



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/capybara/node/simple.rb', line 78

def value
  if tag_name == 'textarea'
    native['_capybara_raw_value']
  elsif tag_name == 'select'
    selected_options = find_xpath('.//option[@selected]')
    if multiple?
      selected_options.map(&method(:option_value))
    else
      option_value(selected_options.first || find_xpath('.//option').first)
    end
  elsif tag_name == 'input' && %w[radio checkbox].include?(native[:type])
    native[:value] || 'on'
  else
    native[:value]
  end
end

#visible?(check_ancestors = true) ⇒ Boolean

Whether or not the element is visible. Does not support CSS, so the result may be inaccurate.

Parameters:

  • check_ancestors (Boolean) (defaults to: true)

    Whether to inherit visibility from ancestors

Returns:

  • (Boolean)

    Whether the element is visible



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

def visible?(check_ancestors = true) # rubocop:disable Style/OptionalBooleanParameter
  return false if (tag_name == 'input') && (native[:type] == 'hidden')
  return false if tag_name == 'template'

  if check_ancestors
    !find_xpath(VISIBILITY_XPATH)
  else
    # No need for an xpath if only checking the current element
    !(native.key?('hidden') ||
      /display:\s?none/.match?(native[:style] || '') ||
      %w[script head style].include?(tag_name))
  end
end