selenium-webdriver 4.0.0.alpha2 → 4.0.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 (113) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGES +338 -4
  3. data/Gemfile +3 -1
  4. data/LICENSE +1 -1
  5. data/NOTICE +2 -0
  6. data/README.md +4 -5
  7. data/lib/selenium/server.rb +21 -29
  8. data/lib/selenium/webdriver/atoms/findElements.js +122 -0
  9. data/lib/selenium/webdriver/atoms/getAttribute.js +100 -7
  10. data/lib/selenium/webdriver/atoms/isDisplayed.js +76 -78
  11. data/lib/selenium/webdriver/atoms/mutationListener.js +55 -0
  12. data/lib/selenium/webdriver/chrome/driver.rb +26 -56
  13. data/lib/selenium/webdriver/chrome/features.rb +106 -0
  14. data/lib/selenium/webdriver/chrome/options.rb +127 -52
  15. data/lib/selenium/webdriver/chrome/profile.rb +8 -5
  16. data/lib/selenium/webdriver/chrome/service.rb +4 -6
  17. data/lib/selenium/webdriver/chrome.rb +10 -9
  18. data/lib/selenium/webdriver/common/driver.rb +110 -19
  19. data/lib/selenium/webdriver/common/driver_extensions/full_page_screenshot.rb +43 -0
  20. data/lib/selenium/webdriver/common/driver_extensions/has_apple_permissions.rb +51 -0
  21. data/lib/selenium/webdriver/common/driver_extensions/has_authentication.rb +89 -0
  22. data/lib/selenium/webdriver/common/driver_extensions/has_casting.rb +77 -0
  23. data/lib/selenium/webdriver/common/driver_extensions/has_cdp.rb +38 -0
  24. data/lib/selenium/webdriver/common/driver_extensions/has_context.rb +45 -0
  25. data/lib/selenium/webdriver/common/driver_extensions/has_devtools.rb +43 -0
  26. data/lib/selenium/webdriver/common/driver_extensions/has_launching.rb +38 -0
  27. data/lib/selenium/webdriver/common/driver_extensions/has_location.rb +5 -8
  28. data/lib/selenium/webdriver/common/driver_extensions/has_log_events.rb +144 -0
  29. data/lib/selenium/webdriver/common/driver_extensions/has_logs.rb +30 -0
  30. data/lib/selenium/webdriver/common/driver_extensions/has_network_conditions.rb +17 -0
  31. data/lib/selenium/webdriver/common/driver_extensions/has_network_connection.rb +6 -27
  32. data/lib/selenium/webdriver/common/driver_extensions/has_network_interception.rb +136 -0
  33. data/lib/selenium/webdriver/common/driver_extensions/has_permissions.rb +11 -11
  34. data/lib/selenium/webdriver/common/driver_extensions/has_pinned_scripts.rb +77 -0
  35. data/lib/selenium/webdriver/common/driver_extensions/has_remote_status.rb +1 -0
  36. data/lib/selenium/webdriver/common/driver_extensions/{rotatable.rb → prints_page.rb} +18 -20
  37. data/lib/selenium/webdriver/common/element.rb +79 -16
  38. data/lib/selenium/webdriver/common/error.rb +12 -0
  39. data/lib/selenium/webdriver/common/interactions/interaction.rb +4 -1
  40. data/lib/selenium/webdriver/common/log_entry.rb +2 -2
  41. data/lib/selenium/webdriver/common/logger.rb +50 -15
  42. data/lib/selenium/webdriver/common/manager.rb +14 -14
  43. data/lib/selenium/webdriver/common/options.rb +186 -0
  44. data/lib/selenium/webdriver/common/platform.rb +6 -1
  45. data/lib/selenium/webdriver/common/port_prober.rb +4 -6
  46. data/lib/selenium/webdriver/common/profile_helper.rb +10 -2
  47. data/lib/selenium/webdriver/common/proxy.rb +6 -3
  48. data/lib/selenium/webdriver/common/search_context.rb +7 -3
  49. data/lib/selenium/webdriver/common/service.rb +23 -113
  50. data/lib/selenium/webdriver/common/service_manager.rb +151 -0
  51. data/lib/selenium/webdriver/common/shadow_root.rb +87 -0
  52. data/lib/selenium/webdriver/common/socket_lock.rb +2 -2
  53. data/lib/selenium/webdriver/common/takes_screenshot.rb +66 -0
  54. data/lib/selenium/webdriver/common/target_locator.rb +32 -4
  55. data/lib/selenium/webdriver/common/timeouts.rb +31 -4
  56. data/lib/selenium/webdriver/common/wait.rb +1 -1
  57. data/lib/selenium/webdriver/common/window.rb +0 -4
  58. data/lib/selenium/webdriver/common.rb +17 -2
  59. data/lib/selenium/webdriver/devtools/console_event.rb +38 -0
  60. data/lib/selenium/webdriver/devtools/exception_event.rb +36 -0
  61. data/lib/selenium/webdriver/devtools/mutation_event.rb +37 -0
  62. data/lib/selenium/webdriver/{chrome/bridge.rb → devtools/pinned_script.rb} +26 -17
  63. data/lib/selenium/webdriver/devtools/request.rb +67 -0
  64. data/lib/selenium/webdriver/devtools/response.rb +66 -0
  65. data/lib/selenium/webdriver/devtools.rb +182 -0
  66. data/lib/selenium/webdriver/edge/driver.rb +5 -31
  67. data/lib/selenium/webdriver/edge/features.rb +44 -0
  68. data/lib/selenium/webdriver/edge/options.rb +11 -48
  69. data/lib/selenium/webdriver/edge/profile.rb +33 -0
  70. data/lib/selenium/webdriver/edge/service.rb +9 -24
  71. data/lib/selenium/webdriver/edge.rb +11 -13
  72. data/lib/selenium/webdriver/firefox/driver.rb +20 -30
  73. data/lib/selenium/webdriver/firefox/extension.rb +8 -0
  74. data/lib/selenium/webdriver/firefox/{bridge.rb → features.rb} +23 -4
  75. data/lib/selenium/webdriver/firefox/options.rb +70 -49
  76. data/lib/selenium/webdriver/firefox/profile.rb +16 -77
  77. data/lib/selenium/webdriver/firefox/service.rb +1 -5
  78. data/lib/selenium/webdriver/firefox.rb +22 -16
  79. data/lib/selenium/webdriver/ie/driver.rb +1 -34
  80. data/lib/selenium/webdriver/ie/options.rb +13 -44
  81. data/lib/selenium/webdriver/ie/service.rb +9 -11
  82. data/lib/selenium/webdriver/ie.rb +8 -7
  83. data/lib/selenium/webdriver/remote/bridge.rb +112 -86
  84. data/lib/selenium/webdriver/remote/capabilities.rb +120 -62
  85. data/lib/selenium/webdriver/remote/commands.rb +7 -0
  86. data/lib/selenium/webdriver/remote/driver.rb +15 -12
  87. data/lib/selenium/webdriver/remote/http/common.rb +0 -5
  88. data/lib/selenium/webdriver/remote/http/default.rb +17 -11
  89. data/lib/selenium/webdriver/remote/http/persistent.rb +11 -6
  90. data/lib/selenium/webdriver/remote.rb +15 -9
  91. data/lib/selenium/webdriver/safari/driver.rb +3 -34
  92. data/lib/selenium/webdriver/safari/{bridge.rb → features.rb} +6 -6
  93. data/lib/selenium/webdriver/safari/options.rb +10 -29
  94. data/lib/selenium/webdriver/safari/service.rb +0 -4
  95. data/lib/selenium/webdriver/safari.rb +16 -8
  96. data/lib/selenium/webdriver/support/block_event_listener.rb +1 -1
  97. data/lib/selenium/webdriver/support/cdp/domain.rb.erb +63 -0
  98. data/lib/selenium/webdriver/support/cdp_client_generator.rb +108 -0
  99. data/lib/selenium/webdriver/support/color.rb +2 -2
  100. data/lib/selenium/webdriver/support/event_firing_bridge.rb +4 -4
  101. data/lib/selenium/webdriver/support/guards/guard.rb +89 -0
  102. data/lib/selenium/webdriver/support/guards/guard_condition.rb +52 -0
  103. data/lib/selenium/webdriver/support/guards.rb +95 -0
  104. data/lib/selenium/webdriver/support/relative_locator.rb +51 -0
  105. data/lib/selenium/webdriver/support/select.rb +2 -2
  106. data/lib/selenium/webdriver/support.rb +1 -0
  107. data/lib/selenium/webdriver/version.rb +1 -1
  108. data/lib/selenium/webdriver.rb +10 -8
  109. data/selenium-webdriver.gemspec +29 -13
  110. metadata +125 -51
  111. data/lib/selenium/webdriver/common/driver_extensions/takes_screenshot.rb +0 -64
  112. data/lib/selenium/webdriver/firefox/binary.rb +0 -110
  113. data/lib/selenium/webdriver/firefox/extension/prefs.json +0 -69
