Skip to content

Commit 3c038ec

Browse files
committed
[rb] add custom matchers for deprecations
1 parent 61ccd71 commit 3c038ec

File tree

14 files changed

+104
-40
lines changed

14 files changed

+104
-40
lines changed

rb/lib/selenium/webdriver/common/driver.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def service_url(opts)
319319
next unless opts.key? key
320320

321321
WebDriver.logger.deprecate(":#{key}", ':service with an instance of Selenium::WebDriver::Service',
322-
id: :service)
322+
id: "service_#{key}".to_sym)
323323
end
324324
@service ||= Service.send(browser,
325325
args: opts.delete(:driver_opts),

rb/lib/selenium/webdriver/common/logger.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,18 @@ def warn(message, id: [])
112112
# @yield appends additional message to end of provided template
113113
#
114114
def deprecate(old, new = nil, id: [], &block)
115-
return if @ignored.include?(:deprecations) || (@ignored & Array(id)).any?
115+
id = Array(id)
116+
return if @ignored.include?(:deprecations) || (@ignored & id).any?
117+
118+
ids = id.empty? ? '' : "[#{id.map(&:inspect).join(', ')}] "
116119

117-
message = +"[DEPRECATION] #{old} is deprecated"
120+
message = +"[DEPRECATION] #{ids}#{old} is deprecated"
118121
message << if new
119122
". Use #{new} instead."
120123
else
121124
' and will be removed in a future release.'
122125
end
123-
warn message, id: id, &block
126+
warn message, &block
124127
end
125128

126129
private

rb/spec/integration/selenium/webdriver/driver_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module WebDriver
5757
path = "#{Dir.tmpdir}/test#{SecureRandom.urlsafe_base64}.jpg"
5858
message = "name used for saved screenshot does not match file type. "\
5959
"It should end with .png extension"
60-
expect(WebDriver.logger).to receive(:warn).with(message)
60+
expect(WebDriver.logger).to receive(:warn).with(message, id: :screenshot)
6161

6262
save_screenshot_and_assert(path)
6363
end

rb/spec/integration/selenium/webdriver/manager_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ module WebDriver
4242
expect(entries.first).to be_kind_of(LogEntry)
4343
end
4444

45-
it 'can get the driver log' do
45+
# Chrome - turned off by default
46+
it 'can get the driver log', except: {browser: %i[chrome edge_chrome]} do
4647
driver.navigate.to url_for('simpleTest.html')
4748

4849
entries = driver.manage.logs.get(:driver)

rb/spec/integration/selenium/webdriver/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
require 'selenium-webdriver'
2525
require_relative 'spec_support'
26+
require_relative '../../../rspec_matchers'
2627

2728
include Selenium # rubocop:disable Style/MixinUsage
2829

rb/spec/rspec_matchers.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
RSpec::Matchers.define :have_deprecated do |deprecation|
21+
match do |actual|
22+
# Not sure how else to capture stdout here
23+
expect {
24+
actual.call
25+
std_out = File.read $stdout if $stdout.is_a?(File)
26+
@deprecations_found = std_out&.scan(/DEPRECATION\] \[:([^\]]*)\]/)&.flatten&.map(&:to_sym)
27+
}.to output.to_stdout_from_any_process
28+
expect(Array(deprecation).sort).to eq(@deprecations_found&.sort)
29+
end
30+
31+
failure_message do
32+
but_message = if @deprecations_found.empty? || @deprecations_found.nil?
33+
'no deprecations were found'
34+
else
35+
"instead these deprecations were found: [#{@deprecations_found.join(', ')}]"
36+
end
37+
"expected :#{deprecation} to have been deprecated, but #{but_message}"
38+
end
39+
40+
failure_message_when_negated do
41+
but_message = "it was found among these deprecations: [#{@deprecations_found.join(', ')}]"
42+
"expected :#{deprecation} not to have been deprecated, but #{but_message}"
43+
end
44+
45+
def supports_block_expectations?
46+
true
47+
end
48+
end

rb/spec/unit/selenium/webdriver/chrome/service_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ module WebDriver
7373

7474
expect {
7575
Selenium::WebDriver::Chrome.driver_path = path
76-
}.to output(/WARN Selenium \[DEPRECATION\] Selenium::WebDriver::Chrome#driver_path=/).to_stdout_from_any_process
76+
}.to have_deprecated(:driver_path)
7777

