Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/lib/logging_test.js
2885 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
'use strict'
19
20
const assert = require('node:assert')
21
const sinon = require('sinon')
22
const logging = require('selenium-webdriver/lib/logging')
23
24
describe('logging', function () {
25
let mgr, root, clock
26
27
beforeEach(function setUp() {
28
mgr = new logging.LogManager()
29
root = mgr.getLogger('')
30
31
clock = sinon.useFakeTimers()
32
})
33
34
afterEach(function tearDown() {
35
clock.restore()
36
})
37
38
describe('LogManager', function () {
39
describe('getLogger()', function () {
40
it('handles falsey input', function () {
41
assert.strictEqual(root, mgr.getLogger())
42
assert.strictEqual(root, mgr.getLogger(''))
43
assert.strictEqual(root, mgr.getLogger(null))
44
assert.strictEqual(root, mgr.getLogger(0))
45
})
46
47
it('creates parent loggers', function () {
48
let logger = mgr.getLogger('foo.bar.baz')
49
assert.strictEqual(logger.parent_, mgr.getLogger('foo.bar'))
50
51
logger = logger.parent_
52
assert.strictEqual(logger.parent_, mgr.getLogger('foo'))
53
54
logger = logger.parent_
55
assert.strictEqual(logger.parent_, mgr.getLogger(''))
56
57
assert.strictEqual(logger.parent_.parent_, null)
58
})
59
})
60
})
61
62
describe('Logger', function () {
63
describe('getEffectiveLevel()', function () {
64
it('defaults to OFF', function () {
65
assert.strictEqual(root.getLevel(), logging.Level.OFF)
66
assert.strictEqual(root.getEffectiveLevel(), logging.Level.OFF)
67
68
root.setLevel(null)
69
assert.strictEqual(root.getLevel(), null)
70
assert.strictEqual(root.getEffectiveLevel(), logging.Level.OFF)
71
})
72
73
it('uses own level if set', function () {
74
let logger = mgr.getLogger('foo.bar.baz')
75
assert.strictEqual(logger.getLevel(), null)
76
assert.strictEqual(logger.getEffectiveLevel(), logging.Level.OFF)
77
78
logger.setLevel(logging.Level.INFO)
79
assert.strictEqual(logger.getLevel(), logging.Level.INFO)
80
assert.strictEqual(logger.getEffectiveLevel(), logging.Level.INFO)
81
})
82
83
it('uses level from set on nearest parent', function () {
84
let ancestor = mgr.getLogger('foo')
85
ancestor.setLevel(logging.Level.SEVERE)
86
87
let logger = mgr.getLogger('foo.bar.baz')
88
assert.strictEqual(logger.getLevel(), null)
89
assert.strictEqual(logger.getEffectiveLevel(), logging.Level.SEVERE)
90
})
91
})
92
93
describe('isLoggable()', function () {
94
it("compares level against logger's effective level", function () {
95
const log1 = mgr.getLogger('foo')
96
log1.setLevel(logging.Level.WARNING)
97
98
const log2 = mgr.getLogger('foo.bar.baz')
99
100
assert(!log2.isLoggable(logging.Level.FINEST))
101
assert(!log2.isLoggable(logging.Level.INFO))
102
assert(log2.isLoggable(logging.Level.WARNING))
103
assert(log2.isLoggable(logging.Level.SEVERE))
104
105
log2.setLevel(logging.Level.INFO)
106
107
assert(!log2.isLoggable(logging.Level.FINEST))
108
assert(log2.isLoggable(logging.Level.INFO))
109
assert(log2.isLoggable(logging.Level.WARNING))
110
assert(log2.isLoggable(logging.Level.SEVERE))
111
112
log2.setLevel(logging.Level.ALL)
113
114
assert(log2.isLoggable(logging.Level.FINEST))
115
assert(log2.isLoggable(logging.Level.INFO))
116
assert(log2.isLoggable(logging.Level.WARNING))
117
assert(log2.isLoggable(logging.Level.SEVERE))
118
})
119
120
it('Level.OFF is never loggable', function () {
121
function test(level) {
122
root.setLevel(level)
123
assert(!root.isLoggable(logging.Level.OFF), 'OFF should not be loggable at ' + level)
124
}
125
126
test(logging.Level.ALL)
127
test(logging.Level.INFO)
128
test(logging.Level.OFF)
129
})
130
})
131
132
describe('log()', function () {
133
it('does not invoke loggable if message is not loggable', function () {
134
const log = mgr.getLogger('foo')
135
log.setLevel(logging.Level.OFF)
136
137
let callback = sinon.spy()
138
log.addHandler(callback)
139
root.addHandler(callback)
140
141
assert(!callback.called)
142
})
143
144
it('invokes handlers for each parent logger', function () {
145
const cb1 = sinon.spy()
146
const cb2 = sinon.spy()
147
const cb3 = sinon.spy()
148
149
const log1 = mgr.getLogger('foo')
150
const log2 = mgr.getLogger('foo.bar')
151
const log3 = mgr.getLogger('foo.bar.baz')
152
const log4 = mgr.getLogger('foo.bar.baz.quot')
153
154
log1.addHandler(cb1)
155
log1.setLevel(logging.Level.INFO)
156
157
log2.addHandler(cb2)
158
log2.setLevel(logging.Level.WARNING)
159
160
log3.addHandler(cb3)
161
log3.setLevel(logging.Level.FINER)
162
163
clock.tick(123456)
164
165
log4.finest('this is the finest message')
166
log4.finer('this is a finer message')
167
log4.info('this is an info message')
168
log4.warning('this is a warning message')
169
log4.severe('this is a severe message')
170
171
assert.strictEqual(4, cb1.callCount)
172
assert.strictEqual(4, cb2.callCount)
173
assert.strictEqual(4, cb3.callCount)
174
175
const entry1 = new logging.Entry(logging.Level.FINER, '[foo.bar.baz.quot] this is a finer message', 123456)
176
const entry2 = new logging.Entry(logging.Level.INFO, '[foo.bar.baz.quot] this is an info message', 123456)
177
const entry3 = new logging.Entry(logging.Level.WARNING, '[foo.bar.baz.quot] this is a warning message', 123456)
178
const entry4 = new logging.Entry(logging.Level.SEVERE, '[foo.bar.baz.quot] this is a severe message', 123456)
179
180
check(cb1.getCall(0).args[0], entry1)
181
check(cb1.getCall(1).args[0], entry2)
182
check(cb1.getCall(2).args[0], entry3)
183
check(cb1.getCall(3).args[0], entry4)
184
185
check(cb2.getCall(0).args[0], entry1)
186
check(cb2.getCall(1).args[0], entry2)
187
check(cb2.getCall(2).args[0], entry3)
188
check(cb2.getCall(3).args[0], entry4)
189
190
check(cb3.getCall(0).args[0], entry1)
191
check(cb3.getCall(1).args[0], entry2)
192
check(cb3.getCall(2).args[0], entry3)
193
check(cb3.getCall(3).args[0], entry4)
194
195
function check(entry, expected) {
196
assert.strictEqual(entry.level, expected.level, 'wrong level')
197
assert.strictEqual(entry.message, expected.message, 'wrong message')
198
assert.strictEqual(entry.timestamp, expected.timestamp, 'wrong time')
199
}
200
})
201
202
it('does not invoke removed handler', function () {
203
root.setLevel(logging.Level.INFO)
204
const cb = sinon.spy()
205
206
root.addHandler(cb)
207
root.info('hi')
208
assert.strictEqual(1, cb.callCount)
209
210
assert(root.removeHandler(cb))
211
root.info('bye')
212
assert.strictEqual(1, cb.callCount)
213
214
assert(!root.removeHandler(cb))
215
})
216
})
217
})
218
219
describe('getLevel()', function () {
220
it('converts named levels', function () {
221
assert.strictEqual(logging.Level.DEBUG, logging.getLevel('DEBUG'))
222
assert.strictEqual(logging.Level.ALL, logging.getLevel('FAKE'))
223
})
224
225
it('converts numeric levels', function () {
226
assert.strictEqual(logging.Level.DEBUG, logging.getLevel(logging.Level.DEBUG.value))
227
})
228
229
it('normalizes numeric levels', function () {
230
assert.strictEqual(logging.Level.OFF, logging.getLevel(logging.Level.OFF.value * 2))
231
232
let diff = logging.Level.SEVERE.value - logging.Level.WARNING.value
233
assert.strictEqual(logging.Level.WARNING, logging.getLevel(logging.Level.WARNING.value + diff * 0.5))
234
235
assert.strictEqual(logging.Level.ALL, logging.getLevel(0))
236
assert.strictEqual(logging.Level.ALL, logging.getLevel(-1))
237
})
238
})
239
240
describe('Preferences', function () {
241
it('can be converted to JSON', function () {
242
let prefs = new logging.Preferences()
243
assert.strictEqual('{}', JSON.stringify(prefs))
244
245
prefs.setLevel('foo', logging.Level.DEBUG)
246
assert.strictEqual('{"foo":"DEBUG"}', JSON.stringify(prefs))
247
248
prefs.setLevel(logging.Type.BROWSER, logging.Level.FINE)
249
assert.strictEqual('{"foo":"DEBUG","browser":"FINE"}', JSON.stringify(prefs))
250
})
251
})
252
})
253
254