cuprite 0.20.1 → 0.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +35 -31
- data/lib/capybara/cuprite/deprecation.rb +8 -1
- data/lib/capybara/cuprite/devices.rb +8 -0
- data/lib/capybara/cuprite/driver/deprecations.rb +22 -184
- data/lib/capybara/cuprite/driver/extensions.rb +50 -0
- data/lib/capybara/cuprite/driver/settings.rb +102 -0
- data/lib/capybara/cuprite/driver/window/frames.rb +35 -0
- data/lib/capybara/cuprite/driver/window/modals.rb +113 -0
- data/lib/capybara/cuprite/driver/window.rb +34 -0
- data/lib/capybara/cuprite/driver/windows.rb +90 -0
- data/lib/capybara/cuprite/driver.rb +331 -66
- data/lib/capybara/cuprite/errors.rb +48 -1
- data/lib/capybara/cuprite/finder/hints.rb +51 -0
- data/lib/capybara/cuprite/finder.rb +18 -3
- data/lib/capybara/cuprite/javascripts/index.js +123 -58
- data/lib/capybara/cuprite/node/extensions.rb +14 -4
- data/lib/capybara/cuprite/node/typing.rb +75 -0
- data/lib/capybara/cuprite/node.rb +208 -63
- data/lib/capybara/cuprite/version.rb +3 -1
- data/lib/capybara/cuprite.rb +1 -4
- data/sig/capybara/cuprite/deprecation.rbs +7 -0
- data/sig/capybara/cuprite/devices.rbs +19 -0
- data/sig/capybara/cuprite/driver/deprecations.rbs +11 -0
- data/sig/capybara/cuprite/driver/extensions.rbs +29 -0
- data/sig/capybara/cuprite/driver/settings.rbs +42 -0
- data/sig/capybara/cuprite/driver/window/frames.rbs +13 -0
- data/sig/capybara/cuprite/driver/window/modals.rbs +29 -0
- data/sig/capybara/cuprite/driver/window.rbs +22 -0
- data/sig/capybara/cuprite/driver/windows.rbs +27 -0
- data/sig/capybara/cuprite/driver.rbs +140 -0
- data/sig/capybara/cuprite/errors.rbs +56 -0
- data/sig/capybara/cuprite/finder/hints.rbs +28 -0
- data/sig/capybara/cuprite/finder.rbs +7 -0
- data/sig/capybara/cuprite/node/extensions.rbs +9 -0
- data/sig/capybara/cuprite/node/typing.rbs +30 -0
- data/sig/capybara/cuprite/node.rbs +112 -0
- data/sig/capybara/cuprite/version.rbs +5 -0
- metadata +27 -11
- data/lib/capybara/cuprite/browser/deprecations.rb +0 -26
- data/lib/capybara/cuprite/browser/extensions.rb +0 -59
- data/lib/capybara/cuprite/browser.rb +0 -108
- data/lib/capybara/cuprite/node/deprecations.rb +0 -43
- data/lib/capybara/cuprite/options.rb +0 -33
- data/lib/capybara/cuprite/page/frames.rb +0 -47
- data/lib/capybara/cuprite/page/modals.rb +0 -118
- data/lib/capybara/cuprite/page.rb +0 -36
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b426f124f9183e2706dba770a180fda5aef212aca138bb4c1d492bba978c0366
|
|
4
|
+
data.tar.gz: 93bdc1a0fe4001fdd87dc321ae08080a6fcdae7c0ea132b2ef0ca2bd23ddcf9f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 468585689677657989eda4e559817e247cd2efe51a21c1945d54bcd314f3c24f6af2910233e69a9ebdba1296ebf6e2dfa62bc9fc8f760da9952584eaefda48ef
|
|
7
|
+
data.tar.gz: d15dff0f72831e8930c55266e8e15a7e670b10062e07088e0589a762c1b12331a67155c40705833778d3b8bab8a90bea43a5b73da7d922c7cb34046f49e21704
|
data/README.md
CHANGED
|
@@ -59,11 +59,11 @@ a working sandbox when possible.
|
|
|
59
59
|
|
|
60
60
|
Since Cuprite uses [Ferrum](https://github.com/rubycdp/ferrum#examples) there
|
|
61
61
|
are many useful methods you can call even using this driver.
|
|
62
|
-
`page.driver.
|
|
62
|
+
`page.driver.current_page` is the Ferrum page of the window Capybara is switched to:
|
|
63
63
|
|
|
64
64
|
```ruby
|
|
65
|
-
|
|
66
|
-
|
|
65
|
+
page.driver.current_page.mouse.move(x: 123, y: 456).down.up
|
|
66
|
+
page.driver.current_page.network.traffic
|
|
67
67
|
```
|
|
68
68
|
|
|
69
69
|
For Selenium you better check your code for `manage` calls because it works
|
|
@@ -92,8 +92,8 @@ end
|
|
|
92
92
|
that triggered the dialog (a click, `visit`, `evaluate_script`, ...), since a JS dialog blocks the page until
|
|
93
93
|
answered, so that action's own command is what was waiting. A dialog fired with nothing in flight (e.g. a bare JS
|
|
94
94
|
timer) only surfaces on whatever command runs next.
|
|
95
|
-
* `:
|
|
96
|
-
* `:
|
|
95
|
+
* `:url_blocklist` (Array) - array of regexes to match against requested URLs, alias `:url_blacklist`
|
|
96
|
+
* `:url_allowlist` (Array) - array of regexes to match against requested URLs, alias `:url_whitelist`
|
|
97
97
|
* `:emulate` (Symbol) - device from Chrome DevTools' list to emulate, e.g. `:iphone_16`, `:pixel_9`, `:ipad_mini`
|
|
98
98
|
* `:mobile` (Boolean) - emulate a mobile device: viewport meta tag and touch events
|
|
99
99
|
* `:scale_factor` (Numeric) - device scale factor to emulate
|
|
@@ -130,7 +130,7 @@ opened where you can further experiment with the test.
|
|
|
130
130
|
|
|
131
131
|
## Clicking/Scrolling
|
|
132
132
|
|
|
133
|
-
* `page.driver.
|
|
133
|
+
* `page.driver.current_page.mouse.click(x:, y:)` Click a very specific area of the screen.
|
|
134
134
|
* `page.scroll_to(left, top)` Scroll to a given position.
|
|
135
135
|
* `element.send_keys(*keys)` Send keys to a given node.
|
|
136
136
|
|
|
@@ -140,10 +140,10 @@ opened where you can further experiment with the test.
|
|
|
140
140
|
Manipulate HTTP request headers with Ferrum:
|
|
141
141
|
|
|
142
142
|
``` ruby
|
|
143
|
-
page.driver.
|
|
144
|
-
page.driver.
|
|
145
|
-
page.driver.
|
|
146
|
-
page.driver.
|
|
143
|
+
page.driver.current_page.headers.get # => {}
|
|
144
|
+
page.driver.current_page.headers.set("X-Trace" => "1")
|
|
145
|
+
page.driver.current_page.headers.add({ "Referer" => "https://example.com" })
|
|
146
|
+
page.driver.current_page.headers.get # => { "X-Trace" => "1", "Referer" => "https://example.com" }
|
|
147
147
|
```
|
|
148
148
|
|
|
149
149
|
Notice that `set` will overwrite already set headers. You should use `add` if
|
|
@@ -154,11 +154,11 @@ cleared at the end of the test.
|
|
|
154
154
|
|
|
155
155
|
## Network traffic
|
|
156
156
|
|
|
157
|
-
* `page.driver.
|
|
157
|
+
* `page.driver.current_page.network.traffic` allows you to inspect network traffic (i.e., loaded resources) on the current page. It returns an array of `Ferrum::Network::Exchange` objects, each representing a network request/response exchange. You can query both the request and response details of each exchange.
|
|
158
158
|
|
|
159
159
|
```ruby
|
|
160
160
|
# Retrieve all network exchanges
|
|
161
|
-
network_traffic = page.driver.
|
|
161
|
+
network_traffic = page.driver.current_page.network.traffic
|
|
162
162
|
|
|
163
163
|
# Access the first exchange
|
|
164
164
|
first_exchange = network_traffic.first
|
|
@@ -167,27 +167,31 @@ first_exchange = network_traffic.first
|
|
|
167
167
|
response = first_exchange.response
|
|
168
168
|
```
|
|
169
169
|
|
|
170
|
-
* `page.driver.
|
|
171
|
-
and returns `
|
|
170
|
+
* `page.driver.current_page.network.wait_idle` Natively waits for network idle
|
|
171
|
+
and returns `nil` if there are still active connections after timeout.
|
|
172
172
|
Accepts the same options as
|
|
173
|
-
[`
|
|
173
|
+
[`wait_idle`](https://docs.rubycdp.com/docs/ferrum/network)
|
|
174
174
|
|
|
175
175
|
```ruby
|
|
176
|
-
page.driver.
|
|
176
|
+
page.driver.current_page.network.wait_idle
|
|
177
177
|
page.driver.refresh
|
|
178
178
|
```
|
|
179
179
|
|
|
180
180
|
Please note that network traffic is not cleared when you visit new page. You can
|
|
181
181
|
manually clear the network traffic by calling
|
|
182
|
-
`page.driver.
|
|
182
|
+
`page.driver.current_page.network.clear_traffic` or `page.driver.reset!`
|
|
183
183
|
|
|
184
|
-
* `page.driver.
|
|
185
|
-
the
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
184
|
+
* `page.driver.current_page.await_navigation(watch: 1) { ... }` runs the block and
|
|
185
|
+
waits until the page it navigates to is loaded, or the timeout passes. A click
|
|
186
|
+
already waits for the page it opens, but only for a navigation that starts within
|
|
187
|
+
`FERRUM_NAVIGATION_WAIT`, 0.1 seconds. Use it when the navigation starts later,
|
|
188
|
+
for example when an autocomplete suggestion loads data first and only then opens
|
|
189
|
+
the page. Sleep makes you wait longer or less than needed, and network idle can
|
|
190
|
+
return before the navigation even started.
|
|
191
|
+
|
|
192
|
+
```ruby
|
|
193
|
+
page.driver.current_page.await_navigation(watch: 1) { find(".suggestion").click }
|
|
194
|
+
```
|
|
191
195
|
|
|
192
196
|
|
|
193
197
|
## Manipulating cookies
|
|
@@ -195,7 +199,7 @@ rescue.
|
|
|
195
199
|
Use Ferrum's cookies to inspect and manipulate cookies:
|
|
196
200
|
|
|
197
201
|
```ruby
|
|
198
|
-
cookies = page.driver.
|
|
202
|
+
cookies = page.driver.current_page.cookies
|
|
199
203
|
cookies.set(name: "stealth", value: "omg")
|
|
200
204
|
cookies["stealth"].value # => "omg"
|
|
201
205
|
cookies.all # => { "stealth" => #<Ferrum::Cookies::Cookie ...> }
|
|
@@ -209,14 +213,14 @@ cookies.clear
|
|
|
209
213
|
Besides capybara screenshot method you can get image or PDF as Base64 with Ferrum:
|
|
210
214
|
|
|
211
215
|
```ruby
|
|
212
|
-
page.driver.
|
|
213
|
-
page.driver.
|
|
216
|
+
page.driver.current_page.screenshot(format: :jpeg, full: true)
|
|
217
|
+
page.driver.current_page.pdf(paper_width: 6, paper_height: 12)
|
|
214
218
|
```
|
|
215
219
|
|
|
216
220
|
|
|
217
221
|
## Authorization
|
|
218
222
|
|
|
219
|
-
* `page.driver.
|
|
223
|
+
* `page.driver.current_page.network.authorize(user:, password:) { |request, _index, _total| request.continue }`
|
|
220
224
|
|
|
221
225
|
## Proxy
|
|
222
226
|
|
|
@@ -239,17 +243,17 @@ Cuprite supports URL blocklisting, which allows you to prevent scripts from
|
|
|
239
243
|
running on designated domains:
|
|
240
244
|
|
|
241
245
|
```ruby
|
|
242
|
-
page.driver.
|
|
246
|
+
page.driver.url_blocklist = %r{http://www.example.com}
|
|
243
247
|
```
|
|
244
248
|
|
|
245
249
|
and also URL allowlisting, which allows scripts to only run on designated
|
|
246
250
|
domains:
|
|
247
251
|
|
|
248
252
|
```ruby
|
|
249
|
-
page.driver.
|
|
253
|
+
page.driver.url_allowlist = %r{http://www.example.com}
|
|
250
254
|
```
|
|
251
255
|
|
|
252
|
-
|
|
256
|
+
`url_blacklist=` and `url_whitelist=` are aliases of the same setters. Only one list can be set at a time.
|
|
253
257
|
|
|
254
258
|
If you are experiencing slower run times, consider creating a URL allowlist of
|
|
255
259
|
domains that are essential or a blocklist of domains that are not essential,
|
|
@@ -2,8 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
module Capybara
|
|
4
4
|
module Cuprite
|
|
5
|
-
|
|
5
|
+
# The release that removes what's deprecated now.
|
|
6
|
+
REMOVE_DEPRECATED_IN = "1.0"
|
|
6
7
|
|
|
8
|
+
# Warns that a method is deprecated, pointing at the code that called it.
|
|
9
|
+
#
|
|
10
|
+
# @api private
|
|
11
|
+
# @param method [String] the deprecated call, e.g. `"page.driver.browser.url_blocklist="`
|
|
12
|
+
# @param advice [String, nil] what to use instead
|
|
13
|
+
# @return [void]
|
|
7
14
|
def self.deprecation_warning(method, advice = nil)
|
|
8
15
|
message = "#{method} is deprecated and will be removed in #{REMOVE_DEPRECATED_IN}"
|
|
9
16
|
message += ", #{advice} instead" if advice
|
|
@@ -2,11 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
module Capybara
|
|
4
4
|
module Cuprite
|
|
5
|
+
# The devices `emulate:` takes, those listed in Chrome DevTools' device toolbar.
|
|
5
6
|
module Devices
|
|
7
|
+
# @api private
|
|
6
8
|
IPHONE_USER_AGENT = "Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Mobile/15E148 Safari/604.1"
|
|
9
|
+
# @api private
|
|
7
10
|
SAMSUNG_GALAXY_S8_PLUS_USER_AGENT = "Mozilla/5.0 (Linux; Android 8.0.0; SM-G955U Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%<version>s Mobile Safari/537.36"
|
|
11
|
+
# @api private
|
|
8
12
|
WINDOWS_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%<version>s Safari/537.36"
|
|
9
13
|
|
|
14
|
+
# Each device's `:window_size`, `:scale_factor`, `:mobile` and `:user_agent`, by name.
|
|
10
15
|
LIST = {
|
|
11
16
|
iphone_se: {
|
|
12
17
|
window_size: [375, 667], scale_factor: 2, mobile: true,
|
|
@@ -178,6 +183,9 @@ module Capybara
|
|
|
178
183
|
}
|
|
179
184
|
}.freeze
|
|
180
185
|
|
|
186
|
+
# @param name [Symbol, String] a key of {LIST}
|
|
187
|
+
# @return [Hash] the device's options
|
|
188
|
+
# @raise [ArgumentError] for an unknown device
|
|
181
189
|
def self.fetch(name)
|
|
182
190
|
LIST.fetch(name.to_sym) do
|
|
183
191
|
raise ArgumentError, "Unknown device to emulate: #{name.inspect}, use one of #{LIST.keys.join(', ')}"
|
|
@@ -1,197 +1,35 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "uri"
|
|
4
3
|
require "capybara/cuprite/deprecation"
|
|
5
4
|
|
|
6
5
|
module Capybara
|
|
7
6
|
module Cuprite
|
|
8
7
|
class Driver < Capybara::Driver::Base
|
|
8
|
+
# The settings that moved from `page.driver.browser` to `page.driver`. They stay on the browser until 1.0 and
|
|
9
|
+
# forward to the driver with a warning.
|
|
10
|
+
# @api private
|
|
9
11
|
module Deprecations
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
# The settings the browser keeps until 1.0.
|
|
13
|
+
BROWSER_METHODS = %i[raise_on_unhandled_modal raise_on_unhandled_modal=
|
|
14
|
+
url_whitelist url_whitelist= url_allowlist url_allowlist=
|
|
15
|
+
url_blacklist url_blacklist= url_blocklist url_blocklist=].freeze
|
|
16
|
+
|
|
17
|
+
module_function
|
|
18
|
+
|
|
19
|
+
# Adds the moved settings to the browser, forwarding them to the driver with a warning. Only this browser
|
|
20
|
+
# object gets them, `Ferrum::Browser` stays as it is.
|
|
21
|
+
#
|
|
22
|
+
# @param browser [Ferrum::Browser]
|
|
23
|
+
# @param driver [Driver]
|
|
24
|
+
# @return [void]
|
|
25
|
+
def extend_browser(browser, driver)
|
|
26
|
+
BROWSER_METHODS.each do |name|
|
|
27
|
+
browser.define_singleton_method(name) do |*args|
|
|
28
|
+
Cuprite.deprecation_warning("page.driver.browser.#{name}", "use page.driver.#{name}")
|
|
29
|
+
driver.public_send(name, *args)
|
|
30
|
+
end
|
|
14
31
|
end
|
|
15
32
|
end
|
|
16
|
-
private_class_method :deprecate
|
|
17
|
-
|
|
18
|
-
deprecate :body, :html
|
|
19
|
-
deprecate :window_handle, :current_window_handle
|
|
20
|
-
deprecate :back, :go_back
|
|
21
|
-
deprecate :forward, :go_forward
|
|
22
|
-
deprecate :render, :save_screenshot
|
|
23
|
-
|
|
24
|
-
def render_base64(format = :png, options = {})
|
|
25
|
-
Cuprite.deprecation_warning("#{self.class}#render_base64", "use page.driver.browser.screenshot or .pdf")
|
|
26
|
-
browser.screenshot(format: format, encoding: :base64, **options)
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
def zoom_factor=(value)
|
|
30
|
-
Cuprite.deprecation_warning("#{self.class}#zoom_factor=", "pass scale: to save_screenshot")
|
|
31
|
-
@zoom_factor = value.to_f
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def paper_size=(value)
|
|
35
|
-
Cuprite.deprecation_warning("#{self.class}#paper_size=",
|
|
36
|
-
"pass paper_width: and paper_height: to save_screenshot")
|
|
37
|
-
@paper_size = value
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
def resize(width, height)
|
|
41
|
-
Cuprite.deprecation_warning("#{self.class}##{__callee__}", "use page.current_window.resize_to")
|
|
42
|
-
resize_window_to(current_window_handle, width, height)
|
|
43
|
-
end
|
|
44
|
-
alias resize_window resize
|
|
45
|
-
|
|
46
|
-
def click(x, y)
|
|
47
|
-
Cuprite.deprecation_warning("#{self.class}#click", "use page.driver.browser.mouse.click")
|
|
48
|
-
browser.mouse.click(x: x, y: y)
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
def scroll_to(left, top)
|
|
52
|
-
Cuprite.deprecation_warning("#{self.class}#scroll_to", "use page.scroll_to")
|
|
53
|
-
browser.mouse.scroll_to(left, top)
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
def set_proxy(host, port, user = nil, password = nil, bypass = nil)
|
|
57
|
-
Cuprite.deprecation_warning("#{self.class}#set_proxy", "pass the :proxy option to the driver")
|
|
58
|
-
@options.merge!(proxy: { host: host, port: port, user: user, password: password, bypass: bypass })
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
def restart
|
|
62
|
-
Cuprite.deprecation_warning("#{self.class}#restart", "use page.driver.browser.restart")
|
|
63
|
-
browser.restart
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
def wait_for_reload(timeout = 1)
|
|
67
|
-
Cuprite.deprecation_warning("#{self.class}#wait_for_reload", "use page.driver.browser.wait_for_reload")
|
|
68
|
-
browser.wait_for_reload(timeout)
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
def timeout
|
|
72
|
-
Cuprite.deprecation_warning("#{self.class}#timeout", "use page.driver.browser.timeout")
|
|
73
|
-
browser.timeout
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
def timeout=(value)
|
|
77
|
-
Cuprite.deprecation_warning("#{self.class}#timeout=", "use page.driver.browser.timeout=")
|
|
78
|
-
browser.timeout = value
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
def device_pixel_ratio
|
|
82
|
-
Cuprite.deprecation_warning("#{self.class}#device_pixel_ratio", "use page.driver.browser.device_pixel_ratio")
|
|
83
|
-
browser.device_pixel_ratio
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
def viewport_size
|
|
87
|
-
Cuprite.deprecation_warning("#{self.class}#viewport_size", "use page.driver.browser.viewport_size")
|
|
88
|
-
browser.viewport_size
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
def authorize(user, password)
|
|
92
|
-
Cuprite.deprecation_warning("#{self.class}##{__callee__}", "use page.driver.browser.network.authorize")
|
|
93
|
-
browser.network.authorize(user: user, password: password) do |request, _index, _total|
|
|
94
|
-
request.continue
|
|
95
|
-
end
|
|
96
|
-
end
|
|
97
|
-
alias basic_authorize authorize
|
|
98
|
-
|
|
99
|
-
def headers
|
|
100
|
-
Cuprite.deprecation_warning("#{self.class}#headers", "use page.driver.browser.headers.get")
|
|
101
|
-
browser.headers.get
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
def headers=(headers)
|
|
105
|
-
Cuprite.deprecation_warning("#{self.class}#headers=", "use page.driver.browser.headers.set")
|
|
106
|
-
browser.headers.set(headers)
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
def add_headers(headers)
|
|
110
|
-
Cuprite.deprecation_warning("#{self.class}#add_headers", "use page.driver.browser.headers.add")
|
|
111
|
-
browser.headers.add(headers)
|
|
112
|
-
end
|
|
113
|
-
|
|
114
|
-
def add_header(name, value, permanent: true)
|
|
115
|
-
Cuprite.deprecation_warning("#{self.class}#add_header", "use page.driver.browser.headers.add")
|
|
116
|
-
browser.headers.add({ name => value }, permanent: permanent)
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
def cookies
|
|
120
|
-
Cuprite.deprecation_warning("#{self.class}#cookies", "use page.driver.browser.cookies.all")
|
|
121
|
-
browser.cookies.all
|
|
122
|
-
end
|
|
123
|
-
|
|
124
|
-
def set_cookie(name, value, options = {})
|
|
125
|
-
Cuprite.deprecation_warning("#{self.class}#set_cookie", "use page.driver.browser.cookies.set")
|
|
126
|
-
options = options.dup
|
|
127
|
-
options[:name] ||= name
|
|
128
|
-
options[:value] ||= value
|
|
129
|
-
options[:domain] ||= default_domain
|
|
130
|
-
browser.cookies.set(**options)
|
|
131
|
-
end
|
|
132
|
-
|
|
133
|
-
def remove_cookie(name, **options)
|
|
134
|
-
Cuprite.deprecation_warning("#{self.class}#remove_cookie", "use page.driver.browser.cookies.remove")
|
|
135
|
-
options[:domain] = default_domain if options.empty?
|
|
136
|
-
options = options.merge(name: name)
|
|
137
|
-
browser.cookies.remove(**options)
|
|
138
|
-
end
|
|
139
|
-
|
|
140
|
-
def clear_cookies
|
|
141
|
-
Cuprite.deprecation_warning("#{self.class}#clear_cookies", "use page.driver.browser.cookies.clear")
|
|
142
|
-
browser.cookies.clear
|
|
143
|
-
end
|
|
144
|
-
|
|
145
|
-
def network_traffic(type = nil)
|
|
146
|
-
Cuprite.deprecation_warning("#{self.class}#network_traffic", "use page.driver.browser.network.traffic")
|
|
147
|
-
traffic = browser.network.traffic
|
|
148
|
-
|
|
149
|
-
case type.to_s
|
|
150
|
-
when "all"
|
|
151
|
-
traffic
|
|
152
|
-
when "blocked"
|
|
153
|
-
traffic.select(&:blocked?)
|
|
154
|
-
else
|
|
155
|
-
# when request isn't blocked
|
|
156
|
-
traffic.reject(&:blocked?)
|
|
157
|
-
end
|
|
158
|
-
end
|
|
159
|
-
|
|
160
|
-
def clear_network_traffic
|
|
161
|
-
Cuprite.deprecation_warning("#{self.class}#clear_network_traffic",
|
|
162
|
-
"use page.driver.browser.network.clear(:traffic)")
|
|
163
|
-
browser.network.clear(:traffic)
|
|
164
|
-
end
|
|
165
|
-
|
|
166
|
-
def wait_for_network_idle(**options)
|
|
167
|
-
Cuprite.deprecation_warning("#{self.class}#wait_for_network_idle",
|
|
168
|
-
"use page.driver.browser.network.wait_for_idle")
|
|
169
|
-
browser.network.wait_for_idle(**options)
|
|
170
|
-
end
|
|
171
|
-
|
|
172
|
-
def clear_memory_cache
|
|
173
|
-
Cuprite.deprecation_warning("#{self.class}#clear_memory_cache",
|
|
174
|
-
"use page.driver.browser.network.clear(:cache)")
|
|
175
|
-
browser.network.clear(:cache)
|
|
176
|
-
end
|
|
177
|
-
|
|
178
|
-
private
|
|
179
|
-
|
|
180
|
-
def default_domain
|
|
181
|
-
if @started
|
|
182
|
-
URI.parse(browser.current_url).host
|
|
183
|
-
else
|
|
184
|
-
URI.parse(default_cookie_host).host || "127.0.0.1"
|
|
185
|
-
end
|
|
186
|
-
end
|
|
187
|
-
|
|
188
|
-
def default_cookie_host
|
|
189
|
-
if respond_to?(:session_options)
|
|
190
|
-
session_options.app_host
|
|
191
|
-
else
|
|
192
|
-
Capybara.app_host
|
|
193
|
-
end || ""
|
|
194
|
-
end
|
|
195
33
|
end
|
|
196
34
|
end
|
|
197
35
|
end
|
|
@@ -3,10 +3,60 @@
|
|
|
3
3
|
module Capybara
|
|
4
4
|
module Cuprite
|
|
5
5
|
class Driver < Capybara::Driver::Base
|
|
6
|
+
# Driver methods Capybara doesn't need, and settings changeable between tests through `page.driver`.
|
|
6
7
|
module Extensions
|
|
8
|
+
# Opens the current page in DevTools and pauses, taking the arguments of `Ferrum::Browser#debug`.
|
|
9
|
+
#
|
|
10
|
+
# @return [void]
|
|
7
11
|
def debug(...)
|
|
8
12
|
browser.debug(...)
|
|
9
13
|
end
|
|
14
|
+
|
|
15
|
+
# @return [Boolean, nil] whether an unhandled dialog raises {UnhandledModalError} instead of warning
|
|
16
|
+
def raise_on_unhandled_modal
|
|
17
|
+
settings.raise_on_unhandled_modal
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# @param value [Boolean]
|
|
21
|
+
# @return [void]
|
|
22
|
+
def raise_on_unhandled_modal=(value)
|
|
23
|
+
settings.raise_on_unhandled_modal = value
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# @return [Array<Regexp>] the URLs requests are allowed to, all others are blocked
|
|
27
|
+
def url_whitelist
|
|
28
|
+
settings.url_whitelist
|
|
29
|
+
end
|
|
30
|
+
alias url_allowlist url_whitelist
|
|
31
|
+
|
|
32
|
+
# Allows requests only to matching URLs, from the current page on. An empty list lifts the allowlist from the
|
|
33
|
+
# next page on, the current page keeps the previous one.
|
|
34
|
+
#
|
|
35
|
+
# @param patterns [Regexp, Array<Regexp>, nil]
|
|
36
|
+
# @return [void]
|
|
37
|
+
# @raise [ArgumentError] for a pattern that isn't a Regexp, or when a blocklist is set
|
|
38
|
+
def url_whitelist=(patterns)
|
|
39
|
+
settings.url_whitelist = patterns
|
|
40
|
+
current_page.network.whitelist = settings.url_whitelist if settings.url_whitelist.any?
|
|
41
|
+
end
|
|
42
|
+
alias url_allowlist= url_whitelist=
|
|
43
|
+
|
|
44
|
+
# @return [Array<Regexp>] the URLs requests to are blocked
|
|
45
|
+
def url_blacklist
|
|
46
|
+
settings.url_blacklist
|
|
47
|
+
end
|
|
48
|
+
alias url_blocklist url_blacklist
|
|
49
|
+
|
|
50
|
+
# Blocks requests to matching URLs, from the current page on. An empty list lifts the blocklist.
|
|
51
|
+
#
|
|
52
|
+
# @param patterns [Regexp, Array<Regexp>, nil]
|
|
53
|
+
# @return [void]
|
|
54
|
+
# @raise [ArgumentError] for a pattern that isn't a Regexp, or when an allowlist is set
|
|
55
|
+
def url_blacklist=(patterns)
|
|
56
|
+
settings.url_blacklist = patterns
|
|
57
|
+
current_page.network.blacklist = settings.url_blacklist
|
|
58
|
+
end
|
|
59
|
+
alias url_blocklist= url_blacklist=
|
|
10
60
|
end
|
|
11
61
|
end
|
|
12
62
|
end
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Capybara
|
|
4
|
+
module Cuprite
|
|
5
|
+
class Driver < Capybara::Driver::Base
|
|
6
|
+
# The driver options only Cuprite handles, which {Driver} sets every page up with.
|
|
7
|
+
# @api private
|
|
8
|
+
class Settings
|
|
9
|
+
# @return [Boolean, nil] whether an unhandled dialog raises {UnhandledModalError} instead of warning
|
|
10
|
+
attr_accessor :raise_on_unhandled_modal
|
|
11
|
+
# @return [Array(Integer, Integer), Array(nil, nil), nil] the size the windows opened next get
|
|
12
|
+
attr_accessor :window_size
|
|
13
|
+
# @return [Array<Regexp>]
|
|
14
|
+
attr_reader :url_blacklist
|
|
15
|
+
# @return [Array<Regexp>]
|
|
16
|
+
attr_reader :url_whitelist
|
|
17
|
+
# @return [Boolean, nil]
|
|
18
|
+
attr_reader :mobile
|
|
19
|
+
# @return [Numeric]
|
|
20
|
+
attr_reader :scale_factor
|
|
21
|
+
|
|
22
|
+
# @param options [Hash, nil] the {Driver} options, with those of the `emulate:` device merged in
|
|
23
|
+
# @param window_size [Array(Integer, Integer), nil] the window size Ferrum launched Chrome with
|
|
24
|
+
# @raise [ArgumentError] for a URL pattern that isn't a Regexp, both URL lists, or an unknown device
|
|
25
|
+
def initialize(options, window_size)
|
|
26
|
+
@options = Hash(options)
|
|
27
|
+
@js_errors = @options[:js_errors]
|
|
28
|
+
@pending_connection_errors = @options.fetch(:pending_connection_errors, true)
|
|
29
|
+
@device_user_agent = Devices.fetch(@options[:emulate])[:user_agent] if @options[:emulate]
|
|
30
|
+
@mobile = @options[:mobile]
|
|
31
|
+
@scale_factor = @options.fetch(:scale_factor, 0)
|
|
32
|
+
@initial_window_size = window_size
|
|
33
|
+
reset
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# @return [Boolean] whether JavaScript errors on the page are raised in Ruby
|
|
37
|
+
def js_errors?
|
|
38
|
+
!!@js_errors
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# @return [Boolean] whether `visit` raises {PendingConnectionsError} when the page doesn't load in time
|
|
42
|
+
def pending_connection_errors?
|
|
43
|
+
!!@pending_connection_errors
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# @param patterns [Regexp, Array<Regexp>, nil]
|
|
47
|
+
# @return [void]
|
|
48
|
+
# @raise [ArgumentError] for a pattern that isn't a Regexp, or when a whitelist is set
|
|
49
|
+
def url_blacklist=(patterns)
|
|
50
|
+
patterns = regexp_patterns(patterns)
|
|
51
|
+
raise ArgumentError, "You can't use blacklist along with whitelist" if patterns.any? && url_whitelist.any?
|
|
52
|
+
|
|
53
|
+
@url_blacklist = patterns
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# @param patterns [Regexp, Array<Regexp>, nil]
|
|
57
|
+
# @return [void]
|
|
58
|
+
# @raise [ArgumentError] for a pattern that isn't a Regexp, or when a blacklist is set
|
|
59
|
+
def url_whitelist=(patterns)
|
|
60
|
+
patterns = regexp_patterns(patterns)
|
|
61
|
+
raise ArgumentError, "You can't use whitelist along with blacklist" if patterns.any? && url_blacklist.any?
|
|
62
|
+
|
|
63
|
+
@url_whitelist = patterns
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Restores the URL lists, the modal behavior and the window size of the {Driver} options.
|
|
67
|
+
#
|
|
68
|
+
# @return [void]
|
|
69
|
+
def reset
|
|
70
|
+
@raise_on_unhandled_modal = @options[:raise_on_unhandled_modal]
|
|
71
|
+
@window_size = @initial_window_size
|
|
72
|
+
@url_blacklist = @url_whitelist = []
|
|
73
|
+
self.url_blacklist = @options[:url_blacklist]
|
|
74
|
+
self.url_whitelist = @options[:url_whitelist]
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# @return [Boolean] whether an `emulate:` device sets the user agent
|
|
78
|
+
def device_user_agent?
|
|
79
|
+
!@device_user_agent.nil?
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# @param chrome_user_agent [String, nil] Chrome's own user agent
|
|
83
|
+
# @return [String, nil] the emulated device's user agent with Chrome's version, or Chrome's own
|
|
84
|
+
def user_agent(chrome_user_agent)
|
|
85
|
+
return chrome_user_agent unless @device_user_agent
|
|
86
|
+
|
|
87
|
+
format(@device_user_agent, version: chrome_user_agent.to_s[%r{Chrome/(\S+)}, 1].to_s)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
private
|
|
91
|
+
|
|
92
|
+
def regexp_patterns(patterns)
|
|
93
|
+
Array(patterns).each do |pattern|
|
|
94
|
+
next if pattern.is_a?(Regexp)
|
|
95
|
+
|
|
96
|
+
raise ArgumentError, "url_blacklist/url_whitelist take regexps, got #{pattern.inspect}"
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Capybara
|
|
4
|
+
module Cuprite
|
|
5
|
+
class Driver < Capybara::Driver::Base
|
|
6
|
+
class Window
|
|
7
|
+
# Keeps the frame the page's DOM and JavaScript calls run in.
|
|
8
|
+
# @api private
|
|
9
|
+
module Frames
|
|
10
|
+
# @param handle [String, :parent, :top] a frame id, or the parent or top frame
|
|
11
|
+
# @return [void]
|
|
12
|
+
def switch_to_frame(handle)
|
|
13
|
+
case handle
|
|
14
|
+
when :parent
|
|
15
|
+
@frame_stack.pop
|
|
16
|
+
when :top
|
|
17
|
+
@frame_stack = []
|
|
18
|
+
else
|
|
19
|
+
@frame_stack << handle
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# @return [Ferrum::Frame] the frame switched to, the main frame by default
|
|
24
|
+
def current_frame
|
|
25
|
+
if @frame_stack.empty?
|
|
26
|
+
page.main_frame
|
|
27
|
+
else
|
|
28
|
+
page.frame_by(id: @frame_stack.last)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|