selenium-webdriver 4.53.0 → 4.54.0

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 (33) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES +12 -0
  3. data/bin/linux-arm64/selenium-manager +0 -0
  4. data/bin/linux-x86_64/selenium-manager +0 -0
  5. data/bin/macos/selenium-manager +0 -0
  6. data/bin/selenium-manager-THIRD-PARTY-NOTICES.txt +26 -25
  7. data/bin/selenium-manager.cdx.json +224 -224
  8. data/bin/windows/selenium-manager.exe +0 -0
  9. data/lib/selenium/server.rb +20 -6
  10. data/lib/selenium/webdriver/bidi/dispatcher/listener.rb +62 -0
  11. data/lib/selenium/webdriver/bidi/dispatcher/ordered_listener.rb +54 -0
  12. data/lib/selenium/webdriver/bidi/dispatcher.rb +80 -0
  13. data/lib/selenium/webdriver/bidi/protocol/bluetooth.rb +24 -0
  14. data/lib/selenium/webdriver/bidi/protocol/browsing_context.rb +98 -0
  15. data/lib/selenium/webdriver/bidi/protocol/domain.rb +10 -0
  16. data/lib/selenium/webdriver/bidi/protocol/input.rb +7 -0
  17. data/lib/selenium/webdriver/bidi/protocol/log.rb +7 -0
  18. data/lib/selenium/webdriver/bidi/protocol/moz_debugging.rb +12 -0
  19. data/lib/selenium/webdriver/bidi/protocol/network.rb +34 -0
  20. data/lib/selenium/webdriver/bidi/protocol/script.rb +21 -0
  21. data/lib/selenium/webdriver/bidi/protocol/speculation.rb +6 -0
  22. data/lib/selenium/webdriver/bidi/protocol.rb +1 -0
  23. data/lib/selenium/webdriver/bidi/support/bidi_generate.rb +22 -5
  24. data/lib/selenium/webdriver/bidi/transport.rb +14 -1
  25. data/lib/selenium/webdriver/common/child_process.rb +1 -0
  26. data/lib/selenium/webdriver/common/local_driver.rb +1 -1
  27. data/lib/selenium/webdriver/common/service_manager.rb +13 -2
  28. data/lib/selenium/webdriver/common/websocket_connection.rb +13 -0
  29. data/lib/selenium/webdriver/remote/bidi_bridge.rb +1 -0
  30. data/lib/selenium/webdriver/remote/bridge.rb +1 -0
  31. data/lib/selenium/webdriver/safari/service.rb +5 -0
  32. data/lib/selenium/webdriver/version.rb +1 -1
  33. metadata +5 -2
Binary file
@@ -130,6 +130,8 @@ module Selenium
130
130
  # @option opts [true,false] :background Run the server in the background (default: false)
131
131
  # @option opts [true,false,String] :log Either a path to a log file,
132
132
  # or true to pass server log to stdout.
133
+ # @option opts [Hash<String, String, nil>] :env Environment variables for the server process,
134
+ # merged into the inherited environment.
133
135
  # @raise [Errno::ENOENT] if the jar file does not exist
134
136
  #
135
137
 
@@ -145,6 +147,7 @@ module Selenium
145
147
  @background = opts.fetch(:background, false)
146
148
  @additional_args = opts.fetch(:args, [])
147
149
  @log = opts[:log]
150
+ @env = opts[:env]
148
151
  if opts[:log_level]
149
152
  @log ||= true
150
153
  @additional_args << '--log-level'
@@ -212,18 +215,29 @@ module Selenium
212
215
  server_command = [@java] + properties + args + @additional_args
213
216
  cp = WebDriver::ChildProcess.build(*server_command)
214
217
 
215
- if @log.is_a?(String)
216
- cp.io = @log
217
- elsif @log
218
- cp.io = :out
219
- end
220
-
218
+ cp.env = @env if @env
219
+ cp.io = log_io
221
220
  cp.detach = @background
222
221
 
223
222
  cp
224
223
  end
225
224
  end
226
225
 
