selenium-webdriver 4.1.0 → 4.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES +141 -1
  3. data/LICENSE +1 -1
  4. data/NOTICE +1 -1
  5. data/bin/linux/selenium-manager +0 -0
  6. data/bin/macos/selenium-manager +0 -0
  7. data/bin/windows/selenium-manager.exe +0 -0
  8. data/lib/selenium/server.rb +34 -26
  9. data/lib/selenium/webdriver/atoms/findElements.js +0 -0
  10. data/lib/selenium/webdriver/atoms/getAttribute.js +0 -0
  11. data/lib/selenium/webdriver/atoms/isDisplayed.js +0 -0
  12. data/lib/selenium/webdriver/atoms/mutationListener.js +0 -0
  13. data/lib/selenium/webdriver/bidi/session.rb +38 -0
  14. data/lib/selenium/webdriver/bidi.rb +55 -0
  15. data/lib/selenium/webdriver/chrome/driver.rb +1 -0
  16. data/lib/selenium/webdriver/chrome/features.rb +5 -0
  17. data/lib/selenium/webdriver/chrome/options.rb +33 -19
  18. data/lib/selenium/webdriver/chrome/service.rb +1 -1
  19. data/lib/selenium/webdriver/chrome.rb +0 -14
  20. data/lib/selenium/webdriver/common/action_builder.rb +108 -21
  21. data/lib/selenium/webdriver/common/child_process.rb +126 -0
  22. data/lib/selenium/webdriver/common/driver.rb +22 -55
  23. data/lib/selenium/webdriver/common/driver_extensions/{has_remote_status.rb → has_bidi.rb} +12 -5
  24. data/lib/selenium/webdriver/common/driver_extensions/has_casting.rb +10 -0
  25. data/lib/selenium/webdriver/common/driver_extensions/has_context.rb +1 -2
  26. data/lib/selenium/webdriver/common/driver_extensions/has_log_events.rb +1 -1
  27. data/lib/selenium/webdriver/common/driver_extensions/has_network_interception.rb +2 -67
  28. data/lib/selenium/webdriver/common/driver_extensions/has_pinned_scripts.rb +1 -1
  29. data/lib/selenium/webdriver/common/element.rb +2 -2
  30. data/lib/selenium/webdriver/common/error.rb +1 -1
  31. data/lib/selenium/webdriver/common/interactions/input_device.rb +10 -4
  32. data/lib/selenium/webdriver/common/interactions/interaction.rb +12 -25
  33. data/lib/selenium/webdriver/common/interactions/interactions.rb +24 -4
  34. data/lib/selenium/webdriver/common/interactions/key_actions.rb +5 -1
  35. data/lib/selenium/webdriver/common/interactions/key_input.rb +11 -27
  36. data/lib/selenium/webdriver/common/interactions/none_input.rb +10 -8
  37. data/lib/selenium/webdriver/common/interactions/pause.rb +49 -0
  38. data/lib/selenium/webdriver/common/interactions/pointer_actions.rb +59 -70
  39. data/lib/selenium/webdriver/common/interactions/pointer_cancel.rb +45 -0
  40. data/lib/selenium/webdriver/common/interactions/pointer_event_properties.rb +63 -0
  41. data/lib/selenium/webdriver/common/interactions/pointer_input.rb +15 -84
  42. data/lib/selenium/webdriver/common/interactions/pointer_move.rb +60 -0
  43. data/lib/selenium/webdriver/common/interactions/pointer_press.rb +85 -0
  44. data/lib/selenium/webdriver/common/interactions/scroll.rb +57 -0
  45. data/lib/selenium/webdriver/common/interactions/scroll_origin.rb +48 -0
  46. data/lib/selenium/webdriver/common/interactions/typing_interaction.rb +54 -0
  47. data/lib/selenium/webdriver/common/interactions/wheel_actions.rb +113 -0
  48. data/lib/selenium/webdriver/common/interactions/wheel_input.rb +42 -0
  49. data/lib/selenium/webdriver/common/keys.rb +1 -0
  50. data/lib/selenium/webdriver/common/manager.rb +0 -27
  51. data/lib/selenium/webdriver/common/options.rb +2 -9
  52. data/lib/selenium/webdriver/common/platform.rb +8 -5
  53. data/lib/selenium/webdriver/common/search_context.rb +0 -6
  54. data/lib/selenium/webdriver/common/selenium_manager.rb +91 -0
  55. data/lib/selenium/webdriver/common/service.rb +7 -3
  56. data/lib/selenium/webdriver/common/service_manager.rb +3 -12
  57. data/lib/selenium/webdriver/common/shadow_root.rb +1 -1
  58. data/lib/selenium/webdriver/common/socket_lock.rb +1 -2
  59. data/lib/selenium/webdriver/common/socket_poller.rb +1 -1
  60. data/lib/selenium/webdriver/common/takes_screenshot.rb +1 -1
  61. data/lib/selenium/webdriver/common/virtual_authenticator/credential.rb +83 -0
  62. data/lib/selenium/webdriver/common/virtual_authenticator/virtual_authenticator.rb +73 -0
  63. data/lib/selenium/webdriver/common/virtual_authenticator/virtual_authenticator_options.rb +62 -0
  64. data/lib/selenium/webdriver/common/websocket_connection.rb +165 -0
  65. data/lib/selenium/webdriver/common/window.rb +6 -6
  66. data/lib/selenium/webdriver/common/zipper.rb +1 -1
  67. data/lib/selenium/webdriver/common.rb +19 -3
  68. data/lib/selenium/webdriver/devtools/network_interceptor.rb +176 -0
  69. data/lib/selenium/webdriver/devtools/request.rb +1 -1
  70. data/lib/selenium/webdriver/devtools/response.rb +1 -1
  71. data/lib/selenium/webdriver/devtools.rb +6 -112
  72. data/lib/selenium/webdriver/edge/features.rb +1 -0
  73. data/lib/selenium/webdriver/firefox/driver.rb +1 -0
  74. data/lib/selenium/webdriver/firefox/features.rb +6 -5
  75. data/lib/selenium/webdriver/firefox/options.rb +5 -2
  76. data/lib/selenium/webdriver/firefox/profile.rb +1 -5
  77. data/lib/selenium/webdriver/firefox/util.rb +46 -0
  78. data/lib/selenium/webdriver/firefox.rb +1 -14
  79. data/lib/selenium/webdriver/ie.rb +0 -14
  80. data/lib/selenium/webdriver/remote/bridge.rb +54 -19
  81. data/lib/selenium/webdriver/remote/commands.rb +15 -6
  82. data/lib/selenium/webdriver/remote/driver.rb +0 -1
  83. data/lib/selenium/webdriver/remote/http/default.rb +6 -12
  84. data/lib/selenium/webdriver/remote/response.rb +2 -2
  85. data/lib/selenium/webdriver/safari/options.rb +1 -1
  86. data/lib/selenium/webdriver/safari.rb +0 -14
  87. data/lib/selenium/webdriver/support/cdp_client_generator.rb +4 -4
  88. data/lib/selenium/webdriver/support/color.rb +7 -7
  89. data/lib/selenium/webdriver/support/guards/guard_condition.rb +1 -1
  90. data/lib/selenium/webdriver/support/guards.rb +1 -1
  91. data/lib/selenium/webdriver/support/nightly_version_generator.rb +60 -0
  92. data/lib/selenium/webdriver/support/select.rb +3 -1
  93. data/lib/selenium/webdriver/version.rb +1 -1
  94. data/lib/selenium/webdriver.rb +1 -1
  95. data/selenium-webdriver.gemspec +14 -10
  96. metadata +66 -42
  97. data/lib/selenium/webdriver/remote/http/persistent.rb +0 -65
  98. data/lib/selenium/webdriver/support/cdp/domain.rb.erb +0 -63
