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
@@ -1,134 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Capybara
4
- module Cuprite
5
- class Page < Ferrum::Page
6
- # Answers JavaScript dialogs as `accept_modal` and `dismiss_modal` asked, and records their messages.
7
- # @api private
8
- module Modals
9
- def initialize(...)
10
- @accept_modal = []
11
- @modal_messages = Thread::Queue.new
12
- @modal_response = nil
13
- @unhandled_modal_error = nil
14
- super
15
- end
16
-
17
- # Dialogs are handled on Ferrum's background dispatcher thread, so
18
- # raising there wouldn't reach the caller. The dialog is answered
19
- # from that thread, skipping the pending error check, and the error
20
- # (if any) is stashed here to be raised from the calling thread the
21
- # next time it makes a command round trip.
22
- def command(...)
23
- return super if Thread.current[:cuprite_handling_dialog]
24
-
25
- raise_pending_unhandled_modal_error! { super }
26
- end
27
-
28
- # Accepts the next dialog.
29
- # @return [void]
30
- def accept_confirm
31
- @accept_modal << true
32
- end
33
-
34
- # Dismisses the next dialog.
35
- # @return [void]
36
- def dismiss_confirm
37
- @accept_modal << false
38
- end
39
-
40
- # Accepts the next dialog with a response.
41
- # @param modal_response [String, nil]
42
- # @return [void]
43
- def accept_prompt(modal_response)
44
- @accept_modal << true
45
- @modal_response = modal_response
46
- end
47
-
48
- # Dismisses the next dialog.
49
- # @return [void]
50
- def dismiss_prompt
51
- @accept_modal << false
52
- end
53
-
54
- # Waits for a dialog to have been answered.
55
- #
56
- # @param options [Hash] `:text` to match and `:wait` in seconds
57
- # @return [String] the dialog's message
58
- # @raise [Capybara::ModalNotFound] when no matching dialog was answered in time
59
- def find_modal(options)
60
- expect_text = options[:text]
61
- expect_regexp = expect_text.is_a?(Regexp) ? expect_text : Regexp.escape(expect_text.to_s)
62
- not_found_msg = "Unable to find modal dialog"
63
- not_found_msg += " with #{expect_text}" if expect_text
64
- deadline = monotonic_time + options.fetch(:wait, timeout)
65
-
66
- loop do
67
- remaining = deadline - monotonic_time
68
- raise Capybara::ModalNotFound, not_found_msg if remaining.negative?
69
-
70
- modal_text = @modal_messages.pop(timeout: remaining)
71
- return modal_text if modal_text && (!expect_text || modal_text.match?(expect_regexp))
72
- end
73
- end
74
-
75
- private
76
-
77
- def monotonic_time
78
- Process.clock_gettime(Process::CLOCK_MONOTONIC)
79
- end
80
-
81
- def raise_pending_unhandled_modal_error!
82
- error = @unhandled_modal_error
83
- @unhandled_modal_error = nil
84
- raise error if error
85
-
86
- yield
87
- ensure
88
- error = @unhandled_modal_error
89
- @unhandled_modal_error = nil
90
- raise error if error
91
- end
92
-
93
- def prepare_page
94
- super
95
- on(:dialog) { |dialog| handle_dialog(dialog) }
96
- end
97
-
98
- def handle_dialog(dialog)
99
- accept_modal = @accept_modal.last
100
-
101
- if [true, false].include?(accept_modal)
102
- @accept_modal.pop
103
- @modal_messages << dialog.message
104
- accept = accept_modal
105
- response = @modal_response
106
- else
107
- with_text = dialog.message ? "with text `#{dialog.message}` " : ""
108
- message = "Modal window #{with_text}has been opened, but you didn't wrap " \
109
- "your code into (`accept_prompt` | `dismiss_prompt` | " \
110
- "`accept_confirm` | `dismiss_confirm` | `accept_alert`), " \
111
- "accepting by default"
112
-
113
- if @settings.raise_on_unhandled_modal
114
- @unhandled_modal_error = UnhandledModalError.new(message)
115
- else
116
- warn message
117
- end
118
-
119
- accept = true
120
- end
121
-
122
- answer_dialog(dialog, accept, response)
123
- end
124
-
125
- def answer_dialog(dialog, accept, response)
126
- Thread.current[:cuprite_handling_dialog] = true
127
- accept ? dialog.accept(response) : dialog.dismiss
128
- ensure
129
- Thread.current[:cuprite_handling_dialog] = nil
130
- end
131
- end
132
- end
133
- end
134
- end
@@ -1,77 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "capybara/cuprite/page/frames"
4
- require "capybara/cuprite/page/modals"
5
-
6
- module Capybara
7
- module Cuprite
8
- # A Ferrum page with Cuprite's JavaScript, frame switching and dialog handling.
9
- # @api private
10
- class Page < Ferrum::Page
11
- # The JavaScript Cuprite adds to every page.
12
- EXTENSION_JS = File.read(Driver::EXTENSION).freeze
13
-
14
- include Frames
15
- include Modals
16
-
17
- # @param client [Ferrum::Client, Ferrum::SessionClient]
18
- # @param settings [Browser::Settings] the options only Cuprite handles
19
- # @param options [Hash] the keywords of `Ferrum::Page#initialize`
20
- def initialize(client, settings:, **options)
21
- @settings = settings
22
- super(client, **options)
23
- end
24
-
25
- # Navigates the page to a URL, see `Ferrum::Page#go_to`.
26
- #
27
- # @param url [String, nil]
28
- # @param options [Hash] the keywords of `Ferrum::Page#go_to`, like `referrer:` and `timeout:`
29
- # @return [String, nil] the navigated frame's id
30
- # @raise [PendingConnectionsError] with `pending_connection_errors` when the page doesn't load in time
31
- def go_to(url = nil, **options)
32
- frame_id = super
33
- return frame_id if frame_id || !@settings.pending_connection_errors?
34
-
35
- pendings = network.traffic.select(&:pending?).map(&:url).compact
36
- raise PendingConnectionsError.new(combine_url!(url), pendings)
37
- end
38
-
39
- # Sets the viewport, keeping the `scale_factor:` and `mobile:` options unless given.
40
- #
41
- # @return [void]
42
- def set_viewport(width: 0, height: 0, scale_factor: @settings.scale_factor, mobile: @settings.mobile)
43
- super
44
- end
45
-
46
- # Ferrum's network falls back to it when it clears other overrides, so the device's user agent stays.
47
- #
48
- # @return [String, nil] the emulated device's user agent, or Chrome's own
49
- def default_user_agent
50
- @settings.user_agent(super)
51
- end
52
-
53
- private
54
-
55
- def subscribe
56
- super
57
- return unless @settings.js_errors?
58
-
59
- on("Runtime.exceptionThrown") do |params|
60
- Thread.main.raise Ferrum::JavaScriptError, params["exceptionDetails"]
61
- end
62
- end
63
-
64
- def prepare_page
65
- super
66
- execute(EXTENSION_JS)
67
-
68
- width, height = @settings.window_size
69
- width && height ? resize(width: width, height: height) : set_viewport
70
- network.emulate(user_agent: default_user_agent) if @settings.device_user_agent?
71
-
72
- network.blacklist = @settings.url_blacklist if @settings.url_blacklist.any?
73
- network.whitelist = @settings.url_whitelist if @settings.url_whitelist.any?
74
- end
75
- end
76
- end
77
- end
@@ -1,27 +0,0 @@
1
- module Capybara
2
- module Cuprite
3
- class Browser < Ferrum::Browser
4
- module Extensions : Ferrum::Browser
5
- def raise_on_unhandled_modal: () -> bool?
6
-
7
- def raise_on_unhandled_modal=: (bool? value) -> bool?
8
-
9
- def url_whitelist: () -> Array[Regexp]
10
-
11
- alias url_allowlist url_whitelist
12
-
13
- def url_whitelist=: (Regexp | Array[Regexp] | nil patterns) -> void
14
-
15
- alias url_allowlist= url_whitelist=
16
-
17
- def url_blacklist: () -> Array[Regexp]
18
-
19
- alias url_blocklist url_blacklist
20
-
21
- def url_blacklist=: (Regexp | Array[Regexp] | nil patterns) -> void
22
-
23
- alias url_blocklist= url_blacklist=
24
- end
25
- end
26
- end
27
- end
@@ -1,41 +0,0 @@
1
- module Capybara
2
- module Cuprite
3
- class Browser < Ferrum::Browser
4
- include Extensions
5
-
6
- @settings: Settings
7
- @page: Page?
8
- @page_closed: bool
9
-
10
- def initialize: (?Hash[Symbol, untyped]? options) -> void
11
-
12
- def command: (String method, ?timeout: (Numeric | Ferrum::Deadline)?, **untyped params) -> Hash[String, untyped]
13
-
14
- def page: () -> Page
15
-
16
- def reset: () -> void
17
-
18
- def quit: () -> void
19
-
20
- def resize: (?width: Integer?, ?height: Integer?, ?fullscreen: bool) -> nil
21
-
22
- def current_window_handle: () -> String
23
-
24
- def window_handles: () -> Array[String]
25
-
26
- def open_new_window: (?(:tab | :window) kind) -> Page
27
-
28
- def switch_to_window: (String target_id) -> nil
29
-
30
- def close_window: (String target_id) -> Ferrum::Target?
31
-
32
- private
33
-
34
- def forget_page: () -> void
35
-
36
- def window_target: (String target_id) -> Ferrum::Target
37
-
38
- def attach_page: (?String? target_id) -> Page
39
- end
40
- end
41
- end
@@ -1,7 +0,0 @@
1
- module Ferrum
2
- class Context
3
- module NewWindow
4
- def create_target: (?new_window: bool) -> Target
5
- end
6
- end
7
- end
@@ -1,37 +0,0 @@
1
- module Capybara
2
- module Cuprite
3
- class Page < Ferrum::Page
4
- module Frames : Ferrum::Page
5
- extend Forwardable
6
-
7
- @frame_stack: Array[String]
8
-
9
- def initialize: (Ferrum::Client | Ferrum::SessionClient client, settings: Browser::Settings, context_id: String?, target_id: String, ?proxy: Hash[Symbol, String | Integer]?) -> void
10
-
11
- def at_css: (String selector, ?within: Ferrum::Node?) -> Ferrum::Node?
12
-
13
- def at_xpath: (String selector, ?within: Ferrum::Node?) -> Ferrum::Node?
14
-
15
- def css: (String selector, ?within: Ferrum::Node?) -> Array[Ferrum::Node]
16
-
17
- def xpath: (String selector, ?within: Ferrum::Node?) -> Array[Ferrum::Node]
18
-
19
- def html: () -> String
20
-
21
- def execution_id: () -> Integer?
22
-
23
- def execution_id!: (?Numeric? timeout) -> Integer
24
-
25
- def evaluate: (String expression, *untyped positional, **untyped keywords) -> untyped
26
-
27
- def execute: (String expression, *untyped positional, **untyped keywords) -> nil
28
-
29
- def switch_to_frame: (String | :parent | :top handle) -> void
30
-
31
- private
32
-
33
- def active_frame: () -> Ferrum::Frame
34
- end
35
- end
36
- end
37
- end
@@ -1,39 +0,0 @@
1
- module Capybara
2
- module Cuprite
3
- class Page < Ferrum::Page
4
- module Modals : Ferrum::Page
5
- @accept_modal: Array[bool]
6
- @modal_messages: Thread::Queue[String?]
7
- @modal_response: String?
8
- @unhandled_modal_error: UnhandledModalError?
9
- @settings: Browser::Settings
10
-
11
- def initialize: (Ferrum::Client | Ferrum::SessionClient client, settings: Browser::Settings, context_id: String?, target_id: String, ?proxy: Hash[Symbol, String | Integer]?) -> void
12
-
13
- def command: (String method, ?timeout: (Numeric | Ferrum::Deadline)?, **untyped params) -> Hash[String, untyped]
14
-
15
- def accept_confirm: () -> Array[bool]
16
-
17
- def dismiss_confirm: () -> Array[bool]
18
-
19
- def accept_prompt: (String? modal_response) -> String?
20
-
21
- def dismiss_prompt: () -> Array[bool]
22
-
23
- def find_modal: (Driver::modal_options options) -> String
24
-
25
- private
26
-
27
- def monotonic_time: () -> Float
28
-
29
- def raise_pending_unhandled_modal_error!: [T] () { () -> T } -> T
30
-
31
- def prepare_page: () -> void
32
-
33
- def handle_dialog: (Ferrum::Dialog dialog) -> nil
34
-
35
- def answer_dialog: (Ferrum::Dialog dialog, bool accept, String? response) -> nil
36
- end
37
- end
38
- end
39
- end
@@ -1,26 +0,0 @@
1
- module Capybara
2
- module Cuprite
3
- class Page < Ferrum::Page
4
- include Frames
5
- include Modals
6
-
7
- EXTENSION_JS: String
8
-
9
- @settings: Browser::Settings
10
-
11
- def initialize: (Ferrum::Client | Ferrum::SessionClient client, settings: Browser::Settings, context_id: String?, target_id: String, ?proxy: Hash[Symbol, String | Integer]?) -> void
12
-
13
- def go_to: (?String? url, ?timeout: (Numeric | Ferrum::Deadline)?, ?referrer: String?) -> String?
14
-
15
- def set_viewport: (?width: Integer, ?height: Integer, ?scale_factor: Numeric, ?mobile: bool?) -> nil
16
-
17
- def default_user_agent: () -> String?
18
-
19
- private
20
-
21
- def subscribe: () -> void
22
-
23
- def prepare_page: () -> void
24
- end
25
- end
26
- end