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, #node

Instance Method Summary collapse

Methods inherited from Node

#drag_to, #initialize, #trigger

Methods included from Searchable

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

Constructor Details

This class inherits a constructor from Capybara::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 node['multiple'] == 'multiple'
      node.xpath(".//option[@selected='selected']").map { |option| option.content  }
    else
      option = node.xpath(".//option[@selected='selected']").first || node.xpath(".//option").first
      option.content if option
    end
  when 'input' == tag_name && 'checkbox' == type && 'checked' == attr_name
    node[attr_name] == 'checked' ? true : false
  else
    node[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' || tag_name == 'area'
    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

#pathObject



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

def path
  node.path
end

#select(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)
  if node['multiple'] != 'multiple'
    node.xpath(".//option[@selected]").each { |node| node.remove_attribute("selected") }
  end

  if option_node = node.xpath(".//option[text()=#{Capybara::XPath.escape(option)}]").first ||
                   node.xpath(".//option[contains(.,#{Capybara::XPath.escape(option)})]").first
    option_node["selected"] = 'selected'
  else
    options = node.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") }
    node['checked'] = 'checked'
  elsif tag_name == 'input' and type == 'checkbox'
    if value && !node['checked']
      node['checked'] = 'checked'
    elsif !value && node['checked']
      node.remove_attribute('checked')
    end
  elsif tag_name == 'input'
    node['value'] = value.to_s
  elsif tag_name == "textarea"
    node.content = value.to_s
  end
end

#tag_nameObject



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

def tag_name
  node.node_name
end

#textObject



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

def text
  node.text
end

#unselect(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)
  if node['multiple'] != 'multiple'
    raise Capybara::UnselectNotAllowed, "Cannot unselect option '#{option}' from single select box."
  end

  if option_node = node.xpath(".//option[text()=#{Capybara::XPath.escape(option)}]").first ||
                   node.xpath(".//option[contains(.,#{Capybara::XPath.escape(option)})]").first
    option_node.remove_attribute('selected')
  else
    options = node.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'
    node.content
  else
    super
  end
end

#visible?Boolean

Returns:

  • (Boolean)


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

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