@@ -61,9 +61,6 @@ module Selenium
61
61
  raise ArgumentError, "cannot find element by #{how.inspect}" unless by
62
62
 
63
63
  bridge.find_element_by by, what, ref
64
- rescue Selenium::WebDriver::Error::TimeoutError
65
- # Implicit Wait times out in Edge
66
- raise Selenium::WebDriver::Error::NoSuchElementError
67
64
  end
68
65
 
69
66
  #
@@ -79,9 +76,6 @@ module Selenium
79
76
  raise ArgumentError, "cannot find elements by #{how.inspect}" unless by
80
77
 
81
78
  bridge.find_elements_by by, what, ref
82
- rescue Selenium::WebDriver::Error::TimeoutError
83
- # Implicit Wait times out in Edge
84
- []
85
79
  end
86
80
 
87
81
  private
@@ -0,0 +1,91 @@
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 'open3'
21
+
22
+ module Selenium
23
+ module WebDriver
24
+ #
25
+ # Wrapper for getting information from the Selenium Manager binaries.
26
+ # This implementation is still in beta, and may change.
27
+ # @api private
28
+ #
29
+ class SeleniumManager
30
+ BIN_PATH = "../../../../../bin"
31
+
32
+ class << self
33
+ # @param [String] driver_name which driver to use.
34
+ # @return [String] the path to the correct driver.
35
+ def driver_path(driver_name)
36
+ @driver_path ||= begin
37
+ unless %w[chromedriver geckodriver msedgedriver IEDriverServer].include?(driver_name)
38
+ msg = "Unable to locate driver with name: #{driver_name}"
39
+ raise Error::WebDriverError, msg
40
+ end
41
+
42
+ location = run("#{binary} --driver #{driver_name}")
43
+ WebDriver.logger.debug("Driver found at #{location}")
44
+ Platform.assert_executable location
45
+
46
+ location
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ # @return [String] the path to the correct selenium manager
53
+ def binary
54
+ @binary ||= begin
55
+ path = File.expand_path(BIN_PATH, __FILE__)
56
+ path << if Platform.windows?
57
+ '/windows/selenium-manager.exe'
58
+ elsif Platform.mac?
59
+ '/macos/selenium-manager'
60
+ elsif Platform.linux?
61
+ '/linux/selenium-manager'
62
+ end
63
+ location = File.expand_path(path, __FILE__)
64
+ unless location.is_a?(String) && File.exist?(location) && File.executable?(location)
65
+ raise Error::WebDriverError, "Unable to obtain Selenium Manager"
66
+ end
67
+
68
+ WebDriver.logger.debug("Selenium Manager found at #{location}")
69
+ location
70
+ end
71
+ end
72
+
73
+ def run(command)
74
+ WebDriver.logger.debug("Executing Process #{command}")
75
+
76
+ begin
77
+ stdout, stderr, status = Open3.capture3(command)
78
+ rescue StandardError => e
79
+ raise Error::WebDriverError, "Unsuccessful command executed: #{command}", e.message
80
+ end
81
+
82
+ if status.exitstatus.positive?
83
+ raise Error::WebDriverError, "Unsuccessful command executed: #{command}\n#{stdout}#{stderr}"
84
+ end
85
+
86
+ stdout.gsub("INFO\t", '').strip
87
+ end
88
+ end
89
+ end # SeleniumManager
90
+ end # WebDriver
91
+ end # Selenium
@@ -80,9 +80,7 @@ module Selenium
80
80
  end
