Class: Capybara::Cuprite::Browser

Inherits:
Ferrum::Browser show all
Extended by:
Forwardable
Defined in:
lib/capybara/cuprite/browser.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Browser

Returns a new instance of Browser.



14
15
16
17
18
19
20
21
# File 'lib/capybara/cuprite/browser.rb', line 14

def initialize(options = nil)
  super

  @options.url_blacklist = prepare_wildcards(options&.dig(:url_blacklist))
  @options.url_whitelist = prepare_wildcards(options&.dig(:url_whitelist))

  @page = nil
end

Instance Method Details

#active_elementObject



130
131
132
# File 'lib/capybara/cuprite/browser.rb', line 130

def active_element
  evaluate("document.activeElement")
end

#all_text(node) ⇒ Object



206
207
208
# File 'lib/capybara/cuprite/browser.rb', line 206

def all_text(node)
  node.text
end

#attribute(node, name) ⇒ Object



186
187
188
# File 'lib/capybara/cuprite/browser.rb', line 186

def attribute(node, name)
  evaluate_on(node: node, expression: %(_cuprite.getAttribute(this, "#{name}")))
end

#attributes(node) ⇒ Object



181
182
183
184
# File 'lib/capybara/cuprite/browser.rb', line 181

def attributes(node)
  value = evaluate_on(node: node, expression: "_cuprite.getAttributes(this)")
  JSON.parse(value)
end

#browser_errorObject



134
135
136
# File 'lib/capybara/cuprite/browser.rb', line 134

def browser_error
  evaluate("_cuprite.browserError()")
end

#close_window(target_id) ⇒ Object

Raises:

  • (Ferrum::NoSuchPageError)


121
122
123
124
125
126
127
128
# File 'lib/capybara/cuprite/browser.rb', line 121

def close_window(target_id)
  target = targets[target_id]
  raise Ferrum::NoSuchPageError unless target

  @page = ClosedPage.new if @page.target_id == target.id
  target.page.close
  targets.delete(target_id) # page.close is async, delete target asap
end

#commandObject



23
24
25
26
27
28
# File 'lib/capybara/cuprite/browser.rb', line 23

def command(...)
  super
rescue Ferrum::DeadBrowserError
  restart
  raise
end

#delete_text(node) ⇒ Object



177
178
179
# File 'lib/capybara/cuprite/browser.rb', line 177

def delete_text(node)
  evaluate_on(node: node, expression: "_cuprite.deleteText(this)")
end

#disabled?(node) ⇒ Boolean

Returns:

  • (Boolean)


198
199
200
# File 'lib/capybara/cuprite/browser.rb', line 198

def disabled?(node)
  evaluate_on(node: node, expression: "_cuprite.isDisabled(this)")
end

#drag(node, other, steps, delay = nil) ⇒ Object



142
143
144
145
146
147
148
149
150
151
# File 'lib/capybara/cuprite/browser.rb', line 142

def drag(node, other, steps, delay = nil)
  x1, y1 = node.find_position
  x2, y2 = other.find_position

  mouse.move(x: x1, y: y1)
  mouse.down
  sleep delay if delay
  mouse.move(x: x2, y: y2, steps: steps)
  mouse.up
end

#drag_by(node, x, y, steps, delay = nil) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/capybara/cuprite/browser.rb', line 153

def drag_by(node, x, y, steps, delay = nil)
  x1, y1 = node.find_position
  x2 = x1 + x
  y2 = y1 + y

  mouse.move(x: x1, y: y1)
  mouse.down
  sleep delay if delay
  mouse.move(x: x2, y: y2, steps: steps)
  mouse.up
end

#find(method, selector) ⇒ Object



82
83
84
# File 'lib/capybara/cuprite/browser.rb', line 82

def find(method, selector)
  find_all(method, selector)
end

#find_within(node, method, selector) ⇒ Object



90
91
92
93
94
# File 'lib/capybara/cuprite/browser.rb', line 90

def find_within(node, method, selector)
  resolved = page.command("DOM.resolveNode", nodeId: node.node_id)
  object_id = resolved.dig("object", "objectId")
  find_all(method, selector, { "objectId" => object_id })
end

#pageObject

Raises:

  • (Ferrum::NoSuchPageError)


30
31
32
33
34
# File 'lib/capybara/cuprite/browser.rb', line 30

def page
  raise Ferrum::NoSuchPageError if @page&.closed?

  @page ||= attach_page