226
+ def log_io
227
+ io = @log.is_a?(String) ? @log : (:out if @log)
228
+ if ENV.key?('SE_DEBUG')
229
+ if @log.is_a?(String)
230
+ WebDriver.logger.warn('SE_DEBUG is set; overriding user-specified server log output to use stderr',
231
+ id: :se_debug)
232
+ end
233
+ WebDriver.logger.io
234
+ elsif WebDriver.logger.debug?
235
+ io || WebDriver.logger.io
236
+ else
237
+ io
238
+ end
239
+ end
240
+
227
241
  def poll_for_ready
228
242
  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
229
243
  loop do
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Licensed to the Software Freedom Conservancy (SFC) under one
4
+ # or more contributor license agreements. See the NOTICE file
5
+ # distributed with this work for additional information
6
+ # regarding copyright ownership. The SFC licenses this file
7
+ # to you under the Apache License, Version 2.0 (the
8
+ # "License"); you may not use this file except in compliance
9
+ # with the License. You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing,
14
+ # software distributed under the License is distributed on an
15
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
+ # KIND, either express or implied. See the License for the
17
+ # specific language governing permissions and limitations
18
+ # under the License.
19
+
20
+ module Selenium
21
+ module WebDriver
22
+ class BiDi
23
+ class Dispatcher
24
+ # Invoked with each payload on a thread of its own, without waiting for earlier invocations.
25
+ #
26
+ # @api private
27
+ class Listener
28
+ attr_reader :event, :payload_type
29
+
30
+ def initialize(event, payload_type, block)
31
+ @event = event
32
+ @payload_type = payload_type
33
+ @block = block
34
+ @active = true
35
+ @mutex = Mutex.new
36
+ end
37
+
38
+ def dispatch(payload)
39
+ Thread.new { invoke(payload) }
40
+ end
41
+
42
+ # Not joined: the caller may be this listener itself, removing itself or quitting the driver.
43
+ def deactivate
44
+ @mutex.synchronize { @active = false }
45
+ end
46
+
47
+ private
48
+
49
+ def invoke(payload)
50
+ return unless @mutex.synchronize { @active }
51
+
52
+ @block.call(payload)
53
+ # Nothing above a listener's thread can handle what escapes it, and test assertions are not StandardErrors.
54
+ rescue Exception => e # rubocop:disable Lint/RescueException
55
+ backtrace = Array(e.backtrace).first(5).join("\n")
56
+ WebDriver.logger.error("A listener for #{event} raised #{e.class}: #{e.message}\n#{backtrace}", id: :bidi)
57
+ end
58
+ end # Listener
59
+ end # Dispatcher
60
+ end # BiDi
61
+ end # WebDriver
62
+ end # Selenium
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Licensed to the Software Freedom Conservancy (SFC) under one
4
+ # or more contributor license agreements. See the NOTICE file
5
+ # distributed with this work for additional information
6
+ # regarding copyright ownership. The SFC licenses this file
7
+ # to you under the Apache License, Version 2.0 (the
8
+ # "License"); you may not use this file except in compliance
9
+ # with the License. You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing,
14
+ # software distributed under the License is distributed on an
15
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
+ # KIND, either express or implied. See the License for the
17
+ # specific language governing permissions and limitations
18
+ # under the License.
19
+
20
+ module Selenium
21
+ module WebDriver
22
+ class BiDi
23
+ class Dispatcher
24
+ # Invoked with one payload at a time, in arrival order, on its own worker thread.
25
+ # Used only when SE_ORDERED_EVENTS is true; kept for evaluating ordered delivery.
26
+ #
27
+ # @api private
28
+ class OrderedListener < Listener
29
+ STOP = Object.new.freeze
30
+
31
+ def initialize(...)
32
+ super
33
+ @queue = Queue.new
34
+ @worker = Thread.new do
35
+ until (payload = @queue.pop).equal?(STOP)
36
+ invoke(payload)
37
+ end
38
+ end
39
+ end
40
+
41
+ def dispatch(payload)
42
+ @queue << payload
43
+ end
44
+
45
+ # A payload queued behind the marker arrived after removal and is never invoked.
46
+ def deactivate
47
+ super
48
+ @queue << STOP
49
+ end
50
+ end # OrderedListener
51
+ end # Dispatcher
52
+ end # BiDi
53
+ end # WebDriver
54
+ end # Selenium
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Licensed to the Software Freedom Conservancy (SFC) under one
4
+ # or more contributor license agreements. See the NOTICE file
5
+ # distributed with this work for additional information
6
+ # regarding copyright ownership. The SFC licenses this file
7
+ # to you under the Apache License, Version 2.0 (the
8
+ # "License"); you may not use this file except in compliance
9
+ # with the License. You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing,
14
+ # software distributed under the License is distributed on an
15
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
+ # KIND, either express or implied. See the License for the
17
+ # specific language governing permissions and limitations
18
+ # under the License.
19
+
20
+ require 'selenium/webdriver/bidi/dispatcher/listener'
21
+ require 'selenium/webdriver/bidi/dispatcher/ordered_listener'
22
+
23
+ module Selenium
24
+ module WebDriver
25
+ class BiDi
26
+ # Holds the event listeners of one BiDi connection and invokes each with the typed
27
+ # payloads of its event. Listening is local: nothing here sends a command, so the
28
+ # caller subscribes with Protocol::Session#subscribe itself.
29
+ #
30
+ # @api private
31
+ class Dispatcher
32
+ def initialize
33
+ @listeners = {}
34
+ @mutex = Mutex.new
35
+ @last_id = 0
36
+ @closed = false
37
+ end
38
+
39
+ def add_listener(event, payload_type, &block)
40
+ raise ArgumentError, 'a block is required to listen for an event' unless block
41
+
42
+ @mutex.synchronize do
43
+ raise Error::WebDriverError, 'The BiDi connection is closed' if @closed
44
+
45
+ id = "#{event}##{@last_id += 1}"
46
+ listener_class = ENV.fetch('SE_ORDERED_EVENTS', nil) == 'true' ? OrderedListener : Listener
47
+ @listeners[id] = listener_class.new(event, payload_type, block)
48
+ id
49
+ end
50
+ end
51
+
52
+ def remove_listener(id)
53
+ @mutex.synchronize { @listeners.delete(id) }&.deactivate
54
+ end
55
+
56
+ # Runs on the socket thread, which also delivers command replies, so it never waits.
57
+ def receive(event, params)
58
+ listeners = @mutex.synchronize { @listeners.values.select { |listener| listener.event == event } }
59
+ return if listeners.empty?
60
+
61
+ payload_type = listeners.first.payload_type
62
+ begin
63
+ payload = payload_type ? payload_type.from_json(params) : params
64
+ rescue Error::SerializationError => e
65
+ return WebDriver.logger.error("Dropped a #{event} payload that failed validation: #{e.message}", id: :bidi)
66
+ end
67
+ listeners.each { |listener| listener.dispatch(payload) }
68
+ end
69
+
70
+ def close
71
+ listeners = @mutex.synchronize do
72
+ @closed = true
73
+ @listeners.values.tap { @listeners.clear }
74
+ end
75
+ listeners.each(&:deactivate)
76
+ end
77
+ end # Dispatcher
78
+ end # BiDi
79
+ end # WebDriver
80
+ end # Selenium
@@ -492,6 +492,30 @@ module Selenium
492
492
  )
