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.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +35 -31
  3. data/lib/capybara/cuprite/deprecation.rb +8 -1
  4. data/lib/capybara/cuprite/devices.rb +8 -0
  5. data/lib/capybara/cuprite/driver/deprecations.rb +22 -184
  6. data/lib/capybara/cuprite/driver/extensions.rb +50 -0
  7. data/lib/capybara/cuprite/driver/settings.rb +102 -0
  8. data/lib/capybara/cuprite/driver/window/frames.rb +35 -0
  9. data/lib/capybara/cuprite/driver/window/modals.rb +113 -0
  10. data/lib/capybara/cuprite/driver/window.rb +34 -0
  11. data/lib/capybara/cuprite/driver/windows.rb +90 -0
  12. data/lib/capybara/cuprite/driver.rb +331 -66
  13. data/lib/capybara/cuprite/errors.rb +48 -1
  14. data/lib/capybara/cuprite/finder/hints.rb +51 -0
  15. data/lib/capybara/cuprite/finder.rb +18 -3
  16. data/lib/capybara/cuprite/javascripts/index.js +123 -58
  17. data/lib/capybara/cuprite/node/extensions.rb +14 -4
  18. data/lib/capybara/cuprite/node/typing.rb +75 -0
  19. data/lib/capybara/cuprite/node.rb +208 -63
  20. data/lib/capybara/cuprite/version.rb +3 -1
  21. data/lib/capybara/cuprite.rb +1 -4
  22. data/sig/capybara/cuprite/deprecation.rbs +7 -0
  23. data/sig/capybara/cuprite/devices.rbs +19 -0
  24. data/sig/capybara/cuprite/driver/deprecations.rbs +11 -0
  25. data/sig/capybara/cuprite/driver/extensions.rbs +29 -0
  26. data/sig/capybara/cuprite/driver/settings.rbs +42 -0
  27. data/sig/capybara/cuprite/driver/window/frames.rbs +13 -0
  28. data/sig/capybara/cuprite/driver/window/modals.rbs +29 -0
  29. data/sig/capybara/cuprite/driver/window.rbs +22 -0
  30. data/sig/capybara/cuprite/driver/windows.rbs +27 -0
  31. data/sig/capybara/cuprite/driver.rbs +140 -0
  32. data/sig/capybara/cuprite/errors.rbs +56 -0
  33. data/sig/capybara/cuprite/finder/hints.rbs +28 -0
  34. data/sig/capybara/cuprite/finder.rbs +7 -0
  35. data/sig/capybara/cuprite/node/extensions.rbs +9 -0
  36. data/sig/capybara/cuprite/node/typing.rbs +30 -0
  37. data/sig/capybara/cuprite/node.rbs +112 -0
  38. data/sig/capybara/cuprite/version.rbs +5 -0
  39. metadata +27 -11
  40. data/lib/capybara/cuprite/browser/deprecations.rb +0 -26
  41. data/lib/capybara/cuprite/browser/extensions.rb +0 -59
  42. data/lib/capybara/cuprite/browser.rb +0 -108
  43. data/lib/capybara/cuprite/node/deprecations.rb +0 -43
  44. data/lib/capybara/cuprite/options.rb +0 -33
  45. data/lib/capybara/cuprite/page/frames.rb +0 -47
  46. data/lib/capybara/cuprite/page/modals.rb +0 -118
  47. data/lib/capybara/cuprite/page.rb +0 -36
@@ -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