cuprite 0.21 → 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.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +27 -25
  3. data/lib/capybara/cuprite/deprecation.rb +20 -0
  4. data/lib/capybara/cuprite/driver/deprecations.rb +36 -0
  5. data/lib/capybara/cuprite/driver/extensions.rb +47 -1
  6. data/lib/capybara/cuprite/{browser → driver}/settings.rb +2 -2
  7. data/lib/capybara/cuprite/driver/window/frames.rb +35 -0
  8. data/lib/capybara/cuprite/driver/window/modals.rb +113 -0
  9. data/lib/capybara/cuprite/driver/window.rb +34 -0
  10. data/lib/capybara/cuprite/driver/windows.rb +90 -0
  11. data/lib/capybara/cuprite/driver.rb +171 -67
  12. data/lib/capybara/cuprite/errors.rb +1 -23
  13. data/lib/capybara/cuprite/finder.rb +4 -4
  14. data/lib/capybara/cuprite/node/extensions.rb +1 -1
  15. data/lib/capybara/cuprite/node/typing.rb +1 -1
  16. data/lib/capybara/cuprite/node.rb +23 -27
  17. data/lib/capybara/cuprite/version.rb +1 -1
  18. data/lib/capybara/cuprite.rb +0 -7
  19. data/sig/capybara/cuprite/deprecation.rbs +7 -0
  20. data/sig/capybara/cuprite/driver/deprecations.rbs +11 -0
  21. data/sig/capybara/cuprite/driver/extensions.rbs +20 -0
  22. data/sig/capybara/cuprite/{browser → driver}/settings.rbs +1 -1
  23. data/sig/capybara/cuprite/driver/window/frames.rbs +13 -0
  24. data/sig/capybara/cuprite/driver/window/modals.rbs +29 -0
  25. data/sig/capybara/cuprite/driver/window.rbs +22 -0
  26. data/sig/capybara/cuprite/driver/windows.rbs +27 -0
  27. data/sig/capybara/cuprite/driver.rbs +32 -15
  28. data/sig/capybara/cuprite/errors.rbs +0 -6
  29. data/sig/capybara/cuprite/finder.rbs +1 -1
  30. data/sig/capybara/cuprite/node.rbs +2 -2
  31. metadata +17 -17
  32. data/lib/capybara/cuprite/browser/extensions.rb +0 -56
  33. data/lib/capybara/cuprite/browser.rb +0 -142
  34. data/lib/capybara/cuprite/context.rb +0 -26
  35. data/lib/capybara/cuprite/page/frames.rb +0 -45
  36. data/lib/capybara/cuprite/page/modals.rb +0 -134
  37. data/lib/capybara/cuprite/page.rb +0 -77
  38. data/sig/capybara/cuprite/browser/extensions.rbs +0 -27
  39. data/sig/capybara/cuprite/browser.rbs +0 -41
  40. data/sig/capybara/cuprite/context.rbs +0 -7
  41. data/sig/capybara/cuprite/page/frames.rbs +0 -37
  42. data/sig/capybara/cuprite/page/modals.rbs +0 -39
  43. data/sig/capybara/cuprite/page.rbs +0 -26
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5e0708e75aaeb6ff425ee91ce8b6f81471eda24a7cbac67957b3fa3b5c4c9545
4
- data.tar.gz: e669fc3917bfe55b6a3a9a095e33340369894251d52778e3355238e538f28bee
3
+ metadata.gz: b426f124f9183e2706dba770a180fda5aef212aca138bb4c1d492bba978c0366
4
+ data.tar.gz: 93bdc1a0fe4001fdd87dc321ae08080a6fcdae7c0ea132b2ef0ca2bd23ddcf9f
5
5
  SHA512:
6
- metadata.gz: 2307ea6681e6c44b65f08ff44ea24dfae3010d4261973266ecdb3dcbe5fa0e8c38708a5cca885493c3adbedf0195e3ade994a580d8b65833a264283db62eb921
7
- data.tar.gz: b77cfec35d9b582cec93f4f314d196c8a4b9a38a368e99e4a08fd6ef1aff8383b439674135c046d6315506abef8e2ff0f2540a42a381a6fdbec82f5195da570b
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.browser` is a `Ferrum::Browser`:
62
+ `page.driver.current_page` is the Ferrum page of the window Capybara is switched to:
63
63
 
64
64
  ```ruby
65
- browser = page.driver.browser
66
- browser.mouse.move(x: 123, y: 456).down.up
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
@@ -130,7 +130,7 @@ opened where you can further experiment with the test.
130
130
 
