Class: Capybara::Node::Simple

Inherits:
Object
  • Object
show all
Includes:
DocumentMatchers, Finders, Matchers
Defined in:
lib/capybara/node/simple.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 DocumentMatchers

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

Methods included from Matchers

#==, #assert_matches_selector, #assert_no_selector, #assert_no_text, #assert_not_matches_selector, #assert_selector, #assert_text, #has_button?, #has_checked_field?, #has_css?, #has_field?, #has_link?, #has_no_button?, #has_no_checked_field?, #has_no_css?, #has_no_field?, #has_no_link?, #has_no_select?, #has_no_selector?, #has_no_table?, #has_no_text?, #has_no_unchecked_field?, #has_no_xpath?, #has_select?, #has_selector?, #has_table?, #has_text?, #has_unchecked_field?, #has_xpath?, #matches_css?, #matches_selector?, #matches_xpath?, #not_matches_css?, #not_matches_selector?, #not_matches_xpath?

Methods included from Finders

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

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

#[](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



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

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

#allow_reload!Object



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

def allow_reload!
  # no op
end

#checked?Boolean

Whether or not the element is checked.

Returns:

  • (Boolean)

    Whether the element is checked



120
121
122
# File 'lib/capybara/node/simple.rb', line 120

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

#disabled?Boolean

Whether or not the element is disabled.

Returns:

  • (Boolean)

    Whether the element is disabled



129
130
131
# File 'lib/capybara/node/simple.rb', line 129

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

#find_css(css) ⇒ 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.



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

def find_css(css)
  native.css(css)
end

#find_xpath(xpath) ⇒ 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.



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

def find_xpath(xpath)
  native.xpath(xpath)
end

#inspectObject



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

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

#pathString

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

Returns:

  • (String)

    An XPath expression



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

def path
  native.path
end

#selected?Boolean

Whether or not the element is selected.

Returns:

  • (Boolean)

    Whether the element is selected



139
140
141
# File 'lib/capybara/node/simple.rb', line 139

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

#synchronize(seconds = nil) ⇒ Object



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

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



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

def tag_name
  native.node_name
end

#text(type = nil) ⇒ String

Returns The text of the element

Returns:

  • (String)

    The text of the element



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

def text(type=nil)
  native.text
end

#titleObject



151
152
153
154
155
156
157
158
# File 'lib/capybara/node/simple.rb', line 151

def title
  if native.respond_to? :title
    native.title
  else
    #old versions of nokogiri don't have #title - remove in 3.0
    native.xpath('/html/head/title | /html/title').first.text
  end
end

#valueString

Returns The value of the form element

Returns:

  • (String)

    The value of the form element



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

def value
  if tag_name == 'textarea'
    native.content
  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
  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



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

def visible?(check_ancestors = true)
  return false if (tag_name == 'input') && (native[:type]=="hidden")

  if check_ancestors
    #check size because oldest supported nokogiri doesnt support xpath boolean() function
    native.xpath("./ancestor-or-self::*[contains(@style, 'display:none') or contains(@style, 'display: none') or @hidden or name()='script' or name()='head']").size() == 0
  else
    #no need for an xpath if only checking the current element
    !(native.has_attribute?('hidden') || (native[:style] =~ /display:\s?none/) || %w(script head).include?(tag_name))
  end
end