81
81
 
82
82
  def launch
83
- sm = ServiceManager.new(self)
84
- sm.start
85
- sm
83
+ ServiceManager.new(self).tap(&:start)
86
84
  end
87
85
 
88
86
  def shutdown_supported
@@ -101,6 +99,12 @@ module Selenium
101
99
  path = path.call if path.is_a?(Proc)
102
100
  path ||= Platform.find_binary(self.class::EXECUTABLE)
103
101
 
102
+ begin
103
+ path ||= SeleniumManager.driver_path(self.class::EXECUTABLE)
104
+ rescue Error::WebDriverError => e
105
+ WebDriver.logger.debug("Unable obtain driver using Selenium Manager\n #{e.message}")
106
+ end
107
+
104
108
  raise Error::WebDriverError, self.class::MISSING_TEXT unless path
105
109
 
106
110
  Platform.assert_executable path
@@ -49,7 +49,7 @@ module Selenium
49
49
  def start
50
50
  raise "already started: #{uri.inspect} #{@executable_path.inspect}" if process_running?
51
51
 
52
- Platform.exit_hook(&method(:stop)) # make sure we don't leave the server running
52
+ Platform.exit_hook { stop } # make sure we don't leave the server running
53
53
 
54
54
  socket_lock.locked do
55
55
  find_free_port