131
131
  ## Clicking/Scrolling
132
132
 
133
- * `page.driver.browser.mouse.click(x:, y:)` Click a very specific area of the screen.
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.browser.headers.get # => {}
144
- page.driver.browser.headers.set("X-Trace" => "1")
145
- page.driver.browser.headers.add({ "Referer" => "https://example.com" })
146
- page.driver.browser.headers.get # => { "X-Trace" => "1", "Referer" => "https://example.com" }
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.browser.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.
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.browser.network.traffic
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,28 +167,30 @@ first_exchange = network_traffic.first
167
167
  response = first_exchange.response
168
168
  ```
169
169
 
170
- * `page.driver.browser.network.wait_idle` Natively waits for network idle
170
+ * `page.driver.current_page.network.wait_idle` Natively waits for network idle
171
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.browser.network.wait_idle
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.browser.network.clear(:traffic)` or `page.driver.reset!`
182
+ `page.driver.current_page.network.clear_traffic` or `page.driver.reset!`
183
183
 
184
- * `page.driver.browser.await_navigation { ... }` unlike `network.wait_idle` runs
185
- the block and waits until the page it reloads or navigates to is loaded, or the
186
- timeout passes. It's useful when you know that for example clicking an
187
- autocomplete suggestion reloads the page. Sleep makes you wait longer or less
188
- than needed, and network idle can return before the page even started to reload.
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.
189
191
 
190
192
  ```ruby
191
- page.driver.browser.await_navigation { find(".suggestion").click }
193
+ page.driver.current_page.await_navigation(watch: 1) { find(".suggestion").click }
192
194
  ```
193
195
 
194
196
 
@@ -197,7 +199,7 @@ page.driver.browser.await_navigation { find(".suggestion").click }
197
199
  Use Ferrum's cookies to inspect and manipulate cookies:
198
200
 
199
201
  ```ruby
200
- cookies = page.driver.browser.cookies
202
+ cookies = page.driver.current_page.cookies
201
203
  cookies.set(name: "stealth", value: "omg")
202
204
  cookies["stealth"].value # => "omg"
203
205
  cookies.all # => { "stealth" => #<Ferrum::Cookies::Cookie ...> }
@@ -211,14 +213,14 @@ cookies.clear
211
213
  Besides capybara screenshot method you can get image or PDF as Base64 with Ferrum:
212
214
 
213
215
  ```ruby
214
- page.driver.browser.screenshot(format: :jpeg, full: true)
215
- page.driver.browser.pdf(paper_width: 6, paper_height: 12)
216
+ page.driver.current_page.screenshot(format: :jpeg, full: true)
217
+ page.driver.current_page.pdf(paper_width: 6, paper_height: 12)
216
218
  ```
217
219
 
218
220
 
219
221
  ## Authorization
220
222
 
221
- * `page.driver.browser.network.authorize(user:, password:) { |request, _index, _total| request.continue }`
223
+ * `page.driver.current_page.network.authorize(user:, password:) { |request, _index, _total| request.continue }`
222
224
 
223
225
  ## Proxy
224
226
 
@@ -241,14 +243,14 @@ Cuprite supports URL blocklisting, which allows you to prevent scripts from
241
243
  running on designated domains:
242
244
 
243
245
  ```ruby
