Class: Capybara::RackTest::Node

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

Direct Known Subclasses

Form

Instance Attribute Summary

Attributes inherited from Driver::Node

#driver, #native

Instance Method Summary collapse

Methods inherited from Driver::Node

#double_click, #drag_to, #hover, #initialize, #inspect, #multiple?, #readonly?, #right_click, #send_keys, #trigger

Constructor Details

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

Instance Method Details

#==(other) ⇒ Object



112
113
114
# File 'lib/capybara/rack_test/node.rb', line 112

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

#[](name) ⇒ Object



11
12
13
# File 'lib/capybara/rack_test/node.rb', line 11

def [](name)
  string_node[name]
end

#all_textObject



3
4
5
# File 'lib/capybara/rack_test/node.rb', line 3

def all_text
  Capybara::Helpers.normalize_whitespace(native.text)
end

#checked?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/capybara/rack_test/node.rb', line 84

def checked?
  string_node.checked?
end

#clickObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/capybara/rack_test/node.rb', line 54

def click
  if tag_name == 'a' && !self[:href].nil?
    method = self["data-method"] if driver.options[:respect_data_method]
    method ||= :get
    driver.follow(method, self[:href].to_s)
  elsif (tag_name == 'input' and %w(submit image).include?(type)) or
      ((tag_name == 'button') and type.nil? or type == "submit")
    associated_form = form
    Capybara::RackTest::Form.new(driver, associated_form).submit(self) if associated_form
  elsif (tag_name == 'label')
    labelled_control = if native[:for]
      find_xpath("//input[@id='#{native[:for]}']").first
    else
      find_xpath(".//input").first
    end

    if labelled_control && (labelled_control.checkbox? || labelled_control.radio?)
      labelled_control.set(!labelled_control.checked?)
    end
  end
end

#disabled?Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
98
# File 'lib/capybara/rack_test/node.rb', line 92

def disabled?
  if %w(option optgroup).include? tag_name
    string_node.disabled? || find_xpath("parent::*")[0].disabled?
  else
    string_node.disabled?
  end
end

#find_css(locator) ⇒ Object



108
109
110
# File 'lib/capybara/rack_test/node.rb', line 108

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

#find_xpath(locator) ⇒ Object



104
105
106
# File 'lib/capybara/rack_test/node.rb', line 104

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

#pathObject



100
101
102
# File 'lib/capybara/rack_test/node.rb', line 100

def path
  native.path
end

#select_optionObject



39
40
41
42
43
44
45
# File 'lib/capybara/rack_test/node.rb', line 39

def select_option
  return if disabled?
  if select_node['multiple'] != 'multiple'
    select_node.find_xpath(".//option[@selected]").each { |node| node.native.remove_attribute("selected") }
  end
  native["selected"] = 'selected'
end

#selected?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/capybara/rack_test/node.rb', line 88

def selected?
  string_node.selected?
end

#set(value) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/capybara/rack_test/node.rb', line 19

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

  if radio?
    set_radio(value)
  elsif checkbox?
    set_checkbox(value)
  elsif input_field?
    set_input(value)
  elsif textarea?
    if self[:readonly]
      warn "Attempt to set readonly element with value: #{value} \n * This will raise an exception in a future version of Capybara"
    else
      native.content = value.to_s
    end
  end
end

#tag_nameObject



76
77
78
# File 'lib/capybara/rack_test/node.rb', line 76

def tag_name
  native.node_name
end

#unselect_optionObject



47
48
49
50
51
52
# File 'lib/capybara/rack_test/node.rb', line 47

def unselect_option
  if select_node['multiple'] != 'multiple'
    raise Capybara::UnselectNotAllowed, "Cannot unselect option from single select box."
  end
  native.remove_attribute('selected')
end

#valueObject



15
16
17
# File 'lib/capybara/rack_test/node.rb', line 15

def value
  string_node.value
end

#visible?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/capybara/rack_test/node.rb', line 80

def visible?
  string_node.visible?
end

#visible_textObject



7
8
9
# File 'lib/capybara/rack_test/node.rb', line 7

def visible_text
  Capybara::Helpers.normalize_whitespace(unnormalized_text)
end