2010-11-21 08:37:36 -05:00
module Capybara
module Node
2010-11-21 08:49:00 -05:00
##
#
# 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.
#
2010-11-21 08:37:36 -05:00
class Simple
include Capybara :: Node :: Finders
include Capybara :: Node :: Matchers
attr_reader :native
def initialize ( native )
native = Nokogiri :: HTML ( native ) if native . is_a? ( String )
@native = native
end
2010-11-21 08:49:00 -05:00
##
#
# @return [String] The text of the element
#
2010-11-21 08:37:36 -05:00
def text
native . text
end
2010-11-21 08:49:00 -05:00
##
#
# Retrieve the given attribute
#
# element[:title] # => HTML title attribute
#
# @param [Symbol] attribute The attribute to retrieve
# @return [String] The value of the attribute
#
2010-11-21 08:37:36 -05:00
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
2010-11-21 08:49:00 -05:00
##
#
# @return [String] The tag name of the element
#
2010-11-21 08:37:36 -05:00
def tag_name
native . node_name
end
2010-11-21 08:49:00 -05:00
##
#
# An XPath expression describing where on the page the element can be found
#
# @return [String] An XPath expression
#
2010-11-21 08:37:36 -05:00
def path
native . path
end
2010-11-21 08:49:00 -05:00
##
#
# @return [String] The value of the form element
#
2010-11-21 08:37:36 -05:00
def value
if tag_name == 'textarea'
2012-04-07 12:59:47 -04:00
native . content . sub ( / \ A \ n / , '' )
2010-11-21 08:37:36 -05:00
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
2010-11-21 08:49:00 -05:00
##
#
# Whether or not the element is visible. Does not support CSS, so
# the result may be inaccurate.
#
# @return [Boolean] Whether the element is visible
#
2010-11-21 08:37:36 -05:00
def visible?
2011-10-24 01:51:03 -04:00
native . xpath ( " ./ancestor-or-self::*[contains(@style, 'display:none') or contains(@style, 'display: none') or name()='script' or name()='head'] " ) . size == 0
2010-11-21 08:37:36 -05:00
end
2011-03-25 06:35:59 -04:00
##
#
# Whether or not the element is checked.
#
# @return [Boolean] Whether the element is checked
#
def checked?
native [ :checked ]
end
##
#
# Whether or not the element is selected.
#
# @return [Boolean] Whether the element is selected
#
def selected?
native [ :selected ]
end
2012-02-01 08:16:17 -05:00
def synchronize
yield # simple nodes don't need to wait
end
2012-06-11 11:29:58 -04:00
def allow_reload!
# no op
end
2012-10-30 07:57:22 -04:00
def unsynchronized
yield # simple nodes don't need to wait
end
2012-06-08 10:47:01 -04:00
def all ( * args )
query = Capybara :: Query . new ( * args )
elements = native . xpath ( query . xpath ) . map do | node |
2012-02-01 08:40:08 -05:00
self . class . new ( node )
2012-06-08 10:47:01 -04:00
end
Capybara :: Result . new ( elements , query )
2010-11-21 08:37:36 -05:00
end
end
end
end