244
- page.driver.browser.url_blocklist = %r{http://www.example.com}
246
+ page.driver.url_blocklist = %r{http://www.example.com}
245
247
  ```
246
248
 
247
249
  and also URL allowlisting, which allows scripts to only run on designated
248
250
  domains:
249
251
 
250
252
  ```ruby
251
- page.driver.browser.url_allowlist = %r{http://www.example.com}
253
+ page.driver.url_allowlist = %r{http://www.example.com}
252
254
  ```
253
255
 
254
256
  `url_blacklist=` and `url_whitelist=` are aliases of the same setters. Only one list can be set at a time.
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Capybara
4
+ module Cuprite
5
+ # The release that removes what's deprecated now.
6
+ REMOVE_DEPRECATED_IN = "1.0"
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]
14
+ def self.deprecation_warning(method, advice = nil)
15
+ message = "#{method} is deprecated and will be removed in #{REMOVE_DEPRECATED_IN}"
16
+ message += ", #{advice} instead" if advice
17
+ warn message, uplevel: 2
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "capybara/cuprite/deprecation"
4
+
5
+ module Capybara
6
+ module Cuprite
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
11
+ module Deprecations
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
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -3,7 +3,7 @@
3
3
  module Capybara
4
4
  module Cuprite
5
5
  class Driver < Capybara::Driver::Base
6
- # Driver methods Capybara doesn't need.
6
+ # Driver methods Capybara doesn't need, and settings changeable between tests through `page.driver`.
7
7
  module Extensions
8
8
  # Opens the current page in DevTools and pauses, taking the arguments of `Ferrum::Browser#debug`.
9
9
  #
@@ -11,6 +11,52 @@ module Capybara
11
11
  def debug(...)
12
12
  browser.debug(...)
13
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=
14
60
  end
15
61
  end
16
62
  end
@@ -2,8 +2,8 @@
2
2
 
3
3
  module Capybara
4
4
  module Cuprite
5
- class Browser < Ferrum::Browser
6
- # The driver options only Cuprite handles, which {Browser} hands to every {Page} it opens.
5
+ class Driver < Capybara::Driver::Base
6
+ # The driver options only Cuprite handles, which {Driver} sets every page up with.
7
7
  # @api private
8
8
  class Settings
9
9
  # @return [Boolean, nil] whether an unhandled dialog raises {UnhandledModalError} instead of warning
@@ -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
@@ -0,0 +1,113 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Capybara
4
+ module Cuprite
5
+ class Driver < Capybara::Driver::Base
6
+ class Window
7
+ # Answers JavaScript dialogs as `accept_modal` and `dismiss_modal` asked, and records their messages.
8
+ # @api private
9
+ module Modals
10
+ # Accepts the next dialog.
11
+ # @return [void]
12
+ def accept_confirm
13
+ @accept_modal << true
14
+ end
15
+
16
+ # Dismisses the next dialog.
17
+ # @return [void]
18
+ def dismiss_confirm
19
+ @accept_modal << false
20
+ end
21
+
22
+ # Accepts the next dialog with a response.
23
+ # @param modal_response [String, nil]
24
+ # @return [void]
25
+ def accept_prompt(modal_response)
26
+ @accept_modal << true
27
+ @modal_response = modal_response
28
+ end
29
+
30
+ # Dismisses the next dialog.
31
+ # @return [void]
32
+ def dismiss_prompt
33
+ @accept_modal << false
34
+ end
35
+
36
+ # Waits for a dialog to have been answered.
37
+ #
38
+ # @param options [Hash] `:text` to match and `:wait` in seconds
39
+ # @return [String] the dialog's message
40
+ # @raise [Capybara::ModalNotFound] when no matching dialog was answered in time
41
+ def find_modal(options)
42
+ expect_text = options[:text]
43
+ expect_regexp = expect_text.is_a?(Regexp) ? expect_text : Regexp.escape(expect_text.to_s)
44
+ not_found_msg = "Unable to find modal dialog"
45
+ not_found_msg += " with #{expect_text}" if expect_text
46
+ deadline = monotonic_time + options.fetch(:wait, page.timeout)
47
+
48
+ loop do
49
+ remaining = deadline - monotonic_time
50
+ raise Capybara::ModalNotFound, not_found_msg if remaining.negative?
51
+
52
+ modal_text = @modal_messages.pop(timeout: remaining)
53
+ return modal_text if modal_text && (!expect_text || modal_text.match?(expect_regexp))
54
+ end
55
+ end
56
+
57
+ # Dialogs are answered on Ferrum's background dispatcher thread, where raising wouldn't reach the caller.
58
+ # The error for a dialog nobody expected is stashed there and raised here, on the calling thread, before and
59
+ # after the block.
60
+ #
61
+ # @yield the calls that may open a dialog
62
+ # @return [Object] the block's result
63
+ # @raise [UnhandledModalError] with `raise_on_unhandled_modal` when an unexpected dialog was answered
64
+ def check_modals
65
+ raise_stashed_modal_error
66
+ yield
67
+ ensure
68
+ raise_stashed_modal_error
69
+ end
70
+
71
+ private
72
+
73
+ def monotonic_time
74
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
75
+ end
76
+
77
+ def raise_stashed_modal_error
78
+ error = @unhandled_modal_error
79
+ @unhandled_modal_error = nil
80
+ raise error if error
81
+ end
82
+
83
+ def handle_dialog(dialog)
84
+ accept_modal = @accept_modal.last
85
+
86
+ if [true, false].include?(accept_modal)
87
+ @accept_modal.pop
88
+ @modal_messages << dialog.message
89
+ accept = accept_modal
90
+ response = @modal_response
91
+ else
92
+ with_text = dialog.message ? "with text `#{dialog.message}` " : ""
93
+ message = "Modal window #{with_text}has been opened, but you didn't wrap " \
94
+ "your code into (`accept_prompt` | `dismiss_prompt` | " \
95
+ "`accept_confirm` | `dismiss_confirm` | `accept_alert`), " \
96
+ "accepting by default"
97
+
98
+ if @settings.raise_on_unhandled_modal
99
+ @unhandled_modal_error = UnhandledModalError.new(message)
100
+ else
101
+ warn message
102
+ end
103
+
104
+ accept = true
105
+ end
106
+
107
+ accept ? dialog.accept(response) : dialog.dismiss
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "capybara/cuprite/driver/window/frames"
4
+ require "capybara/cuprite/driver/window/modals"
5
+
6
+ module Capybara
7
+ module Cuprite
8
+ class Driver < Capybara::Driver::Base
9
+ # What Cuprite keeps for each of Capybara's windows next to Ferrum's page: the frame switched to and the
10
+ # dialogs to answer.
11
+ # @api private
12
+ class Window
13
+ include Frames
14
+ include Modals
15
+
16
+ # @return [Ferrum::Page]
17
+ attr_reader :page
18
+
19
+ # @param page [Ferrum::Page]
20
+ # @param settings [Settings] the options only Cuprite handles
21
+ def initialize(page, settings)
22
+ @page = page
23
+ @settings = settings
24
+ @frame_stack = []
25
+ @accept_modal = []
26
+ @modal_messages = Thread::Queue.new
27
+ @modal_response = nil
28
+ @unhandled_modal_error = nil
29
+ page.on(:dialog) { |dialog| handle_dialog(dialog) }
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Capybara
4
+ module Cuprite
5
+ class Driver < Capybara::Driver::Base
6
+ # Capybara's windows: Ferrum's page targets, with the one switched to as {Driver#current_page}.
7
+ # @api private
8
+ module Windows
9
+ # @return [Array<String>] the handles of all open windows and tabs
10
+ def window_handles
11
+ browser.targets.values.select(&:page?).map(&:id)
12
+ end
13
+
14
+ # @return [String] the handle of the current window
15
+ def current_window_handle
16
+ current_page.target_id
17
+ end
18
+
19
+ # Opens a blank page without switching to it.
20
+ #
21
+ # @param kind [:tab, :window] a tab in the current window, or a window of its own
22
+ # @return [Ferrum::Page]
23
+ # @raise [ArgumentError] for any other kind
24
+ def open_new_window(kind = :tab)
25
+ unless %i[tab window].include?(kind)
26
+ raise ArgumentError, "open_new_window takes :tab or :window, got #{kind.inspect}"
27
+ end
28
+
29
+ browser.create_page(new_window: kind == :window)
30
+ end
31
+
32
+ # Makes a window or tab the current one.
33
+ #
34
+ # @param handle [String]
35
+ # @return [void]
36
+ # @raise [Ferrum::NoSuchPageError] when there's no such window
37
+ def switch_to_window(handle)
38
+ target = window_target(handle)
39
+ forget_page
40
+ @current_page = target.page
41
+ @current_page.activate
42
+ end
43
+
44
+ # Closes a window or tab.
45
+ #
46
+ # @param handle [String]
47
+ # @return [void]
48
+ # @raise [Ferrum::NoSuchPageError] when there's no such window
49
+ def close_window(handle)
50
+ target = window_target(handle)
51
+ if @current_page&.target_id == target.id
52
+ forget_page
53
+ @page_closed = true
54
+ end
55
+ browser.close_target(target.id)
56
+ @windows.delete(target.id)
57
+ end
58
+
59
+ # @return [Class] the error raised for a closed window
60
+ def no_such_window_error
61
+ Ferrum::NoSuchPageError
62
+ end
63
+
64
+ private
65
+
66
+ def within_window(handle)
67
+ original = current_window_handle
68
+ raise Ferrum::NoSuchPageError unless window_handles.include?(handle)
69
+
70
+ switch_to_window(handle)
71
+ yield
72
+ ensure
73
+ switch_to_window(original)
74
+ end
75
+
76
+ def forget_page
77
+ @current_page = nil
78
+ @page_closed = false
79
+ end
80
+
81
+ def window_target(handle)
82
+ target = browser.targets[handle]
83
+ raise Ferrum::NoSuchPageError unless target&.page?
84
+
85
+ target
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end