@@ -60,6 +60,7 @@ module Selenium
60
60
 
61
61
  def stop
62
62
  return unless @shutdown_supported
63
+ return if process_exited?
63
64
 
64
65
  stop_server
65
66
  @process.poll_for_exit STOP_TIMEOUT
@@ -78,12 +79,7 @@ module Selenium
78
79
  def build_process(*command)
79
80
  WebDriver.logger.debug("Executing Process #{command}")
80
81
  @process = ChildProcess.build(*command)
81
- if WebDriver.logger.debug?
82
- @process.io.stdout = @process.io.stderr = WebDriver.logger.io
83
- elsif Platform.jruby?
84
- # Apparently we need to read the output of drivers on JRuby.
85
- @process.io.stdout = @process.io.stderr = File.new(Platform.null_device, 'w')
86
- end
82
+ @process.io = WebDriver.logger.io if WebDriver.logger.debug?
87
83
 
88
84
  @process
89
85
  end
@@ -103,8 +99,6 @@ module Selenium
103
99
 
104
100
  def start_process
105
101
  @process = build_process(@executable_path, "--port=#{@port}", *@extra_args)
106
- # NOTE: this is a bug only in Windows 7
107
- @process.leader = true unless Platform.windows?
108
102
  @process.start
109
103
  end
110
104
 
@@ -112,12 +106,9 @@ module Selenium
112
106
  return if process_exited?
113
107
 
114
108
  @process.stop STOP_TIMEOUT
115
- @process.io.stdout.close if Platform.jruby? && !WebDriver.logger.debug?
116
109
  end
117
110
 
118
111
  def stop_server
119
- return if process_exited?
120
-
121
112
  connect_to_server do |http|
122
113
  headers = WebDriver::Remote::Http::Common::DEFAULT_HEADERS.dup
123
114
  http.get('/shutdown', headers)
@@ -45,7 +45,7 @@ module Selenium
45
45
  alias_method :eql?, :==
46
46
 
47
47
  def hash
48
- @id.hash ^ @bridge.hash
48
+ [@id, @bridge].hash
49
49
  end
50
50
 
51
51
  #
@@ -66,8 +66,7 @@ module Selenium
66
66
 
67
67
  def can_lock?
68
68
  @server = TCPServer.new(Platform.localhost, @port)
69
- ChildProcess.close_on_exec @server
70
-
69
+ @server.close_on_exec = true
71
70
  true
72
71
  rescue SocketError, Errno::EADDRINUSE, Errno::EBADF => e
73
72
  WebDriver.logger.debug("#{self}: #{e.message}")
@@ -99,7 +99,7 @@ module Selenium
99
99
  end
100
100
 
101
101
  def socket_writable?(sock)
102
- IO.select(nil, [sock], nil, CONNECT_TIMEOUT)
102
+ sock.wait_writable(CONNECT_TIMEOUT)
103
103
  end
104
104
 
105
105
  def conn_completed?(sock)
@@ -32,7 +32,7 @@ module Selenium
32
32
  def save_screenshot(png_path, full_page: false)
33
33
  extension = File.extname(png_path).downcase
34
34
  if extension != '.png'
35
- WebDriver.logger.warn "name used for saved screenshot does not match file type. "\
35
+ WebDriver.logger.warn "name used for saved screenshot does not match file type. " \
36
36
  "It should end with .png extension",
37
37
  id: :screenshot
38
38
  end
