Class: Capybara::Driver::RackTest::Node

Inherits:
Node
  • Object
show all
Defined in:
lib/capybara/driver/rack_test_driver.rb

Direct Known Subclasses

Form

Instance Attribute Summary

Attributes inherited from Node

#driver, #native

Instance Method Summary collapse

Methods inherited from Node

#drag_to, #initialize, #inspect, #trigger

Constructor Details

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

Instance Method Details

#[](name) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/capybara/driver/rack_test_driver.rb', line 12

def [](name)
  attr_name = name.to_s
  case
  when 'select' == tag_name && 'value' == attr_name
    if native['multiple'] == 'multiple'
      native.xpath(".//option[@selected='selected']").map { |option| option.content  }
    else
      option = native.xpath(".//option[@selected='selected']").first || native.xpath(".//option").first
      option.content if option
    end
  when 'input' == tag_name && 'checkbox' == type && 'checked' == attr_name
    native[attr_name] == 'checked' ? true : false
  else
    native[attr_name]
  end
end

#clickObject



82
83
84
85
86
87
88
89
# File 'lib/capybara/driver/rack_test_driver.rb', line 82

def click
  if tag_name == 'a'
    method = self["data-method"] || :get
    driver.process(method, self[:href].to_s)
  elsif (tag_name == 'input' or tag_name == 'button') and %w(submit image).include?(type)
    Form.new(driver, form).submit(self)
  end
end

#find(locator) ⇒ Object



103
104
105
# File 'lib/capybara/driver/rack_test_driver.rb', line 103

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

#pathObject



99
100
101
# File 'lib/capybara/driver/rack_test_driver.rb', line 99

def path
  native.path
end

#select_option(option) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/capybara/driver/rack_test_driver.rb', line 54

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

  if option_node = native.xpath(".//option[text()=#{Capybara::XPath.escape(option)}]").first ||
                   native.xpath(".//option[contains(.,#{Capybara::XPath.escape(option)})]").first
    option_node["selected"] = 'selected'
  else
    options = native.xpath(".//option").map { |o| "'#{o.text}'" }.join(', ')
    raise Capybara::OptionNotFound, "No such option '#{option}' in this select box. Available options: #{options}"
  end
end

#set(value) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/capybara/driver/rack_test_driver.rb', line 37

def set(value)
  if tag_name == 'input' and type == 'radio'
    driver.html.xpath("//input[@name=#{Capybara::XPath.escape(self[:name])}]").each { |node| node.remove_attribute("checked") }
    native['checked'] = 'checked'
  elsif tag_name == 'input' and type == 'checkbox'
    if value && !native['checked']
      native['checked'] = 'checked'
    elsif !value && native['checked']
      native.remove_attribute('checked')
    end
  elsif tag_name == 'input'
    native['value'] = value.to_s
  elsif tag_name == "textarea"
    native.content = value.to_s
  end
end

#tag_nameObject



91
92
93
# File 'lib/capybara/driver/rack_test_driver.rb', line 91

def tag_name
  native.node_name
end

#textObject



8
9
10
# File 'lib/capybara/driver/rack_test_driver.rb', line 8

def text
  native.text
end

#unselect_option(option) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/capybara/driver/rack_test_driver.rb', line 68

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

  if option_node = native.xpath(".//option[text()=#{Capybara::XPath.escape(option)}]").first ||
                   native.xpath(".//option[contains(.,#{Capybara::XPath.escape(option)})]").first
    option_node.remove_attribute('selected')
  else
    options = native.xpath(".//option").map { |o| "'#{o.text}'" }.join(', ')
    raise Capybara::OptionNotFound, "No such option '#{option}' in this select box. Available options: #{options}"
  end
end

#valueObject



29
30
31
32
33
34
35
# File 'lib/capybara/driver/rack_test_driver.rb', line 29

def value
  if tag_name == 'textarea'
    native.content
  else
    self[:value]
  end
end

#visible?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/capybara/driver/rack_test_driver.rb', line 95

def visible?
  native.xpath("./ancestor-or-self::*[contains(@style, 'display:none') or contains(@style, 'display: none')]").size == 0
end