@@ -30,6 +30,7 @@ module Selenium
30
30
  link_text: 'link text',
31
31
  name: 'name',
32
32
  partial_link_text: 'partial link text',
33
+ relative: 'relative',
33
34
  tag_name: 'tag name',
34
35
  xpath: 'xpath'
35
36
  }.freeze
@@ -59,7 +60,7 @@ module Selenium
59
60
  by = FINDERS[how.to_sym]
60
61
  raise ArgumentError, "cannot find element by #{how.inspect}" unless by
61
62
 
62
- bridge.find_element_by by, what.to_s, ref
63
+ bridge.find_element_by by, what, ref
63
64
  rescue Selenium::WebDriver::Error::TimeoutError
64
65
  # Implicit Wait times out in Edge
65
66
  raise Selenium::WebDriver::Error::NoSuchElementError
@@ -77,7 +78,7 @@ module Selenium
77
78
  by = FINDERS[how.to_sym]
78
79
  raise ArgumentError, "cannot find elements by #{how.inspect}" unless by
79
80
 
80
- bridge.find_elements_by by, what.to_s, ref
81
+ bridge.find_elements_by by, what, ref
81
82
  rescue Selenium::WebDriver::Error::TimeoutError
82
83
  # Implicit Wait times out in Edge
83
84
  []