7878
expect {
7979
expect(Selenium::WebDriver::Chrome.driver_path).to eq path
80-
}.to output(/WARN Selenium \[DEPRECATION\] Selenium::WebDriver::Chrome#driver_path/).to_stdout_from_any_process
80+
}.to have_deprecated(:driver_path)
8181

8282
service = Service.chrome
8383

@@ -139,7 +139,7 @@ module WebDriver
139139

140140
expect {
141141
driver.new(driver_path: driver_path)
142-
}.to output(/WARN Selenium \[DEPRECATION\] :driver_path/).to_stdout_from_any_process
142+
}.to have_deprecated(:service_driver_path)
143143
end
144144

145145
it 'accepts :port but throws deprecation notice' do
@@ -151,7 +151,7 @@ module WebDriver
151151

152152
expect {
153153
driver.new(port: driver_port)
154-
}.to output(/WARN Selenium \[DEPRECATION\] :port/).to_stdout_from_any_process
154+
}.to have_deprecated(:service_port)
155155
end
156156

157157
it 'accepts :driver_opts but throws deprecation notice' do
@@ -164,7 +164,7 @@ module WebDriver
164164

165165
expect {
166166
driver.new(driver_opts: driver_opts)
167-
}.to output(/WARN Selenium \[DEPRECATION\] :driver_opts/).to_stdout_from_any_process
167+
}.to have_deprecated(:service_driver_opts)
168168
end
169169

170170
it 'accepts :service without creating a new instance' do

rb/spec/unit/selenium/webdriver/common/logger_spec.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ module WebDriver
6666

6767
describe '#output' do
6868
it 'outputs to stdout by default' do
69-
expect { logger.warn('message') }.to output(/WARN Selenium message/).to_stdout
69+
expect { logger.warn('message') }.to output(/WARN Selenium message/).to_stdout_from_any_process
7070
end
7171

7272
it 'allows output to file' do
@@ -83,34 +83,44 @@ module WebDriver
8383

8484
describe '#warn' do
8585
it 'logs with String' do
86-
expect { logger.warn "String Value" }.to output(/WARN Selenium String Value/).to_stdout
86+
expect { logger.warn "String Value" }.to output(/WARN Selenium String Value/).to_stdout_from_any_process
8787
end
8888

8989
it 'logs single id when set' do
9090
msg = /WARN Selenium \[:foo\] warning message/
91-
expect { logger.warn('warning message', id: :foo) }.to output(msg).to_stdout
91+
expect { logger.warn('warning message', id: :foo) }.to output(msg).to_stdout_from_any_process
9292
end
9393

9494
it 'logs multiple ids when set' do
9595
msg = /WARN Selenium \[:foo, :bar\] warning message/
96-
expect { logger.warn('warning message', id: %i[foo bar]) }.to output(msg).to_stdout
96+
expect { logger.warn('warning message', id: %i[foo bar]) }.to output(msg).to_stdout_from_any_process
9797
end
9898
end
9999

100100
describe '#deprecate' do
101101
it 'allows to deprecate functionality with replacement' do
102102
message = /WARN Selenium \[DEPRECATION\] #old is deprecated\. Use #new instead\./
103-
expect { logger.deprecate('#old', '#new') }.to output(message).to_stdout
103+
expect { logger.deprecate('#old', '#new') }.to output(message).to_stdout_from_any_process
104104
end
105105

106106
it 'allows to deprecate functionality without replacement' do
107107
message = /WARN Selenium \[DEPRECATION\] #old is deprecated and will be removed in a future release\./
108-
expect { logger.deprecate('#old') }.to output(message).to_stdout
108+
expect { logger.deprecate('#old') }.to output(message).to_stdout_from_any_process
109109
end
110110