end

#parents(node) ⇒ Object



169
170
171
# File 'lib/capybara/cuprite/browser.rb', line 169

def parents(node)
  evaluate_on(node: node, expression: "_cuprite.parents(this)", by_value: false)
end

#path(node) ⇒ Object



202
203
204
# File 'lib/capybara/cuprite/browser.rb', line 202

def path(node)
  evaluate_on(node: node, expression: "_cuprite.path(this)")
end

#property(node, name) ⇒ Object



86
87
88
# File 'lib/capybara/cuprite/browser.rb', line 86

def property(node, name)
  node.property(name)
end

#quitObject



42
43
44
45
# File 'lib/capybara/cuprite/browser.rb', line 42

def quit
  super
  @page = nil
end

#resetObject



36
37
38
39
40
# File 'lib/capybara/cuprite/browser.rb', line 36

def reset
  super
  @options.reset_window_size
  @page = nil
end

#resize(**options) ⇒ Object



47
48
49
50
# File 'lib/capybara/cuprite/browser.rb', line 47

def resize(**options)
  @options.window_size = [options[:width], options[:height]]
  super
end

#select_file(node, value) ⇒ Object



165
166
167
# File 'lib/capybara/cuprite/browser.rb', line 165

def select_file(node, value)
  node.select_file(value)
end

#sourceObject

Raises:

  • (NotImplementedError)


138
139
140
# File 'lib/capybara/cuprite/browser.rb', line 138

def source
  raise NotImplementedError
end

#status_codeObject



78
79
80
# File 'lib/capybara/cuprite/browser.rb', line 78

def status_code
  network.status
end

#switch_to_window(target_id) ⇒ Object

Raises:

  • (Ferrum::NoSuchPageError)


114
115
116
117
118
119
# File 'lib/capybara/cuprite/browser.rb', line 114

def switch_to_window(target_id)
  target = targets[target_id]
  raise Ferrum::NoSuchPageError unless target

  @page = attach_page(target.id)
end

#url_blacklistObject Also known as: url_blocklist



63
64
65
# File 'lib/capybara/cuprite/browser.rb', line 63

def url_blacklist
  @options.url_blacklist
end

#url_blacklist=(patterns) ⇒ Object Also known as: url_blocklist=



68
69
70
71
# File 'lib/capybara/cuprite/browser.rb', line 68

def url_blacklist=(patterns)
  @options.url_blacklist = prepare_wildcards(patterns)
  page.network.blacklist = @options.url_blacklist if @client && @options.url_blacklist.any?
end

#url_whitelistObject Also known as: url_allowlist



52
53
54
# File 'lib/capybara/cuprite/browser.rb', line 52

def url_whitelist
  @options.url_whitelist
end

#url_whitelist=(patterns) ⇒ Object Also known as: url_allowlist=



57
58
59
60
# File 'lib/capybara/cuprite/browser.rb', line 57

def url_whitelist=(patterns)
  @options.url_whitelist = prepare_wildcards(patterns)
  page.network.whitelist = @options.url_whitelist if @client && @options.url_whitelist.any?
end

#value(node) ⇒ Object



190
191
192
# File 'lib/capybara/cuprite/browser.rb', line 190

def value(node)
  evaluate_on(node: node, expression: "_cuprite.value(this)")
end

#visible?(node) ⇒ Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/capybara/cuprite/browser.rb', line 194

def visible?(node)
  evaluate_on(node: node, expression: "_cuprite.isVisible(this)")
end

#visible_text(node) ⇒ Object



173
174
175
# File 'lib/capybara/cuprite/browser.rb', line 173

def visible_text(node)
  evaluate_on(node: node, expression: "_cuprite.visibleText(this)")
end

#visit(*args) ⇒ Object



74
75
76
# File 'lib/capybara/cuprite/browser.rb', line 74

def visit(*args)
  goto(*args)
end

#window_handleObject



96
97
98
# File 'lib/capybara/cuprite/browser.rb', line 96

def window_handle
  page.target_id
end

#window_handlesObject



100
101
102
# File 'lib/capybara/cuprite/browser.rb', line 100

def window_handles
  targets.keys
end

#within_window(locator = nil) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/capybara/cuprite/browser.rb', line 104

def within_window(locator = nil)
  original = window_handle
  raise Ferrum::NoSuchPageError unless window_handles.include?(locator)

  switch_to_window(locator)
  yield
ensure
  switch_to_window(original)
end