@@ -92,7 +93,10 @@ module Selenium
92
93
  when 1
93
94
  arg = args.first
94
95
 
95
- raise ArgumentError, "expected #{arg.inspect}:#{arg.class} to respond to #shift" unless arg.respond_to?(:shift)
96
+ unless arg.respond_to?(:shift)
97
+ raise ArgumentError,
98
+ "expected #{arg.inspect}:#{arg.class} to respond to #shift"
99
+ end
96
100
 
97
101
  # this will be a single-entry hash, so use #shift over #first or #[]
98
102
  arr = arg.dup.shift
@@ -21,36 +21,33 @@ module Selenium
21
21
  module WebDriver
22
22
  #
23
23
  # Base class implementing default behavior of service object,
24
- # responsible for starting and stopping driver implementations.
24
+ # responsible for storing a service manager configuration.
25
25
  #
26
26
 
27
27
  class Service
28
- START_TIMEOUT = 20
29
- SOCKET_LOCK_TIMEOUT = 45
30
- STOP_TIMEOUT = 20
31
-
32
28
  class << self
33
29
  attr_reader :driver_path
34
30
 
35
- def chrome(*args)
36
- Chrome::Service.new(*args)
31
+ def chrome(**opts)
32
+ Chrome::Service.new(**opts)
37
33
  end
38
34
 
39
- def firefox(*args)
40
- Firefox::Service.new(*args)
35
+ def firefox(**opts)
36
+ Firefox::Service.new(**opts)
41
37
  end
42
38
 
43
- def ie(*args)
44
- IE::Service.new(*args)
39
+ def ie(**opts)
40
+ IE::Service.new(**opts)
45
41
  end
46
42
  alias_method :internet_explorer, :ie
47
43
 
48
- def edge(*args)
49
- Edge::Service.new(*args)
44
+ def edge(**opts)
45
+ Edge::Service.new(**opts)
50
46
  end
47
+ alias_method :microsoftedge, :edge
51
48
 
