class Capybara::Poltergeist::Node

Attributes

id[R]
page_id[R]

Public Class Methods

new(driver, page_id, id) click to toggle source
Calls superclass method
# File lib/capybara/poltergeist/node.rb, line 6
def initialize(driver, page_id, id)
  super(driver, self)

  @page_id = page_id
  @id      = id
end

Public Instance Methods

==(other) click to toggle source
# File lib/capybara/poltergeist/node.rb, line 169
def ==(other)
  (page_id == other.page_id) && command(:equals, other.id)
end
[](name) click to toggle source
# File lib/capybara/poltergeist/node.rb, line 66
def [](name)
  # Although the attribute matters, the property is consistent. Return that in
  # preference to the attribute for links and images.
  if (tag_name == 'img' and name == 'src') or (tag_name == 'a' and name == 'href' )
     #if attribute exists get the property
     value = command(:attribute, name) && command(:property, name)
     return value
  end

  value = property(name)
  value = command(:attribute, name) if value.nil? || value.is_a?(Hash)

  value
end
all_text() click to toggle source
# File lib/capybara/poltergeist/node.rb, line 46
def all_text
  filter_text command(:all_text)
end
as_json(*) click to toggle source

@api private

# File lib/capybara/poltergeist/node.rb, line 188
def as_json(*)
  { ELEMENT: {page_id: @page_id, id: @id} }
end
attributes() click to toggle source
# File lib/capybara/poltergeist/node.rb, line 81
def attributes
  command :attributes
end
browser() click to toggle source
# File lib/capybara/poltergeist/node.rb, line 13
def browser
  driver.browser
end
checked?() click to toggle source
# File lib/capybara/poltergeist/node.rb, line 129
def checked?
  self[:checked]
end
click(keys=[], offset={}) click to toggle source
# File lib/capybara/poltergeist/node.rb, line 141
def click(keys=[], offset={})
  command :click, keys, offset
end
command(name, *args) click to toggle source
# File lib/capybara/poltergeist/node.rb, line 17
def command(name, *args)
  browser.send(name, page_id, id, *args)
rescue BrowserError => error
  case error.name
  when 'Poltergeist.ObsoleteNode'
    raise ObsoleteNode.new(self, error.response)
  when 'Poltergeist.MouseEventFailed'
    raise MouseEventFailed.new(self, error.response)
  else
    raise
  end
end
disabled?() click to toggle source
# File lib/capybara/poltergeist/node.rb, line 137
def disabled?
  command :disabled?
end
double_click(keys=[], offset={}) click to toggle source
# File lib/capybara/poltergeist/node.rb, line 149
def double_click(keys=[], offset={})
  command :double_click, keys, offset
end
drag_by(x, y) click to toggle source
# File lib/capybara/poltergeist/node.rb, line 161
def drag_by(x, y)
  command :drag_by, x, y
end
drag_to(other) click to toggle source
# File lib/capybara/poltergeist/node.rb, line 157
def drag_to(other)
  command :drag, other.id
end
find(method, selector) click to toggle source
# File lib/capybara/poltergeist/node.rb, line 34
def find(method, selector)
  command(:find_within, method, selector).map { |id| self.class.new(driver, page_id, id) }
end
find_css(selector) click to toggle source
# File lib/capybara/poltergeist/node.rb, line 42
def find_css(selector)
  find :css, selector
end
find_xpath(selector) click to toggle source
# File lib/capybara/poltergeist/node.rb, line 38
def find_xpath(selector)
  find :xpath, selector
end
hover() click to toggle source
# File lib/capybara/poltergeist/node.rb, line 153
def hover
  command :hover
end
parents() click to toggle source
# File lib/capybara/poltergeist/node.rb, line 30
def parents
  command(:parents).map { |parent_id| self.class.new(driver, page_id, parent_id) }
end
path() click to toggle source
# File lib/capybara/poltergeist/node.rb, line 178
def path
  command :path
end
property(name) click to toggle source
# File lib/capybara/poltergeist/node.rb, line 62
def property(name)
  command :property, name
end
right_click(keys=[], offset={}) click to toggle source
# File lib/capybara/poltergeist/node.rb, line 145
def right_click(keys=[], offset={})
  command :right_click, keys, offset
end
select_option() click to toggle source
# File lib/capybara/poltergeist/node.rb, line 112
def select_option
  command :select, true
end
selected?() click to toggle source
# File lib/capybara/poltergeist/node.rb, line 133
def selected?
  !!self[:selected]
end
send_key(*keys)
Alias for: send_keys
send_keys(*keys) click to toggle source
# File lib/capybara/poltergeist/node.rb, line 173
def send_keys(*keys)
  command :send_keys, keys
end
Also aliased as: send_key
set(value, options = {}) click to toggle source
# File lib/capybara/poltergeist/node.rb, line 89
def set(value, options = {})
  warn "Options passed to Node#set but Poltergeist doesn't currently support any - ignoring" unless options.empty?

  if tag_name == 'input'
    case self[:type]
    when 'radio'
      click
    when 'checkbox'
      click if value != checked?
    when 'file'
      files = value.respond_to?(:to_ary) ? value.to_ary.map(&:to_s) : value.to_s
      command :select_file, files
    else
      command :set, value.to_s
    end
  elsif tag_name == 'textarea'
    command :set, value.to_s
  elsif self[:isContentEditable]
    command :delete_text
    send_keys(value.to_s)
  end
end
tag_name() click to toggle source
# File lib/capybara/poltergeist/node.rb, line 121
def tag_name
  @tag_name ||= command(:tag_name)
end
to_json(*) click to toggle source

@api private

# File lib/capybara/poltergeist/node.rb, line 183
def to_json(*)
  JSON.generate as_json
end
trigger(event) click to toggle source
# File lib/capybara/poltergeist/node.rb, line 165
def trigger(event)
  command :trigger, event
end
unselect_option() click to toggle source
# File lib/capybara/poltergeist/node.rb, line 116
def unselect_option
  command(:select, false) or
  raise(Capybara::UnselectNotAllowed, "Cannot unselect option from single select box.")
end
value() click to toggle source
# File lib/capybara/poltergeist/node.rb, line 85
def value
  command :value
end
visible?() click to toggle source
# File lib/capybara/poltergeist/node.rb, line 125
def visible?
  command :visible?
end
visible_text() click to toggle source
# File lib/capybara/poltergeist/node.rb, line 50
def visible_text
  if Capybara::VERSION.to_f < 3.0
    filter_text command(:visible_text)
  else
    command(:visible_text).to_s
                          .gsub(/\A[[:space:]&&[^\u00a0]]+/, "")
                          .gsub(/[[:space:]&&[^\u00a0]]+\z/, "")
                          .gsub(/\n+/, "\n")
                          .tr("\u00a0", ' ')
  end
end

Private Instance Methods

filter_text(text, visible = true) click to toggle source
# File lib/capybara/poltergeist/node.rb, line 194
def filter_text(text, visible = true)
  if Capybara::VERSION.to_f < 3
    Capybara::Helpers.normalize_whitespace(text.to_s)
  else
    text.gsub(/[\u200b\u200e\u200f]/, '')
        .gsub(/[\ \n\f\t\v\u2028\u2029]+/, ' ')
        .gsub(/\A[[:space:]&&[^\u00a0]]+/, "")
        .gsub(/[[:space:]&&[^\u00a0]]+\z/, "")
        .tr("\u00a0", ' ')
  end
end