111111
it 'appends deprecation message with provided block' do
112112
message = /WARN Selenium \[DEPRECATION\] #old is deprecated\. Use #new instead\. More Details\./
113-
expect { logger.deprecate('#old', '#new') { 'More Details.' } }.to output(message).to_stdout
113+
expect { logger.deprecate('#old', '#new') { 'More Details.' } }.to output(message).to_stdout_from_any_process
114+
end
115+
116+
it 'logs single id when set' do
117+
msg = /WARN Selenium \[:foo\] warning message/
118+
expect { logger.warn('warning message', id: :foo) }.to output(msg).to_stdout_from_any_process
119+
end
120+
121+
it 'logs multiple ids when set' do
122+
msg = /WARN Selenium \[:foo, :bar\] warning message/
123+
expect { logger.warn('warning message', id: %i[foo bar]) }.to output(msg).to_stdout_from_any_process
114124
end
115125
end
116126

rb/spec/unit/selenium/webdriver/edge/service_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ module WebDriver
7272
path = '/path/to/driver'
7373
expect {
7474
Selenium::WebDriver::Edge.driver_path = path
75-
}.to output(/WARN Selenium \[DEPRECATION\] Selenium::WebDriver::Edge#driver_path=/).to_stdout_from_any_process
75+
}.to have_deprecated(:driver_path)
7676

7777
expect {
7878
expect(Selenium::WebDriver::Edge.driver_path).to eq path
79-
}.to output(/WARN Selenium \[DEPRECATION\] Selenium::WebDriver::Edge#driver_path/).to_stdout_from_any_process
79+
}.to have_deprecated(:driver_path)
8080

8181
service = Service.edge
8282

@@ -140,7 +140,7 @@ module WebDriver
140140

141141
expect {
142142
driver.new(driver_path: driver_path)
143-
}.to output(/WARN Selenium \[DEPRECATION\] :driver_path/).to_stdout_from_any_process
143+
}.to have_deprecated(:service_driver_path)
144144
end
145145

146146
it 'accepts :port but throws deprecation notice' do
@@ -152,7 +152,7 @@ module WebDriver
152152

153153
expect {
154154
driver.new(port: driver_port)
155-
}.to output(/WARN Selenium \[DEPRECATION\] :port/).to_stdout_from_any_process
155+
}.to have_deprecated(:service_port)
156156
end
157157

158158
it 'accepts :driver_opts but throws deprecation notice' do
@@ -165,7 +165,7 @@ module WebDriver
165165

166166
expect {
167167
driver.new(driver_opts: driver_opts)
168-
}.to output(/WARN Selenium \[DEPRECATION\] :driver_opts/).to_stdout_from_any_process
168+
}.to have_deprecated(:service_driver_opts)
169169
end
170170

171171
it 'accepts :service without creating a new instance' do

rb/spec/unit/selenium/webdriver/firefox/service_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ module WebDriver
7373

7474
expect {
7575
Selenium::WebDriver::Firefox.driver_path = path
76-
}.to output(/WARN Selenium \[DEPRECATION\] Selenium::WebDriver::Firefox#driver_path=/).to_stdout_from_any_process
76+
}.to have_deprecated(:driver_path)
7777

7878
expect {
7979
expect(Selenium::WebDriver::Firefox.driver_path).to eq path
80-
}.to output(/WARN Selenium \[DEPRECATION\] Selenium::WebDriver::Firefox#driver_path/).to_stdout_from_any_process
80+
}.to have_deprecated(:driver_path)
8181

8282
service = Service.firefox
8383

@@ -141,7 +141,7 @@ module WebDriver
141141

142142
expect {
143143
driver.new(driver_path: driver_path)
144-
}.to output(/WARN Selenium \[DEPRECATION\] :driver_path/).to_stdout_from_any_process
144+
}.to have_deprecated(:service_driver_path)
145145
end
146146

147147
it 'accepts :port but throws deprecation notice' do
@@ -153,7 +153,7 @@ module WebDriver
153153

154154
expect {
155155
driver.new(port: driver_port)
156-
}.to output(/WARN Selenium \[DEPRECATION\] :port/).to_stdout_from_any_process
156+
}.to have_deprecated(:service_port)
157157
end
158158

159159
it 'accepts :driver_opts but throws deprecation notice' do
@@ -166,7 +166,7 @@ module WebDriver
166166

167167
expect {
168168
driver.new(driver_opts: driver_opts)
169-
}.to output(/WARN Selenium \[DEPRECATION\] :driver_opts/).to_stdout_from_any_process
169+
}.to have_deprecated(:service_driver_opts)
170170
end
171171

172172
it 'accepts :service without creating a new instance' do

0 commit comments

Comments
 (0)