52
- def safari(*args)
53
- Safari::Service.new(*args)
49
+ def safari(**opts)
50
+ Safari::Service.new(**opts)
54
51
  end
55
52
 
56
53
  def driver_path=(path)
@@ -60,7 +57,7 @@ module Selenium
60
57
  end
61
58
 
62
59
  attr_accessor :host
63
- attr_reader :executable_path
60
+ attr_reader :executable_path, :port, :extra_args
64
61
 
65
62
  #
66
63
  # End users should use a class method for the desired driver, rather than using this directly.
@@ -82,31 +79,20 @@ module Selenium
82
79
  raise Error::WebDriverError, "invalid port: #{@port}" if @port < 1
83
80
  end
84
81
 
85
- def start
86
- raise "already started: #{uri.inspect} #{@executable_path.inspect}" if process_running?
87
-
88
- Platform.exit_hook(&method(:stop)) # make sure we don't leave the server running
89
-
90
- socket_lock.locked do
91
- find_free_port
92
- start_process
93
- connect_until_stable
94
- end
82
+ def launch
83
+ sm = ServiceManager.new(self)
84
+ sm.start
85
+ sm
95
86
  end
96
87
 
97
- def stop
98
- return unless self.class::SHUTDOWN_SUPPORTED
99
-
100
- stop_server
101
- @process.poll_for_exit STOP_TIMEOUT
102
- rescue ChildProcess::TimeoutError
103
- nil # noop
104
- ensure
105
- stop_process
88
+ def shutdown_supported
89
+ self.class::SHUTDOWN_SUPPORTED
106
90
  end
107
91
 
108
- def uri
109
- @uri ||= URI.parse("http://#{@host}:#{@port}")
92
+ protected
93
+
94
+ def extract_service_args(driver_opts)
95
+ driver_opts.key?(:args) ? driver_opts.delete(:args) : []
110
96
  end
111
97
 
112
98
  private
@@ -120,82 +106,6 @@ module Selenium
120
106
  Platform.assert_executable path
121
107
  path
122
108
  end
123
-
124
- def build_process(*command)
125
- WebDriver.logger.debug("Executing Process #{command}")
126
- @process = ChildProcess.build(*command)
127
- if WebDriver.logger.debug?
128
- @process.io.stdout = @process.io.stderr = WebDriver.logger.io
129
- elsif Platform.jruby?
130
- # Apparently we need to read the output of drivers on JRuby.
131
- @process.io.stdout = @process.io.stderr = File.new(Platform.null_device, 'w')
132
- end
133
-
134
- @process
135
- end
136
-
137
- def connect_to_server
138
- Net::HTTP.start(@host, @port) do |http|
139
- http.open_timeout = STOP_TIMEOUT / 2
140
- http.read_timeout = STOP_TIMEOUT / 2
141
-
142
- yield http
143
- end
144
- end
145
-
146
- def find_free_port
147
- @port = PortProber.above(@port)
148
- end
149
-
150
- def start_process
151
- @process = build_process(@executable_path, "--port=#{@port}", *@extra_args)
152
- # Note: this is a bug only in Windows 7
153
- @process.leader = true unless Platform.windows?
154
- @process.start
155
- end
156
-
157
- def stop_process
158
- return if process_exited?
159
-
160
- @process.stop STOP_TIMEOUT
161
- @process.io.stdout.close if Platform.jruby? && !WebDriver.logger.debug?
162
- end
163
-
164
- def stop_server
165
- return if process_exited?
166
-
167
- connect_to_server { |http| http.get('/shutdown') }
168
- end
169
-
170
- def process_running?
171
- defined?(@process) && @process&.alive?
172
- end
173
-
174
- def process_exited?
175
- @process.nil? || @process.exited?
176
- end
177
-
178
- def connect_until_stable
179
- socket_poller = SocketPoller.new @host, @port, START_TIMEOUT
180
- return if socket_poller.connected?
181
-
182
- raise Error::WebDriverError, cannot_connect_error_text
183
- end
184
-
185
- def cannot_connect_error_text
186
- "unable to connect to #{self.class::EXECUTABLE} #{@host}:#{@port}"
187
- end
188
-
189
- def socket_lock
190
- @socket_lock ||= SocketLock.new(@port - 1, SOCKET_LOCK_TIMEOUT)
191
- end
192
-
193
- protected
194
-
195
- def extract_service_args(driver_opts)
196
- driver_opts.key?(:args) ? driver_opts.delete(:args) : []
197
- end
198
-
199
109
  end # Service
