Parent

Capybara::Node::Simple

A {Capybara::Node::Simple} is a simpler version of {Capybara::Node::Base} which includes only {Capybara::Node::Finders} and {Capybara::Node::Matchers} and does not include {Capybara::Node::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.

Attributes

native[R]

Public Class Methods

new(native) click to toggle source
# File lib/capybara/node/simple.rb, line 20
def initialize(native)
  native = Nokogiri::HTML(native) if native.is_a?(String)
  @native = native
end

Public Instance Methods

[](name) click to toggle source

Retrieve the given attribute

element[:title] # => HTML title attribute

@param [Symbol] attribute The attribute to retrieve @return [String] The value of the attribute

# File lib/capybara/node/simple.rb, line 42
def [](name)
  attr_name = name.to_s
  if attr_name == 'value'
    value
  elsif 'input' == tag_name and 'checkbox' == native[:type] and 'checked' == attr_name
    native['checked'] == 'checked'
  else
    native[attr_name]
  end
end
all(*args) click to toggle source
# File lib/capybara/node/simple.rb, line 133
def all(*args)
  query = Capybara::Query.new(*args)
  elements = native.xpath(query.xpath).map do |node|
    self.class.new(node)
  end
  Capybara::Result.new(elements, query)
end
allow_reload!() click to toggle source
# File lib/capybara/node/simple.rb, line 125
def allow_reload!
  # no op
end
checked?() click to toggle source

Whether or not the element is checked.

@return [Boolean] Whether the element is checked

# File lib/capybara/node/simple.rb, line 107
def checked?
  native[:checked]
end
path() click to toggle source

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

@return [String] An XPath expression

# File lib/capybara/node/simple.rb, line 67
def path
  native.path
end
selected?() click to toggle source

Whether or not the element is selected.

@return [Boolean] Whether the element is selected

# File lib/capybara/node/simple.rb, line 117
def selected?
  native[:selected]
end
synchronize() click to toggle source
# File lib/capybara/node/simple.rb, line 121
def synchronize
  yield # simple nodes don't need to wait
end
tag_name() click to toggle source

@return [String] The tag name of the element

# File lib/capybara/node/simple.rb, line 57
def tag_name
  native.node_name
end
text() click to toggle source

@return [String] The text of the element

# File lib/capybara/node/simple.rb, line 29
def text
  native.text
end
unsynchronized() click to toggle source
# File lib/capybara/node/simple.rb, line 129
def unsynchronized
  yield # simple nodes don't need to wait
end
value() click to toggle source

@return [String] The value of the form element

# File lib/capybara/node/simple.rb, line 75
def value
  if tag_name == 'textarea'
    native.content.sub(/\A\n/, '')
  elsif tag_name == 'select'
    if native['multiple'] == 'multiple'
      native.xpath(".//option[@selected='selected']").map { |option| option[:value] || option.content  }
    else
      option = native.xpath(".//option[@selected='selected']").first || native.xpath(".//option").first
      option[:value] || option.content if option
    end
  else
    native[:value]
  end
end
visible?() click to toggle source

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

@return [Boolean] Whether the element is visible

# File lib/capybara/node/simple.rb, line 97
def visible?
  native.xpath("./ancestor-or-self::*[contains(@style, 'display:none') or contains(@style, 'display: none') or name()='script' or name()='head']").size == 0
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.