493
493
  execute(cmd: 'bluetooth.simulateDescriptorResponse', params: params)
494
494
  end
495
+
496
+ # @api private
497
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
498
+ def add_request_device_prompt_updated_listener(&block)
499
+ add_listener('bluetooth.requestDevicePromptUpdated', &block)
500
+ end
501
+
502
+ # @api private
503
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
504
+ def add_gatt_connection_attempted_listener(&block)
505
+ add_listener('bluetooth.gattConnectionAttempted', &block)
506
+ end
507
+
508
+ # @api private
509
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
510
+ def add_characteristic_event_generated_listener(&block)
511
+ add_listener('bluetooth.characteristicEventGenerated', &block)
512
+ end
513
+
514
+ # @api private
515
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
516
+ def add_descriptor_event_generated_listener(&block)
517
+ add_listener('bluetooth.descriptorEventGenerated', &block)
518
+ end
495
519
  end # Bluetooth
496
520
  end # Protocol
497
521
  end # BiDi
@@ -904,6 +904,104 @@ module Selenium
904
904
  execute(cmd: 'browsingContext.traverseHistory', params: params)
905
905
  end
906
906
 
907
+ # @api private
908
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
909
+ # @see https://w3c.github.io/webdriver-bidi/#event-browsingContext-contextCreated
910
+ def add_context_created_listener(&block)
911
+ add_listener('browsingContext.contextCreated', &block)
912
+ end
913
+
914
+ # @api private
915
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
916
+ # @see https://w3c.github.io/webdriver-bidi/#event-browsingContext-contextDestroyed
917
+ def add_context_destroyed_listener(&block)
918
+ add_listener('browsingContext.contextDestroyed', &block)
919
+ end
920
+
921
+ # @api private
922
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
923
+ # @see https://w3c.github.io/webdriver-bidi/#event-browsingContext-domContentLoaded
924
+ def add_dom_content_loaded_listener(&block)
925
+ add_listener('browsingContext.domContentLoaded', &block)
926
+ end
927
+
928
+ # @api private
929
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
930
+ # @see https://w3c.github.io/webdriver-bidi/#event-browsingContext-downloadEnd
931
+ def add_download_end_listener(&block)
932
+ add_listener('browsingContext.downloadEnd', &block)
933
+ end
934
+
935
+ # @api private
936
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
937
+ # @see https://w3c.github.io/webdriver-bidi/#event-browsingContext-downloadWillBegin
938
+ def add_download_will_begin_listener(&block)
939
+ add_listener('browsingContext.downloadWillBegin', &block)
940
+ end
941
+
942
+ # @api private
943
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
944
+ # @see https://w3c.github.io/webdriver-bidi/#event-browsingContext-fragmentNavigated
945
+ def add_fragment_navigated_listener(&block)
946
+ add_listener('browsingContext.fragmentNavigated', &block)
947
+ end
948
+
949
+ # @api private
950
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
951
+ # @see https://w3c.github.io/webdriver-bidi/#event-browsingContext-historyUpdated
952
+ def add_history_updated_listener(&block)
953
+ add_listener('browsingContext.historyUpdated', &block)
954
+ end
955
+
956
+ # @api private
957
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
958
+ # @see https://w3c.github.io/webdriver-bidi/#event-browsingContext-load
959
+ def add_load_listener(&block)
960
+ add_listener('browsingContext.load', &block)
961
+ end
962
+
963
+ # @api private
964
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
965
+ # @see https://w3c.github.io/webdriver-bidi/#event-browsingContext-navigationAborted
966
+ def add_navigation_aborted_listener(&block)
967
+ add_listener('browsingContext.navigationAborted', &block)
968
+ end
969
+
970
+ # @api private
971
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
972
+ # @see https://w3c.github.io/webdriver-bidi/#event-browsingContext-navigationCommitted
973
+ def add_navigation_committed_listener(&block)
974
+ add_listener('browsingContext.navigationCommitted', &block)
975
+ end
976
+
977
+ # @api private
978
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
979
+ # @see https://w3c.github.io/webdriver-bidi/#event-browsingContext-navigationFailed
980
+ def add_navigation_failed_listener(&block)
981
+ add_listener('browsingContext.navigationFailed', &block)
982
+ end
983
+
984
+ # @api private
985
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
986
+ # @see https://w3c.github.io/webdriver-bidi/#event-browsingContext-navigationStarted
987
+ def add_navigation_started_listener(&block)
988
+ add_listener('browsingContext.navigationStarted', &block)
989
+ end
990
+
991
+ # @api private
992
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
993
+ # @see https://w3c.github.io/webdriver-bidi/#event-browsingContext-userPromptClosed
994
+ def add_user_prompt_closed_listener(&block)
995
+ add_listener('browsingContext.userPromptClosed', &block)
996
+ end
997
+
998
+ # @api private
999
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
1000
+ # @see https://w3c.github.io/webdriver-bidi/#event-browsingContext-userPromptOpened
1001
+ def add_user_prompt_opened_listener(&block)
1002
+ add_listener('browsingContext.userPromptOpened', &block)
1003
+ end
1004
+
907
1005
  # @api private