200
110
  end # WebDriver
201
111
  end # Selenium
@@ -0,0 +1,151 @@
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
+ #
23
+ # Base class implementing default behavior of service_manager object,
24
+ # responsible for starting and stopping driver implementations.
25
+ #
26
+ # @api private
27
+ #
28
+ class ServiceManager
29
+ START_TIMEOUT = 20
30
+ SOCKET_LOCK_TIMEOUT = 45
31
+ STOP_TIMEOUT = 20
32
+
33
+ #
34
+ # End users should use a class method for the desired driver, rather than using this directly.
35
+ #
36
+ # @api private
37
+ #
38
+
39
+ def initialize(config)
40
+ @executable_path = config.executable_path
41
+ @host = Platform.localhost
42
+ @port = config.port
43
+ @extra_args = config.extra_args
44
+ @shutdown_supported = config.shutdown_supported
45
+
46
+ raise Error::WebDriverError, "invalid port: #{@port}" if @port < 1
47
+ end
48
+
49
+ def start
50
+ raise "already started: #{uri.inspect} #{@executable_path.inspect}" if process_running?
51
+
52
+ Platform.exit_hook(&method(:stop)) # make sure we don't leave the server running
53
+
54
+ socket_lock.locked do
55
+ find_free_port
56
+ start_process
57
+ connect_until_stable
58
+ end
59
+ end
60
+
61
+ def stop
62
+ return unless @shutdown_supported
63
+
64
+ stop_server
65
+ @process.poll_for_exit STOP_TIMEOUT
66
+ rescue ChildProcess::TimeoutError
67
+ nil # noop
68
+ ensure
69
+ stop_process
70
+ end
71
+
72
+ def uri
73
+ @uri ||= URI.parse("http://#{@host}:#{@port}")
74
+ end
75
+
76
+ private
77
+
78
+ def build_process(*command)
79
+ WebDriver.logger.debug("Executing Process #{command}")
80
+ @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
87
+
88
+ @process
89
+ end
90
+
91
+ def connect_to_server
92
+ Net::HTTP.start(@host, @port) do |http|
93
+ http.open_timeout = STOP_TIMEOUT / 2
94
+ http.read_timeout = STOP_TIMEOUT / 2
95
+
96
+ yield http
97
+ end
98
+ end
99
+
100
+ def find_free_port
101
+ @port = PortProber.above(@port)
102
+ end
103
+
104
+ def start_process
105
+ @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
+ @process.start
109
+ end
110
+
111
+ def stop_process
112
+ return if process_exited?
113
+
114
+ @process.stop STOP_TIMEOUT
115
+ @process.io.stdout.close if Platform.jruby? && !WebDriver.logger.debug?
116
+ end
117
+
118
+ def stop_server
119
+ return if process_exited?
120
+
121
+ connect_to_server do |http|
122
+ headers = WebDriver::Remote::Http::Common::DEFAULT_HEADERS.dup
123
+ http.get('/shutdown', headers)
124
+ end
125
+ end
126
+
127
+ def process_running?
128
+ defined?(@process) && @process&.alive?
129
+ end
130
+
131
+ def process_exited?
132
+ @process.nil? || @process.exited?
133
+ end
134
+
135
+ def connect_until_stable
136
+ socket_poller = SocketPoller.new @host, @port, START_TIMEOUT
137
+ return if socket_poller.connected?
138
+
139
+ raise Error::WebDriverError, cannot_connect_error_text
140
+ end
141
+
142
+ def cannot_connect_error_text
143
+ "unable to connect to #{@executable_path} #{@host}:#{@port}"
144
+ end
145
+
146
+ def socket_lock
147
+ @socket_lock ||= SocketLock.new(@port - 1, SOCKET_LOCK_TIMEOUT)
148
+ end
149
+ end # Service
150
+ end # WebDriver
151
+ end # Selenium
@@ -0,0 +1,87 @@
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 ShadowRoot
23
+ ROOT_KEY = 'shadow-6066-11e4-a52e-4f735466cecf'
24
+
25
+ include SearchContext
26
+
27
+ #
28
+ # Creates a new shadow root
29
+ #
30
+ # @api private
31
+ #
32
+
33
+ def initialize(bridge, id)
34
+ @bridge = bridge
35
+ @id = id
36
+ end
37
+
38
+ def inspect
39
+ format '#<%<class>s:0x%<hash>x id=%<id>s>', class: self.class, hash: hash * 2, id: @id.inspect
40
+ end
41
+
42
+ def ==(other)
43
+ other.is_a?(self.class) && ref == other.ref
44
+ end
45
+ alias_method :eql?, :==
46
+
47
+ def hash
48
+ @id.hash ^ @bridge.hash
49
+ end
50
+
51
+ #
52
+ # @api private
53
+ # @see SearchContext
54
+ #
55
+
56
+ def ref
57
+ [:shadow_root, @id]
58
+ end
59
+
60
+ #
61
+ # Convert to a ShadowRoot JSON Object for transmission over the wire.
62
+ # @see https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#basic-terms-and-concepts
63
+ #
64
+ # @api private
65
+ #
66
+
67
+ def to_json(*)
68
+ JSON.generate as_json
69
+ end
70
+
71
+ #
72
+ # For Rails 3 - http://jonathanjulian.com/2010/04/rails-to_json-or-as_json/
73
+ #
74
+ # @api private
75
+ #
76
+
77
+ def as_json(*)
78
+ {ROOT_KEY => @id}
79
+ end
80
+
81
+ private
82
+
83
+ attr_reader :bridge
84
+
85
+ end # ShadowRoot
86
+ end # WebDriver
87
+ end # Selenium
@@ -69,8 +69,8 @@ module Selenium
69
69
  ChildProcess.close_on_exec @server
