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



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

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[:value] || option.content  }
    else
      option = native.xpath(".//option[@selected='selected']").first || native.xpath(".//option").first
      option[:value] || 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



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

def click
  if tag_name == 'a'
    method = self["data-method"] || :get
    driver.process(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")
    Form.new(driver, form).submit(self)
  end
end

#find(locator) ⇒ Object



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

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

#pathObject



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

def path
  native.path
end

#select_optionObject



56
57
58
59
60
61
# File 'lib/capybara/driver/rack_test_driver.rb', line 56

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

#set(value) ⇒ Object



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

def set(value)
  if tag_name == 'input' and type == 'radio'
    other_radios_xpath = XPath.generate { |x| x.anywhere(:input)[x.attr(:name).equals(self[:name])] }.to_s
    driver.html.xpath(other_radios_xpath).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



80
81
82
# File 'lib/capybara/driver/rack_test_driver.rb', line 80

def tag_name
  native.node_name
end

#textObject



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

def text
  native.text
end

#unselect_optionObject



63
64
65
66
67
68
# File 'lib/capybara/driver/rack_test_driver.rb', line 63

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



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

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

#visible?Boolean

Returns:

  • (Boolean)


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

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