908
1006
  # moz: vendor variant of BrowsingContext, with browser-specific commands and params.
909
1007
  # Construct Moz.new(source) for a matching session; other sessions use BrowsingContext.
@@ -30,6 +30,12 @@ module Selenium
30
30
  @transport = Transport.new(connection)
31
31
  end
32
32
 
33
+ # Stops invoking the listener an `add_<event>_listener` method returned the id for, without
34
+ # waiting for a running invocation to finish. Local only, and a no-op for an unknown id.
35
+ def remove_listener(id)
36
+ @transport.remove_listener(id)
37
+ end
38
+
33
39
  private
34
40
 
35
41
  # The connection this domain runs over, so a generated vendor accessor can build its
@@ -39,6 +45,10 @@ module Selenium
39
45
  def execute(cmd:, params: nil, result: nil)
40
46
  @transport.execute(cmd: cmd, params: params, result: result)
41
47
  end
48
+
49
+ def add_listener(event, &block)
50
+ @transport.add_listener(event, self.class.const_get(:EVENT_TYPES).fetch(event), &block)
51
+ end
42
52
  end
43
53
  end
44
54
  end
@@ -411,6 +411,13 @@ module Selenium
411
411
  params = SetFilesParameters.new(context: context, element: element, files: files)
412
412
  execute(cmd: 'input.setFiles', params: params)
