Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/unit/selenium/webdriver/common/logger_spec.rb
1865 views
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 File.expand_path('../spec_helper', __dir__)
21
22
module Selenium
23
module WebDriver
24
describe Logger do
25
subject(:logger) { described_class.new('Selenium', default_level: :info, ignored: [:logger_info]) }
26
27
around do |example|
28
debug = $DEBUG
29
$DEBUG = false
30
example.call
31
$DEBUG = debug
32
end
33
34
describe '#new' do
35
it 'allows creating a logger with a different progname' do
36
other_logger = described_class.new('NotSelenium')
37
msg = /WARN NotSelenium message/
38
expect { other_logger.warn('message') }.to output(msg).to_stderr_from_any_process
39
end
40
41
it 'does not log info from constructor' do
42
expect {
43
described_class.new('Selenium')
44
}.not_to output(/.+/).to_stdout_from_any_process
45
end
46
end
47
48
describe '#level' do
49
it 'logs at warn level by default' do
50
expect(logger.level).to eq(1)
51
expect(logger).to be_info
52
end
53
54
it 'logs at debug level if $DEBUG is set to true' do
55
$DEBUG = true
56
logger = described_class.new('Selenium')
57
$DEBUG = nil
58
expect(logger.level).to eq(0)
59
expect(logger).to be_debug
60
end
61
62
it 'allows changing level by name during execution' do
63
logger.level = :error
64
expect(logger.level).to eq(3)
65
expect(logger).to be_error
66
end
67
68
it 'allows changing level by integer during execution' do
69
logger.level = 1
70
expect(logger).to be_info
71
end
72
end
73
74
describe '#output' do
75
it 'outputs to stderr by default' do
76
expect { logger.warn('message') }.to output(/WARN Selenium message/).to_stderr_from_any_process
77
end
78
79
it 'allows output to file' do
80
logger.output = 'test.log'
81
logger.warn('message')
82
expect(File.read('test.log')).to include('WARN Selenium message')
83
ensure
84
logger.close
85
File.delete('test.log')
86
end
87
end
88
89
describe '#debug' do
90
before { logger.level = :debug }
91
92
it 'logs message' do
93
expect { logger.debug 'String Value' }.to output(/DEBUG Selenium String Value/).to_stderr_from_any_process
94
end
95
96
it 'logs single id when set' do
97
msg = /DEBUG Selenium \[:foo\] debug message/
98
expect { logger.debug('debug message', id: :foo) }.to output(msg).to_stderr_from_any_process
99
end
100
101
it 'logs multiple ids when set' do
102
msg = /DEBUG Selenium \[:foo, :bar\] debug message/
103
expect { logger.debug('debug message', id: %i[foo bar]) }.to output(msg).to_stderr_from_any_process
104
end
105
end
106
107
describe '#info' do
108
it 'logs info on first displayed logging only' do
109
logger = described_class.new('Selenium', default_level: :info)
110
111
logger.debug('first')
112
expect { logger.info('first') }.to output(/:logger_info/).to_stderr_from_any_process
113
expect { logger.info('second') }.not_to output(/:logger_info/).to_stderr_from_any_process
114
end
115
116
it 'logs message' do
117
expect { logger.info 'String Value' }.to output(/INFO Selenium String Value/).to_stderr_from_any_process
118
end
119
120
it 'logs single id when set' do
121
msg = /INFO Selenium \[:foo\] info message/
122
expect { logger.info('info message', id: :foo) }.to output(msg).to_stderr_from_any_process
123
end
124
125
it 'logs multiple ids when set' do
126
msg = /INFO Selenium \[:foo, :bar\] info message/
127
expect { logger.info('info message', id: %i[foo bar]) }.to output(msg).to_stderr_from_any_process
128
end
129
end
130
131
describe '#warn' do
132
it 'logs message' do
133
expect { logger.warn 'String Value' }.to output(/WARN Selenium String Value/).to_stderr_from_any_process
134
end
135
136
it 'logs single id when set' do
137
msg = /WARN Selenium \[:foo\] warning message/
138
expect { logger.warn('warning message', id: :foo) }.to output(msg).to_stderr_from_any_process
139
end
140
141
it 'logs multiple ids when set' do
142
msg = /WARN Selenium \[:foo, :bar\] warning message/
143
expect { logger.warn('warning message', id: %i[foo bar]) }.to output(msg).to_stderr_from_any_process
144
end
145
end
146
147
describe '#deprecate' do
148
it 'allows to deprecate functionality with replacement' do
149
message = /WARN Selenium \[DEPRECATION\] #old is deprecated\. Use #new instead\./
150
expect { logger.deprecate('#old', '#new') }.to output(message).to_stderr_from_any_process
151
end
152
153
it 'allows to deprecate functionality without replacement' do
154
message = /WARN Selenium \[DEPRECATION\] #old is deprecated and will be removed in a future release\./
155
expect { logger.deprecate('#old') }.to output(message).to_stderr_from_any_process
156
end
157
158
it 'allows to deprecate functionality with a reference message' do
159
ref_url = 'https://selenium.dev'
160
warn_msg = 'WARN Selenium \[DEPRECATION\] #old is deprecated\. Use #new instead\.'
161
message = /#{warn_msg} See explanation for this deprecation: #{ref_url}/
162
expect { logger.deprecate('#old', '#new', reference: ref_url) }.to output(message).to_stderr_from_any_process
163
end
164
165
it 'appends deprecation message with provided block' do
166
message = /WARN Selenium \[DEPRECATION\] #old is deprecated\. Use #new instead\. More Details\./
167
expect { logger.deprecate('#old', '#new') { 'More Details.' } }.to output(message).to_stderr_from_any_process
168
end
169
170
it 'logs single id when set' do
171
msg = /WARN Selenium \[:foo\] warning message/
172
expect { logger.warn('warning message', id: :foo) }.to output(msg).to_stderr_from_any_process
173
end
174
175
it 'logs multiple ids when set' do
176
msg = /WARN Selenium \[:foo, :bar\] warning message/
177
expect { logger.warn('warning message', id: %i[foo bar]) }.to output(msg).to_stderr_from_any_process
178
end
179
end
180
181
describe '#ignore' do
182
it 'prevents logging when id' do
183
logger.ignore(:foo)
184
expect { logger.deprecate('#old', '#new', id: :foo) }.not_to output.to_stderr_from_any_process
185
end
186
187
it 'prevents logging when ignoring multiple ids' do
188
logger.ignore(:foo)
189
logger.ignore(:bar)
190
expect { logger.deprecate('#old', '#new', id: :foo) }.not_to output.to_stderr_from_any_process
191
expect { logger.deprecate('#old', '#new', id: :bar) }.not_to output.to_stderr_from_any_process
192
end
193
194
it 'prevents logging when ignoring Array of ids' do
195
logger.ignore(%i[foo bar])
196
expect { logger.deprecate('#old', '#new', id: %i[foo foobar]) }.not_to output.to_stderr_from_any_process
197
end
198
199
it 'prevents logging any deprecation when ignoring :deprecations' do
200
logger.ignore(:deprecations)
201
expect { logger.deprecate('#old', '#new') }.not_to output.to_stderr_from_any_process
202
end
203
end
204
205
describe '#allow' do
206
it 'logs only allowed ids from method' do
207
logger.allow(:foo)
208
logger.allow(:bar)
209
expect { logger.deprecate('#old', '#new', id: :foo) }.to output(/foo/).to_stderr_from_any_process
210
expect { logger.deprecate('#old', '#new', id: :bar) }.to output(/bar/).to_stderr_from_any_process
211
expect { logger.deprecate('#old', '#new', id: :foobar) }.not_to output.to_stderr_from_any_process
212
end
213
214
it 'logs only allowed ids from Array' do
215
logger.allow(%i[foo bar])
216
expect { logger.deprecate('#old', '#new', id: :foo) }.to output(/foo/).to_stderr_from_any_process
217
expect { logger.deprecate('#old', '#new', id: :bar) }.to output(/bar/).to_stderr_from_any_process
218
expect { logger.deprecate('#old', '#new', id: :foobar) }.not_to output.to_stderr_from_any_process
219
end
220
221
it 'prevents logging any deprecation when ignoring :deprecations' do
222
logger.allow(:deprecations)
223
expect { logger.deprecate('#old', '#new') }.to output(/new/).to_stderr_from_any_process
224
end
225
end
226
end
227
end # WebDriver
228
end # Selenium
229
230