Class: Capybara::Selenium::Node

Inherits:
Driver::Node show all
Defined in:
lib/capybara/selenium/node.rb

Instance Attribute Summary

Attributes inherited from Driver::Node

#driver, #native

Instance Method Summary collapse

Methods inherited from Driver::Node

#initialize, #inspect, #trigger

Constructor Details

This class inherits a constructor from Capybara::Driver::Node

Instance Method Details

#==(other) ⇒ Object



162
163
164
# File 'lib/capybara/selenium/node.rb', line 162

def ==(other)
  native == other.native
end

#[](name) ⇒ Object



17
18
19
20
21
# File 'lib/capybara/selenium/node.rb', line 17

def [](name)
  native.attribute(name.to_s)
rescue Selenium::WebDriver::Error::WebDriverError
  nil
end

#all_textObject



8
9
10
11
12
13
14
15
# File 'lib/capybara/selenium/node.rb', line 8

def all_text
  text = driver.execute_script("return arguments[0].textContent", self)
  text.gsub(/[\u200b\u200e\u200f]/, '')
      .gsub(/[\ \n\f\t\v\u2028\u2029]+/, ' ')
      .gsub(/\A[[:space:]&&[^\u00a0]]+/, "")
      .gsub(/[[:space:]&&[^\u00a0]]+\z/, "")
      .tr("\u00a0", ' ')
end

#click(keys = [], **options) ⇒ Object



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

def click(keys = [], **options)
  if keys.empty? && !coords?(options)
    native.click
  else
    scroll_if_needed do
      action_with_modifiers(keys, options) do |a|
        coords?(options) ? a.click : a.click(native)
      end
    end
  end
rescue StandardError => e
  if e.is_a?(::Selenium::WebDriver::Error::ElementClickInterceptedError) ||
     e.message =~ /Other element would receive the click/
    scroll_to_center
  end
  raise e
end

#content_editable?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/capybara/selenium/node.rb', line 150

def content_editable?
  native.attribute('isContentEditable')
end

#disabled?Boolean

Returns:

  • (Boolean)


135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/capybara/selenium/node.rb', line 135

def disabled?
  return true unless native.enabled?

  # workaround for selenium-webdriver/geckodriver reporting elements as enabled when they are nested in disabling elements
  if driver.marionette?
    if %w[option optgroup].include? tag_name
      find_xpath("parent::*[self::optgroup or self::select]")[0].disabled?
    else
      !find_xpath("parent::fieldset[@disabled] | ancestor::*[not(self::legend) or preceding-sibling::legend][parent::fieldset[@disabled]]").empty?
    end
  else
    false
  end
end

#double_click(keys = [], **options) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/capybara/selenium/node.rb', line 105

def double_click(keys = [], **options)
  scroll_if_needed do
    action_with_modifiers(keys, options) do |a|
      coords?(options) ? a.double_click : a.double_click(native)
    end
  end
end

#drag_to(element) ⇒ Object



121
122
123
# File 'lib/capybara/selenium/node.rb', line 121

def drag_to(element)
  scroll_if_needed { driver.browser.action.drag_and_drop(native, element.native).perform }
end

#find_css(locator) ⇒ Object



158
159
160
# File 'lib/capybara/selenium/node.rb', line 158

def find_css(locator)
  native.find_elements(:css, locator).map { |n| self.class.new(driver, n) }
end

#find_xpath(locator) ⇒ Object



154
155
156
# File 'lib/capybara/selenium/node.rb', line 154

def find_xpath(locator)
  native.find_elements(:xpath, locator).map { |n| self.class.new(driver, n) }
end

#hoverObject



117
118
119
# File 'lib/capybara/selenium/node.rb', line 117

def hover
  scroll_if_needed { driver.browser.action.move_to(native).perform }
end

#multiple?Boolean

Returns:

  • (Boolean)


131
# File 'lib/capybara/selenium/node.rb', line 131

def multiple?; boolean_attr(self[:multiple]); end

#pathObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/capybara/selenium/node.rb', line 166

def path
  path = find_xpath(XPath.ancestor_or_self).reverse

  result = []
  while (node = path.shift)
    parent = path.first
    selector = node.tag_name
    if parent
      siblings = parent.find_xpath(selector)
      selector += "[#{siblings.index(node) + 1}]" unless siblings.size == 1
    end
    result.push selector
  end

  '/' + result.reverse.join('/')
end

#readonly?Boolean

Returns:

  • (Boolean)


130
# File 'lib/capybara/selenium/node.rb', line 130

def readonly?; boolean_attr(self[:readonly]); end

#right_click(keys = [], **options) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/capybara/selenium/node.rb', line 97

def right_click(keys = [], **options)
  scroll_if_needed do
    action_with_modifiers(keys, options) do |a|
      coords?(options) ? a.context_click : a.context_click(native)
    end
  end
end

#select_optionObject



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

def select_option
  native.click unless selected? || disabled?
end

#selected?Boolean Also known as: checked?

Returns:

  • (Boolean)


132
# File 'lib/capybara/selenium/node.rb', line 132

def selected?; boolean_attr(native.selected?); end

#send_keys(*args) ⇒ Object



113
114
115
# File 'lib/capybara/selenium/node.rb', line 113

def send_keys(*args)
  native.send_keys(*args)
end

#set(value, **options) ⇒ Object

Set the value of the form element to the given value.

Parameters:

  • value (String)

    The new value

  • options (Hash{})

    Driver specific options for how to set the value

Options Hash (**options):

  • :clear (Symbol, Array) — default: nil

    The method used to clear the previous value <br/> nil => clear via javascript <br/> :none => append the new value to the existing value <br/> :backspace => send backspace keystrokes to clear the field <br/> Array => an array of keys to send before the value being set, e.g. [[:command, ‘a’], :backspace]

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/capybara/selenium/node.rb', line 42

def set(value, **options)
  raise ArgumentError, "Value cannot be an Array when 'multiple' attribute is not present. Not a #{value.class}" if value.is_a?(Array) && !multiple?

  case tag_name
  when 'input'
    case self[:type]
    when 'radio'
      click
    when 'checkbox'
      click if value ^ checked?
    when 'file'
      set_file(value)
    when 'date'
      set_date(value)
    when 'time'
      set_time(value)
    when 'datetime-local'
      set_datetime_local(value)
    else
      set_text(value, options)
    end
  when 'textarea'
    set_text(value, options)
  else
    set_content_editable(value) if content_editable?
  end
end

#tag_nameObject



125
126
127
# File 'lib/capybara/selenium/node.rb', line 125

def tag_name
  native.tag_name.downcase
end

#unselect_optionObject



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

def unselect_option
  raise Capybara::UnselectNotAllowed, "Cannot unselect option from single select box." unless select_node.multiple?
  native.click if selected?
end

#valueObject



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

def value
  if tag_name == "select" && multiple?
    native.find_elements(:css, "option:checked").map { |n| n[:value] || n.text }
  else
    native[:value]
  end
end

#visible?Boolean

Returns:

  • (Boolean)


129
# File 'lib/capybara/selenium/node.rb', line 129

def visible?; boolean_attr(native.displayed?); end

#visible_textObject



4
5
6
# File 'lib/capybara/selenium/node.rb', line 4

def visible_text
  native.text
end