413
413
  end
414
+
415
+ # @api private
416
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
417
+ # @see https://w3c.github.io/webdriver-bidi/#event-input-fileDialogOpened
418
+ def add_file_dialog_opened_listener(&block)
419
+ add_listener('input.fileDialogOpened', &block)
420
+ end
414
421
  end # Input
415
422
  end # Protocol
416
423
  end # BiDi
@@ -108,6 +108,13 @@ module Selenium
108
108
  EVENT_TYPES = {
109
109
  'log.entryAdded' => Log::Entry
110
110
  }.freeze
111
+
112
+ # @api private
113
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
114
+ # @see https://w3c.github.io/webdriver-bidi/#event-log-entryAdded
115
+ def add_entry_added_listener(&block)
116
+ add_listener('log.entryAdded', &block)
117
+ end
111
118
  end # Log
112
119
  end # Protocol
113
120
  end # BiDi
@@ -329,6 +329,18 @@ module Selenium
329
329
  params = StepOverParameters.new(context: context)
330
330
  execute(cmd: 'moz:debugging.stepOver', params: params)
331
331
  end
332
+
333
+ # @api private
334
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
335
+ def add_paused_listener(&block)
336
+ add_listener('moz:debugging.paused', &block)
337
+ end
338
+
339
+ # @api private
340
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
341
+ def add_resumed_listener(&block)
342
+ add_listener('moz:debugging.resumed', &block)
343
+ end
332
344
  end # MozDebugging
333
345
  end # Protocol
334
346
  end # BiDi
@@ -756,6 +756,40 @@ module Selenium
756
756
  params = SetExtraHeadersParameters.new(headers: headers, contexts: contexts, user_contexts: user_contexts)
757
757
  execute(cmd: 'network.setExtraHeaders', params: params)
758
758
  end