@@ -0,0 +1,83 @@
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
+ #
21
+ # A credential stored in a virtual authenticator.
22
+ # @see https://w3c.github.io/webauthn/#credential-parameters
23
+ #
24
+
25
+ module Selenium
26
+ module WebDriver
27
+ class Credential
28
+ class << self
29
+ def resident(**opts)
30
+ Credential.new(resident_credential: true, **opts)
31
+ end
32
+
33
+ def non_resident(**opts)
34
+ Credential.new(resident_credential: false, **opts)
35
+ end
36
+
37
+ def encode(byte_array)
38
+ Base64.urlsafe_encode64(byte_array&.pack('C*'))
39
+ end
40
+
41
+ def decode(base64)
42
+ Base64.urlsafe_decode64(base64).unpack('C*')
43
+ end
44
+
45
+ def from_json(opts)
46
+ user_handle = opts['userHandle'] ? decode(opts['userHandle']) : nil
47
+ new(id: decode(opts["credentialId"]),
48
+ resident_credential: opts["isResidentCredential"],
49
+ rp_id: opts['rpId'],
50
+ private_key: opts['privateKey'],
51
+ sign_count: opts['signCount'],
52
+ user_handle: user_handle)
53
+ end
54
+ end
55
+
56
+ attr_reader :id, :resident_credential, :rp_id, :user_handle, :private_key, :sign_count
57
+ alias_method :resident_credential?, :resident_credential
58
+
59
+ def initialize(id:, resident_credential:, rp_id:, private_key:, user_handle: nil, sign_count: 0)
60
+ @id = id
61
+ @resident_credential = resident_credential
62
+ @rp_id = rp_id
63
+ @user_handle = user_handle
64
+ @private_key = private_key
65
+ @sign_count = sign_count
66
+ end
67
+
68
+ #
69
+ # @api private
70
+ #
71
+
72
+ def as_json(*)
73
+ credential_data = {'credentialId' => Credential.encode(id),
74
+ 'isResidentCredential' => resident_credential?,
75
+ 'rpId' => rp_id,
76
+ 'privateKey' => Credential.encode(private_key),
77
+ 'signCount' => sign_count}
78
+ credential_data['userHandle'] = Credential.encode(user_handle) if user_handle
79
+ credential_data
80
+ end
81
+ end # Credential
82
+ end # WebDriver
83
+ end # Selenium
@@ -0,0 +1,73 @@
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 VirtualAuthenticator
23
+
24
+ attr_reader :options
25
+
26
+ #
27
+ # api private
28
+ # Use `Driver#add_virtual_authenticator`
29
+ #
30
+
31
+ def initialize(bridge, authenticator_id, options)
32
+ @id = authenticator_id
33
+ @bridge = bridge
34
+ @options = options
35
+ @valid = true
36
+ end
37
+
38
+ def add_credential(credential)
39
+ credential = credential.as_json
40
+ @bridge.add_credential credential, @id
41
+ end
42
+
43
+ def credentials
44
+ credential_data = @bridge.credentials @id
45
+ credential_data.map do |cred|
46
+ Credential.from_json(cred)
47
+ end
48
+ end
49
+
50
+ def remove_credential(credential_id)
51
+ credential_id = Credential.encode(credential_id) if credential_id.instance_of?(Array)
52
+ @bridge.remove_credential credential_id, @id
53
+ end
54
+
55
+ def remove_all_credentials
56
+ @bridge.remove_all_credentials @id
57
+ end
58
+
59
+ def user_verified=(verified)
60
+ @bridge.user_verified verified, @id
61
+ end
62
+
63
+ def remove!
64
+ @bridge.remove_virtual_authenticator(@id)
65
+ @valid = false
66
+ end
67
+
68
+ def valid?
69
+ @valid
70
+ end
71
+ end # VirtualAuthenticator
72
+ end # WebDriver
73
+ end # Selenium
@@ -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
+ #
21
+ # Options for the creation of virtual authenticators.
22
+ # @see http://w3c.github.io/webauthn/#sctn-automation
23
+ #
24
+
25
+ module Selenium
26
+ module WebDriver
27
+ class VirtualAuthenticatorOptions
28
+
29
+ PROTOCOL = {ctap2: "ctap2", u2f: "ctap1/u2f"}.freeze
30
+ TRANSPORT = {ble: "ble", usb: "usb", nfc: "nfc", internal: "internal"}.freeze
31
+
32
+ attr_accessor :protocol, :transport, :resident_key, :user_verification, :user_consenting, :user_verified
33
+ alias_method :resident_key?, :resident_key
34
+ alias_method :user_verification?, :user_verification
35
+ alias_method :user_consenting?, :user_consenting
36
+ alias_method :user_verified?, :user_verified
37
+
38
+ def initialize(protocol: :ctap2, transport: :usb, resident_key: false,
39
+ user_verification: false, user_consenting: true, user_verified: false)
40
+ @protocol = protocol
41
+ @transport = transport
42
+ @resident_key = resident_key
43
+ @user_verification = user_verification
44
+ @user_consenting = user_consenting
45
+ @user_verified = user_verified
46
+ end
47
+
48
+ #
49
+ # @api private
50
+ #
51
+
52
+ def as_json(*)
53
+ {'protocol' => PROTOCOL[protocol],
54
+ 'transport' => TRANSPORT[transport],
55
+ 'hasResidentKey' => resident_key?,
56
+ 'hasUserVerification' => user_verification?,
57
+ 'isUserConsenting' => user_consenting?,
58
+ 'isUserVerified' => user_verified?}
59
+ end
60
+ end # VirtualAuthenticatorOptions
61
+ end # WebDriver
62
+ end # Selenium
@@ -0,0 +1,165 @@
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 'websocket'
21
+
22
+ module Selenium
23
+ module WebDriver
24
+ class WebSocketConnection
25
+ CONNECTION_ERRORS = [
26
+ Errno::ECONNRESET, # connection is aborted (browser process was killed)
27
+ Errno::EPIPE # broken pipe (browser process was killed)
28
+ ].freeze
29
+
30
+ RESPONSE_WAIT_TIMEOUT = 30
31
+ RESPONSE_WAIT_INTERVAL = 0.1
32
+
33
+ MAX_LOG_MESSAGE_SIZE = 9999
34
+
35
+ def initialize(url:)
36
+ @callback_threads = ThreadGroup.new
37
+
38
+ @session_id = nil
39
+ @url = url
40
+
41
+ process_handshake
42
+ @socket_thread = attach_socket_listener
43
+ end
44
+
45
+ def close
46
+ @callback_threads.list.each(&:exit)
47
+ @socket_thread.exit
48
+ socket.close
49
+ end
50
+
51
+ def callbacks
52
+ @callbacks ||= Hash.new { |callbacks, event| callbacks[event] = [] }
53
+ end
54
+
55
+ def send_cmd(**payload)
56
+ id = next_id
57
+ data = payload.merge(id: id)
58
+ WebDriver.logger.debug "WebSocket -> #{data}"[...MAX_LOG_MESSAGE_SIZE]
59
+ data = JSON.generate(data)
60
+ out_frame = WebSocket::Frame::Outgoing::Client.new(version: ws.version, data: data, type: 'text')
61
+ socket.write(out_frame.to_s)
62
+
63
+ wait.until { messages.delete(id) }
64
+ end
65
+
66
+ private
67
+
68
+ # We should be thread-safe to use the hash without synchronization
69
+ # because its keys are WebSocket message identifiers and they should be
70
+ # unique within a devtools session.
71
+ def messages
72
+ @messages ||= {}
73
+ end
74
+
75
+ def process_handshake
76
+ socket.print(ws.to_s)
77
+ ws << socket.readpartial(1024)
78
+ end
79
+
80
+ def attach_socket_listener
81
+ Thread.new do
82
+ Thread.current.abort_on_exception = true
83
+ Thread.current.report_on_exception = false
84
+
85
+ until socket.eof?
86
+ incoming_frame << socket.readpartial(1024)
87
+
88
+ while (frame = incoming_frame.next)
89
+ message = process_frame(frame)
90
+ next unless message['method']
91
+
92
+ params = message['params']
93
+ callbacks[message['method']].each do |callback|
94
+ @callback_threads.add(callback_thread(params, &callback))
95
+ end
96
+ end
97
+ end
98
+ rescue *CONNECTION_ERRORS
99
+ Thread.stop
100
+ end
101
+ end
102
+
103
+ def incoming_frame
104
+ @incoming_frame ||= WebSocket::Frame::Incoming::Client.new(version: ws.version)
105
+ end
106
+
107
+ def process_frame(frame)
108
+ message = frame.to_s
109
+
110
+ # Firefox will periodically fail on unparsable empty frame
111
+ return {} if message.empty?
112
+
113
+ message = JSON.parse(message)
114
+ messages[message["id"]] = message
115
+ WebDriver.logger.debug "WebSocket <- #{message}"[...MAX_LOG_MESSAGE_SIZE]
116
+
117
+ message
118
+ end
119
+
120
+ def callback_thread(params)
121
+ Thread.new do
122
+ Thread.current.abort_on_exception = true
123
+
124
+ # We might end up blocked forever when we have an error in event.
125
+ # For example, if network interception event raises error,
126
+ # the browser will keep waiting for the request to be proceeded
127
+ # before returning back to the original thread. In this case,
128
+ # we should at least print the error.
129
+ Thread.current.report_on_exception = true
130
+
131
+ yield params
132
+ rescue *CONNECTION_ERRORS
133
+ Thread.stop
134
+ end
135
+ end
136
+
137
+ def wait
138
+ @wait ||= Wait.new(timeout: RESPONSE_WAIT_TIMEOUT, interval: RESPONSE_WAIT_INTERVAL)
139
+ end
140
+
141
+ def socket
142
+ @socket ||= if URI(@url).scheme == 'wss'
143
+ socket = TCPSocket.new(ws.host, ws.port)
144
+ socket = OpenSSL::SSL::SSLSocket.new(socket, OpenSSL::SSL::SSLContext.new)
145
+ socket.sync_close = true
146
+ socket.connect
147
+
148
+ socket
149
+ else
150
+ TCPSocket.new(ws.host, ws.port)
151
+ end
152
+ end
153
+
154
+ def ws
155
+ @ws ||= WebSocket::Handshake::Client.new(url: @url)
156
+ end
157
+
158
+ def next_id
159
+ @id ||= 0
160
+ @id += 1
161
+ end
162
+
163
+ end # BiDi
164
+ end # WebDriver
165
+ end # Selenium
@@ -36,8 +36,8 @@ module Selenium
36
36
 
