Class: Capybara::Poltergeist::Node

Inherits:
Driver::Node
  • Object
show all
Defined in:
lib/capybara/poltergeist/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(driver, page_id, id) ⇒ Node

Returns a new instance of Node.



7
8
9
10
11
12
# File 'lib/capybara/poltergeist/node.rb', line 7

def initialize(driver, page_id, id)
  super(driver, self)

  @page_id = page_id
  @id      = id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/capybara/poltergeist/node.rb', line 5

def id
  @id
end

#page_idObject (readonly)

Returns the value of attribute page_id.



5
6
7
# File 'lib/capybara/poltergeist/node.rb', line 5

def page_id
  @page_id
end

Instance Method Details

#==(other) ⇒ Object



170
171
172
# File 'lib/capybara/poltergeist/node.rb', line 170

def ==(other)
  (page_id == other.page_id) && command(:equals, other.id)
end

#[](name) ⇒ Object



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

def [](name)
  # Although the attribute matters, the property is consistent. Return that in
  # preference to the attribute for links and images.
  if (tag_name == 'img' and name == 'src') or (tag_name == 'a' and name == 'href' )
     #if attribute exists get the property
     value = command(:attribute, name) && command(:property, name)
     return value
  end

  value = property(name)
  value = command(:attribute, name) if value.nil? || value.is_a?(Hash)

  value
end

#all_textObject



47
48
49
# File 'lib/capybara/poltergeist/node.rb', line 47

def all_text
  filter_text command(:all_text)
end

#as_jsonObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



189
190
191
# File 'lib/capybara/poltergeist/node.rb', line 189

def as_json(*)
  { ELEMENT: {page_id: @page_id, id: @id} }
end

#attributesObject



82
83
84
# File 'lib/capybara/poltergeist/node.rb', line 82

def attributes
  command :attributes
end

#browserObject



14
15
16
# File 'lib/capybara/poltergeist/node.rb', line 14

def browser
  driver.browser
end

#checked?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/capybara/poltergeist/node.rb', line 130

def checked?
  self[:checked]
end

#click(keys = [], offset = {}) ⇒ Object



142
143
144
# File 'lib/capybara/poltergeist/node.rb', line 142

def click(keys=[], offset={})
  command :click, keys, offset
end

#command(name, *args) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/capybara/poltergeist/node.rb', line 18

def command(name, *args)
  browser.send(name, page_id, id, *args)
rescue BrowserError => error
  case error.name
  when 'Poltergeist.ObsoleteNode'
    raise ObsoleteNode.new(self, error.response)
  when 'Poltergeist.MouseEventFailed'
    raise MouseEventFailed.new(self, error.response)
  else
    raise
  end
end

#disabled?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/capybara/poltergeist/node.rb', line 138

def disabled?
  command :disabled?
end

#double_click(keys = [], offset = {}) ⇒ Object



150
151
152
# File 'lib/capybara/poltergeist/node.rb', line 150

def double_click(keys=[], offset={})
  command :double_click, keys, offset
end

#drag_by(x, y) ⇒ Object



162
163
164
# File 'lib/capybara/poltergeist/node.rb', line 162

def drag_by(x, y)
  command :drag_by, x, y
end

#drag_to(other) ⇒ Object



158
159
160
# File 'lib/capybara/poltergeist/node.rb', line 158

def drag_to(other)
  command :drag, other.id
end

#find(method, selector) ⇒ Object



35
36
37
# File 'lib/capybara/poltergeist/node.rb', line 35

def find(method, selector)
  command(:find_within, method, selector).map { |id| self.class.new(driver, page_id, id) }
end

#find_css(selector) ⇒ Object



43
44
45
# File 'lib/capybara/poltergeist/node.rb', line 43

def find_css(selector)
  find :css, selector
end

#find_xpath(selector) ⇒ Object



39
40
41
# File 'lib/capybara/poltergeist/node.rb', line 39

def find_xpath(selector)
  find :xpath, selector
end

#hoverObject



154
155
156
# File 'lib/capybara/poltergeist/node.rb', line 154

def hover
  command :hover
end

#parentsObject



31
32
33
# File 'lib/capybara/poltergeist/node.rb', line 31

def parents
  command(:parents).map { |parent_id| self.class.new(driver, page_id, parent_id) }
end

#pathObject



179
180
181
# File 'lib/capybara/poltergeist/node.rb', line 179

def path
  command :path
end

#property(name) ⇒ Object



63
64
65
# File 'lib/capybara/poltergeist/node.rb', line 63

def property(name)
  command :property, name
end

#right_click(keys = [], offset = {}) ⇒ Object



146
147
148
# File 'lib/capybara/poltergeist/node.rb', line 146

def right_click(keys=[], offset={})
  command :right_click, keys, offset
end

#select_optionObject



113
114
115
# File 'lib/capybara/poltergeist/node.rb', line 113

def select_option
  command :select, true
end

#selected?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/capybara/poltergeist/node.rb', line 134

def selected?
  !!self[:selected]
end

#send_keys(*keys) ⇒ Object Also known as: send_key



174
175
176
# File 'lib/capybara/poltergeist/node.rb', line 174

def send_keys(*keys)
  command :send_keys, keys
end

#set(value, options = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/capybara/poltergeist/node.rb', line 90

def set(value, options = {})
  warn "Options passed to Node#set but Poltergeist doesn't currently support any - ignoring" unless options.empty?

  if tag_name == 'input'
    case self[:type]
    when 'radio'
      click
    when 'checkbox'
      click if value != checked?
    when 'file'
      files = value.respond_to?(:to_ary) ? value.to_ary.map(&:to_s) : value.to_s
      command :select_file, files
    else
      command :set, value.to_s
    end
  elsif tag_name == 'textarea'
    command :set, value.to_s
  elsif self[:isContentEditable]
    command :delete_text
    send_keys(value.to_s)
  end
end

#tag_nameObject



122
123
124
# File 'lib/capybara/poltergeist/node.rb', line 122

def tag_name
  @tag_name ||= command(:tag_name)
end

#to_jsonObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



184
185
186
# File 'lib/capybara/poltergeist/node.rb', line 184

def to_json(*)
  JSON.generate as_json
end

#trigger(event) ⇒ Object



166
167
168
# File 'lib/capybara/poltergeist/node.rb', line 166

def trigger(event)
  command :trigger, event
end

#unselect_optionObject



117
118
119
120
# File 'lib/capybara/poltergeist/node.rb', line 117

def unselect_option
  command(:select, false) or
  raise(Capybara::UnselectNotAllowed, "Cannot unselect option from single select box.")
end

#valueObject



86
87
88
# File 'lib/capybara/poltergeist/node.rb', line 86

def value
  command :value
end

#visible?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/capybara/poltergeist/node.rb', line 126

def visible?
  command :visible?
end

#visible_textObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/capybara/poltergeist/node.rb', line 51

def visible_text
  if Capybara::VERSION.to_f < 3.0
    filter_text command(:visible_text)
  else
    command(:visible_text).to_s
                          .gsub(/\A[[:space:]&&[^\u00a0]]+/, "")
                          .gsub(/[[:space:]&&[^\u00a0]]+\z/, "")
                          .gsub(/\n+/, "\n")
                          .tr("\u00a0", ' ')
  end
end