759
+
760
+ # @api private
761
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
762
+ # @see https://w3c.github.io/webdriver-bidi/#event-network-authRequired
763
+ def add_auth_required_listener(&block)
764
+ add_listener('network.authRequired', &block)
765
+ end
766
+
767
+ # @api private
768
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
769
+ def add_before_request_sent_listener(&block)
770
+ add_listener('network.beforeRequestSent', &block)
771
+ end
772
+
773
+ # @api private
774
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
775
+ # @see https://w3c.github.io/webdriver-bidi/#event-network-fetchError
776
+ def add_fetch_error_listener(&block)
777
+ add_listener('network.fetchError', &block)
778
+ end
779
+
780
+ # @api private
781
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
782
+ # @see https://w3c.github.io/webdriver-bidi/#event-network-responseCompleted
783
+ def add_response_completed_listener(&block)
784
+ add_listener('network.responseCompleted', &block)
785
+ end
786
+
787
+ # @api private
788
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
789
+ # @see https://w3c.github.io/webdriver-bidi/#event-network-responseStarted
790
+ def add_response_started_listener(&block)
791
+ add_listener('network.responseStarted', &block)
792
+ end
759
793
  end # Network
760
794
  end # Protocol
761
795
  end # BiDi
@@ -1059,6 +1059,27 @@ module Selenium
1059
1059
  params = RemovePreloadScriptParameters.new(script: script)
1060
1060
  execute(cmd: 'script.removePreloadScript', params: params)
1061
1061
  end
1062
+
1063
+ # @api private
1064
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
1065
+ # @see https://w3c.github.io/webdriver-bidi/#event-script-message
1066
+ def add_message_listener(&block)
1067
+ add_listener('script.message', &block)
1068
+ end
1069
+
1070
+ # @api private
1071
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
1072
+ # @see https://w3c.github.io/webdriver-bidi/#event-script-realmCreated
1073
+ def add_realm_created_listener(&block)
1074
+ add_listener('script.realmCreated', &block)
1075
+ end
1076
+
1077
+ # @api private
1078
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
1079
+ # @see https://w3c.github.io/webdriver-bidi/#event-script-realmDestroyed
1080
+ def add_realm_destroyed_listener(&block)
1081
+ add_listener('script.realmDestroyed', &block)
1082
+ end
1062
1083
  end # Script
1063
1084
  end # Protocol
1064
1085
  end # BiDi
@@ -51,6 +51,12 @@ module Selenium
51
51
  EVENT_TYPES = {
52
52
  'speculation.prefetchStatusUpdated' => Speculation::PrefetchStatusUpdatedParameters
53
53
  }.freeze
54
+
55
+ # @api private
56
+ # @see https://www.selenium.dev/documentation/warnings/bidi-implementation/
57
+ def add_prefetch_status_updated_listener(&block)
58
+ add_listener('speculation.prefetchStatusUpdated', &block)
59
+ end
54
60
  end # Speculation
55
61
  end # Protocol
56
62
  end # BiDi
@@ -22,6 +22,7 @@
22
22
  # below when a new BiDi domain is generated.
23
23
  require 'selenium/webdriver/common/error'
24
24
  require 'selenium/webdriver/bidi/serialization'
25
+ require 'selenium/webdriver/bidi/dispatcher'
25
26
  require 'selenium/webdriver/bidi/transport'
26
27
  require 'selenium/webdriver/bidi/protocol/domain'
27
28
  require 'selenium/webdriver/bidi/protocol/error_code'
@@ -296,9 +296,24 @@ module BiDiGenerate
296
296
 
297
297
  # payload_ref is the Protocol-relative class the event's params parse into (nil when
298
298
  # non-structured, dispatched raw) — the inbound counterpart to a command's result_ref.
299
- Event = Struct.new(:wire_name, :event_name, :payload_ref, keyword_init: true) do
299
+ Event = Struct.new(:wire_name, :event_name, :payload_ref, :spec_href, keyword_init: true) do
300
300
  # An EVENT_TYPES entry mapping the wire method to the type its params parse into.
301
301
  def type_entry = "'#{wire_name}' => #{payload_ref || 'nil'}"