70
70
 
71
71
  true
72
- rescue SocketError, Errno::EADDRINUSE, Errno::EBADF => ex
73
- WebDriver.logger.debug("#{self}: #{ex.message}")
72
+ rescue SocketError, Errno::EADDRINUSE, Errno::EBADF => e
73
+ WebDriver.logger.debug("#{self}: #{e.message}")
74
74
  false
75
75
  end
76
76
 
@@ -0,0 +1,66 @@
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
+ #
23
+ # @api private
24
+ #
25
+ module TakesScreenshot
26
+ #
27
+ # Save a PNG screenshot of the viewport to the given path
28
+ #
29
+ # @api public
30
+ #
31
+
32
+ def save_screenshot(png_path, full_page: false)
33
+ extension = File.extname(png_path).downcase
34
+ if extension != '.png'
35
+ WebDriver.logger.warn "name used for saved screenshot does not match file type. "\
36
+ "It should end with .png extension",
37
+ id: :screenshot
38
+ end
39
+ File.open(png_path, 'wb') { |f| f << screenshot_as(:png, full_page: full_page) }
40
+ end
41
+
42
+ #
43
+ # Return a PNG screenshot in the given format as a string
44
+ #
45
+ # @param [:base64, :png] format
46
+ # @param [Boolean] full_page allows taking full page screenshots if supported
47
+ # @return String screenshot
48
+ #
49
+ # @api public
50
+
51
+ def screenshot_as(format, full_page: false)
52
+ case format
53
+ when :base64
54
+ full_page ? full_screenshot : screenshot
55
+ when :png
56
+ screenshot_as(:base64, full_page: full_page).unpack1('m')
57
+ else
58
+ raise Error::UnsupportedOperationError, "unsupported format: #{format.inspect}"
59
+ end
60
+ rescue NameError
61
+ raise Error::UnsupportedOperationError, "Full Page Screenshots are not supported for #{inspect}"
62
+ end
63
+
64
+ end # TakesScreenshot
65
+ end # WebDriver
66
+ end # Selenium
@@ -44,6 +44,34 @@ module Selenium
44
44
  @bridge.switch_to_parent_frame
45
45
  end
46
46
 
