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

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



75
76
77
78
79
80
81
# File 'lib/capybara/driver/rack_test_driver.rb', line 75

def click
  if tag_name == 'a'
    driver.visit(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



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

def path
  node.path
end

#select(option) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/capybara/driver/rack_test_driver.rb', line 47

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()='#{option}']").first ||
                   node.xpath(".//option[contains(.,'#{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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/capybara/driver/rack_test_driver.rb', line 30

def set(value)
  if tag_name == 'input' and %w(text password hidden file).include?(type)
    node['value'] = value.to_s
  elsif tag_name == 'input' and type == 'radio'
    driver.html.xpath("//input[@name='#{self[:name]}']").each { |node| node.remove_attribute("checked") }
    node['checked'] = 'checked'
  elsif tag_name == 'input' and type == 'checkbox'
    if value
      node['checked'] = 'checked'
    else
      node.remove_attribute('checked')
    end
  elsif tag_name == "textarea"
    node.content = value.to_s
  end
end

#tag_nameObject



83
84
85
# File 'lib/capybara/driver/rack_test_driver.rb', line 83

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



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/capybara/driver/rack_test_driver.rb', line 61

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()='#{option}']").first ||
                   node.xpath(".//option[contains(.,'#{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

#visible?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/capybara/driver/rack_test_driver.rb', line 87

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