302
+
303
+ def listener_method = "add_#{event_name}_listener"
304
+
305
+ def render_lines(indent)
306
+ pad = ' ' * indent
307
+ lines = ["#{pad}# @api private", "#{pad}# @see #{BiDiGenerate::BIDI_DOC_URL}"]
308
+ lines << "#{pad}# @see #{spec_href}" if spec_href
309
+ lines + ["#{pad}def #{listener_method}(&block)",
310
+ "#{pad} add_listener('#{wire_name}', &block)",
311
+ "#{pad}end"]
312
+ end
313
+
314
+ def rbs_signature
315
+ "() { (#{payload_ref || 'Hash[String, untyped]'}) -> void } -> String"
316
+ end
302
317
  end
303
318
 
304
319
  # The RBS type a schema value primitive maps to; an unrecognized one is untyped.
@@ -1495,9 +1510,10 @@ module BiDiGenerate
1495
1510
  end
1496
1511
 
1497
1512
  # Public instance methods every accessor would shadow if it reused their name:
1498
- # Domain's own (`execute`/`initialize`) plus everything Object/Kernel expose. The
1499
- # collision guard fails generation before a schema-driven shadow can ship.
1500
- INHERITED_INSTANCE_METHODS = (%w[execute initialize].to_set + Object.instance_methods.to_set(&:to_s)).freeze
1513
+ # Domain's own plus everything Object/Kernel expose. The collision guard fails generation
1514
+ # before a schema-driven shadow can ship.
1515
+ INHERITED_INSTANCE_METHODS = (%w[execute initialize add_listener remove_listener].to_set +
1516
+ Object.instance_methods.to_set(&:to_s)).freeze
1501
1517
 
1502
1518
  # Fail generation if an accessor name would collide with a command method, an
1503
1519
  # inherited method, or another accessor — turning a future shadow into a build error
@@ -1542,7 +1558,8 @@ module BiDiGenerate
1542
1558
  def self.build_event(schema, event)
1543
1559
  params = event['params']
1544
1560
  payload_ref = params && params['ref'] && schema.structured_ref(params['ref'])
1545
- Event.new(wire_name: event['method'], event_name: camel_to_snake(event['name']), payload_ref: payload_ref)
1561
+ Event.new(wire_name: event['method'], event_name: camel_to_snake(event['name']), payload_ref: payload_ref,
1562
+ spec_href: event['specHref'])
1546
1563
  end
1547
1564
 
1548
1565
  # The projector tags lifted-out types with {synthetic, owner, label}. Emit each
@@ -21,7 +21,8 @@ module Selenium
21
21
  module WebDriver
22
22
  class BiDi
23
23
  # The seam between the generated Protocol layer and the websocket: serializes a
24
- # command's params, sends it, and parses the reply into its declared type.
24
+ # command's params, sends it, and parses the reply into its declared type, and relays
25
+ # event listeners to the connection's dispatcher.
25
26
  #
26
27
  # @api private
27
28
  class Transport
@@ -42,8 +43,20 @@ module Selenium
42
43
  result ? result.from_json(value) : value
43
44
  end
44
45
 
46
+ def add_listener(event, payload_type, &block)
47
+ dispatcher.add_listener(event, payload_type, &block)
48
+ end
49
+
50
+ def remove_listener(id)
51
+ dispatcher.remove_listener(id)
52
+ end
53
+
45
54
  private
46
55
 
56
+ def dispatcher
57
+ @connection.event_receiver { Dispatcher.new }
58
+ end
59
+
47
60
  def serialize(params)
48
61
  params&.as_json || {}
49
62
  end
@@ -33,6 +33,7 @@ module Selenium
33
33
  POLL_INTERVAL = 0.1
34
34
 
35
35
  attr_accessor :detach, :env
36
+ attr_reader :status
36
37
  attr_writer :io
37
38
 
38
39
  def self.build(*command)
@@ -30,7 +30,7 @@ module Selenium
30
30
 
31
31
  begin
32
32
  yield(caps, http_client) if block_given?
33
- rescue Selenium::WebDriver::Error::WebDriverError
33
+ rescue StandardError
34
34
  @service_manager&.stop
35
35
  raise
36
36
  end