37
37
  def size=(dimension)
38
38
  unless dimension.respond_to?(:width) && dimension.respond_to?(:height)
39
- raise ArgumentError, "expected #{dimension.inspect}:#{dimension.class}" \
40
- ' to respond to #width and #height'
39
+ raise ArgumentError, "expected #{dimension.inspect}:#{dimension.class} " \
40
+ 'to respond to #width and #height'
41
41
  end
42
42
 
43
43
  @bridge.resize_window dimension.width, dimension.height
@@ -61,8 +61,8 @@ module Selenium
61
61
 
62
62
  def position=(point)
63
63
  unless point.respond_to?(:x) && point.respond_to?(:y)
64
- raise ArgumentError, "expected #{point.inspect}:#{point.class}" \
65
- ' to respond to #x and #y'
64
+ raise ArgumentError, "expected #{point.inspect}:#{point.class} " \
65
+ 'to respond to #x and #y'
66
66
  end
67
67
 
68
68
  @bridge.reposition_window point.x, point.y
@@ -86,8 +86,8 @@ module Selenium
86
86
 
87
87
  def rect=(rectangle)
88
88
  unless %w[x y width height].all? { |val| rectangle.respond_to? val }
89
- raise ArgumentError, "expected #{rectangle.inspect}:#{rectangle.class}" \
90
- ' to respond to #x, #y, #width, and #height'
89
+ raise ArgumentError, "expected #{rectangle.inspect}:#{rectangle.class} " \
90
+ 'to respond to #x, #y, #width, and #height'
91
91
  end
92
92
 
93
93
  @bridge.set_window_rect(x: rectangle.x,
@@ -41,7 +41,7 @@ module Selenium
41
41
  to = File.join(destination, entry.name)
42
42
  dirname = File.dirname(to)
43
43
 
44
- FileUtils.mkdir_p dirname unless File.exist? dirname
44
+ FileUtils.mkdir_p dirname
45
45
  zip.extract(entry, to)
46
46
  end
47
47
  end