47
+ #
48
+ # Switch to a new top-level browsing context
49
+ #
50
+ # @param type either :tab or :window
51
+ #
52
+
53
+ def new_window(type = :window)
54
+ unless %i[window tab].include?(type)
55
+ raise ArgumentError, "Valid types are :tab and :window, received: #{type.inspect}"
56
+ end
57
+
58
+ handle = @bridge.new_window(type)['handle']
59
+
60
+ if block_given?
61
+ execute_and_close = proc do
62
+ yield(self)
63
+ begin
64
+ @bridge.close
65
+ rescue Error::NoSuchWindowError
66
+ # window already closed
67
+ end
68
+ end
69
+ window(handle, &execute_and_close)
70
+ else
71
+ window(handle)
72
+ end
73
+ end
74
+
47
75
  #
48
76
  # switch to the given window handle
49
77
  #
@@ -57,10 +85,10 @@ module Selenium
57
85
  def window(id)
58
86
  if block_given?
59
87
  original = begin
60
- @bridge.window_handle
61
- rescue Error::NoSuchWindowError
62
- nil
63
- end
88
+ @bridge.window_handle
89
+ rescue Error::NoSuchWindowError
90
+ nil
91
+ end
64
92
 
65
93
  unless @bridge.window_handles.include? id
66
94
  raise Error::NoSuchWindowError, "The specified identifier '#{id}' is not found in the window handle list"
@@ -24,22 +24,49 @@ module Selenium
24
24
  @bridge = bridge
25
25
  end
26
26
 
27
+ #
28
+ # Gets the amount of time the driver should wait when searching for elements.
29
+ #
30
+
31
+ def implicit_wait
32
+ Float(@bridge.timeouts['implicit']) / 1000
33
+ end
34
+
27
35
  #
28
36
  # Set the amount of time the driver should wait when searching for elements.
29
37
  #
30
38
 
31
39
  def implicit_wait=(seconds)
32
- @bridge.implicit_wait_timeout = Integer(seconds * 1000)
40
+ @bridge.timeouts = {'implicit' => Integer(seconds * 1000)}
33
41
  end
34
42
 
43
+ #
44
+ # Gets the amount of time to wait for an asynchronous script to finish
45
+ # execution before throwing an error.
46
+ #
47
+
48
+ def script
49
+ Float(@bridge.timeouts['script']) / 1000
50
+ end
51
+ alias_method :script_timeout, :script
52
+
35
53
  #
36
54
  # Sets the amount of time to wait for an asynchronous script to finish
37
55
  # execution before throwing an error. If the timeout is negative, then the
38
56
  # script will be allowed to run indefinitely.
39
57
  #
40
58
 
41
- def script_timeout=(seconds)
42
- @bridge.script_timeout = Integer(seconds * 1000)
59
+ def script=(seconds)
60
+ @bridge.timeouts = {'script' => Integer(seconds * 1000)}
61
+ end
62
+ alias_method :script_timeout=, :script=
63
+
64
+ #
65
+ # Gets the amount of time to wait for a page load to complete before throwing an error.
66
+ #
67
+
68
+ def page_load
69
+ Float(@bridge.timeouts['pageLoad']) / 1000
43
70
  end
44
71
 
45
72
  #
@@ -48,7 +75,7 @@ module Selenium
48
75
  #
49
76
 
50
77
  def page_load=(seconds)
51
- @bridge.timeout 'page load', Integer(seconds * 1000)
78
+ @bridge.timeouts = {'pageLoad' => Integer(seconds * 1000)}
52
79
  end
53
80
  end # Timeouts
54
81
  end # WebDriver
@@ -55,7 +55,7 @@ module Selenium
55
55
  begin
56
56
  result = yield
57
57
  return result if result
58
- rescue *@ignored => last_error
58
+ rescue *@ignored => last_error # rubocop:disable Naming/RescuedExceptionsVariableName
59
59
  # swallowed
60
60
  end
61
61
 
@@ -19,10 +19,6 @@
19
19
 
20
20
  module Selenium
21
21
  module WebDriver
22
- #
23
- # @api beta This API may be changed or removed in a future release.
24
- #
25
-
26
22
  class Window
27
23
  #
28
24
  # @api private