Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/bidi/log_inspector_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 { Browser } = require('selenium-webdriver')
22
const { Pages, suite } = require('../../lib/test')
23
const logInspector = require('selenium-webdriver/bidi/logInspector')
24
const filterBy = require('selenium-webdriver/bidi/filterBy')
25
26
suite(
27
function (env) {
28
let driver
29
30
beforeEach(async function () {
31
driver = await env.builder().build()
32
})
33
34
afterEach(async function () {
35
await driver.quit()
36
})
37
38
function delay(ms) {
39
return new Promise((resolve) => setTimeout(resolve, ms))
40
}
41
42
describe('Log Inspector', function () {
43
it('can listen to console log', async function () {
44
const inspector = await logInspector(driver)
45
await inspector.onConsoleEntry(function (log) {
46
assert.equal(log.text, 'Hello, world!')
47
assert.notEqual(log.source.realmId, null)
48
assert.notEqual(log.source.browsingContextId, null)
49
assert.equal(log.type, 'console')
50
assert.equal(log.level, 'info')
51
assert.equal(log.method, 'log')
52
assert.equal(log.args.length, 1)
53
})
54
55
await driver.get(Pages.logEntryAdded)
56
await driver.findElement({ id: 'consoleLog' }).click()
57
58
await inspector.close()
59
})
60
61
it('can listen to console log with different consumers', async function () {
62
let logEntry = null
63
const inspector = await logInspector(driver)
64
await inspector.onConsoleEntry(function (log) {
65
logEntry = log
66
assert.equal(logEntry.text, 'Hello, world!')
67
assert.notEqual(log.source.realmId, null)
68
assert.notEqual(log.source.browsingContextId, null)
69
assert.equal(logEntry.type, 'console')
70
assert.equal(logEntry.level, 'info')
71
assert.equal(logEntry.method, 'log')
72
assert.equal(logEntry.args.length, 1)
73
})
74
75
let logEntryText = null
76
await inspector.onConsoleEntry(function (log) {
77
logEntryText = log.text
78
assert.equal(logEntryText, 'Hello, world!')
79
})
80
81
await driver.get(Pages.logEntryAdded)
82
await driver.findElement({ id: 'consoleLog' }).click()
83
84
await inspector.close()
85
})
86
87
it('can filter console info level log', async function () {
88
let logEntry = null
89
const inspector = await logInspector(driver)
90
await inspector.onConsoleEntry(function (log) {
91
logEntry = log
92
assert.equal(logEntry.text, 'Hello, world!')
93
assert.notEqual(log.source.realmId, null)
94
assert.notEqual(log.source.browsingContextId, null)
95
assert.equal(logEntry.type, 'console')
96
assert.equal(logEntry.level, 'info')
97
assert.equal(logEntry.method, 'log')
98
assert.equal(logEntry.args.length, 1)
99
}, filterBy.FilterBy.logLevel('info'))
100
101
await driver.get(Pages.logEntryAdded)
102
await driver.findElement({ id: 'consoleLog' }).click()
103
104
await inspector.close()
105
})
106
107
it('can filter console log', async function () {
108
const inspector = await logInspector(driver)
109
await inspector.onConsoleEntry(function (log) {
110
assert.notEqual(log, null)
111
}, filterBy.FilterBy.logLevel('info'))
112
113
await driver.get(Pages.logEntryAdded)
114
await driver.findElement({ id: 'consoleLog' }).click()
115
116
await inspector.close()
117
})
118
119
it('can call multiple callbacks for console log', async function () {
120
let logEntry1 = null
121
let logEntry2 = null
122
let logEntry3 = null
123
let logEntry4 = null
124
const inspector = await logInspector(driver)
125
await inspector.onConsoleEntry(function (log) {
126
logEntry1 = log
127
})
128
129
await inspector.onConsoleEntry(function (log) {
130
logEntry2 = log
131
})
132
133
await inspector.onConsoleEntry(function (log) {
134
logEntry3 = log
135
}, filterBy.FilterBy.logLevel('info'))
136
137
await inspector.onConsoleEntry(function (log) {
138
logEntry4 = log
139
}, filterBy.FilterBy.logLevel('error'))
140
141
await driver.get(Pages.logEntryAdded)
142
await driver.findElement({ id: 'consoleLog' }).click()
143
144
await delay(3000)
145
146
assert.notEqual(logEntry1, null)
147
assert.notEqual(logEntry2, null)
148
assert.notEqual(logEntry3, null)
149
assert.equal(logEntry4, null)
150
151
await inspector.close()
152
})
153
154
it('can listen to javascript log', async function () {
155
let logEntry = null
156
const inspector = await logInspector(driver)
157
await inspector.onJavascriptLog(function (log) {
158
logEntry = log
159
assert.equal(logEntry.text, 'Error: Not working')
160
assert.equal(logEntry.type, 'javascript')
161
assert.equal(logEntry.level, 'error')
162
})
163
164
await driver.get(Pages.logEntryAdded)
165
await driver.findElement({ id: 'jsException' }).click()
166
167
await inspector.close()
168
})
169
170
it('can filter javascript log at error level', async function () {
171
let logEntry = null
172
const inspector = await logInspector(driver)
173
await inspector.onJavascriptLog(function (log) {
174
logEntry = log
175
assert.equal(logEntry.text, 'Error: Not working')
176
assert.equal(logEntry.type, 'javascript')
177
assert.equal(logEntry.level, 'error')
178
}, filterBy.FilterBy.logLevel('error'))
179
180
await driver.get(Pages.logEntryAdded)
181
await driver.findElement({ id: 'jsException' }).click()
182
183
await inspector.close()
184
})
185
186
it('can filter javascript log', async function () {
187
let logEntry = null
188
const inspector = await logInspector(driver)
189
await inspector.onJavascriptLog(function (log) {
190
logEntry = log
191
assert.notEqual(logEntry, null)
192
}, filterBy.FilterBy.logLevel('error'))
193
194
await driver.get(Pages.logEntryAdded)
195
await driver.findElement({ id: 'jsException' }).click()
196
197
await inspector.close()
198
})
199
200
it('can call multiple callbacks for javascript log', async function () {
201
let logEntry1 = null
202
let logEntry2 = null
203
let logEntry3 = null
204
let logEntry4 = null
205
const inspector = await logInspector(driver)
206
await inspector.onJavascriptLog(function (log) {
207
logEntry1 = log
208
})
209
210
await inspector.onJavascriptLog(function (log) {
211
logEntry2 = log
212
})
213
214
await inspector.onJavascriptLog(function (log) {
215
logEntry3 = log
216
}, filterBy.FilterBy.logLevel('error'))
217
218
await inspector.onJavascriptLog(function (log) {
219
logEntry4 = log
220
}, filterBy.FilterBy.logLevel('info'))
221
222
await driver.get(Pages.logEntryAdded)
223
await driver.findElement({ id: 'jsException' }).click()
224
225
await delay(3000)
226
227
assert.notEqual(logEntry1, null)
228
assert.notEqual(logEntry2, null)
229
assert.notEqual(logEntry3, null)
230
assert.equal(logEntry4, null)
231
232
await inspector.close()
233
})
234
235
it('can listen to javascript error log', async function () {
236
let logEntry = null
237
const inspector = await logInspector(driver)
238
await inspector.onJavascriptException(function (log) {
239
logEntry = log
240
assert.equal(logEntry.text, 'Error: Not working')
241
assert.notEqual(log.source.realmId, null)
242
assert.notEqual(log.source.browsingContextId, null)
243
assert.equal(logEntry.type, 'javascript')
244
assert.equal(logEntry.level, 'error')
245
})
246
247
await driver.get(Pages.logEntryAdded)
248
await driver.findElement({ id: 'jsException' }).click()
249
250
await inspector.close()
251
})
252
253
it('can retrieve stack trace for a log', async function () {
254
let logEntry = null
255
const inspector = await logInspector(driver)
256
await inspector.onJavascriptException(function (log) {
257
logEntry = log
258
const stackTrace = logEntry.stackTrace
259
assert.notEqual(stackTrace, null)
260
assert.equal(stackTrace.callFrames.length > 0, true)
261
})
262
263
await driver.get(Pages.logEntryAdded)
264
await driver.findElement({ id: 'jsException' }).click()
265
266
await inspector.close()
267
})
268
269
it('can listen to any log', async function () {
270
let logEntry = null
271
const inspector = await logInspector(driver)
272
await inspector.onLog(function (log) {
273
logEntry = log
274
assert.equal(logEntry.text, 'Hello, world!')
275
assert.notEqual(log.source.realmId, null)
276
assert.notEqual(log.source.browsingContextId, null)
277
assert.equal(logEntry.type, 'console')
278
assert.equal(logEntry.level, 'info')
279
assert.equal(logEntry.method, 'log')
280
assert.equal(logEntry.args.length, 1)
281
})
282
283
await driver.get(Pages.logEntryAdded)
284
await driver.findElement({ id: 'consoleLog' }).click()
285
286
await inspector.close()
287
})
288
289
it('can filter any log', async function () {
290
let logEntry = null
291
const inspector = await logInspector(driver)
292
await inspector.onLog(function (log) {
293
logEntry = log
294
assert.equal(logEntry.text, 'Hello, world!')
295
assert.notEqual(log.source.realmId, null)
296
assert.notEqual(log.source.browsingContextId, null)
297
assert.equal(logEntry.type, 'console')
298
assert.equal(logEntry.level, 'info')
299
assert.equal(logEntry.method, 'log')
300
assert.equal(logEntry.args.length, 1)
301
}, filterBy.FilterBy.logLevel('info'))
302
303
await driver.get(Pages.logEntryAdded)
304
await driver.findElement({ id: 'consoleLog' }).click()
305
306
await inspector.close()
307
})
308
309
it('can filter any log at error level', async function () {
310
let logEntry = null
311
const inspector = await logInspector(driver)
312
await inspector.onLog(function (log) {
313
logEntry = log
314
assert.equal(logEntry.text, 'Error: Not working')
315
assert.equal(logEntry.type, 'javascript')
316
assert.equal(logEntry.level, 'error')
317
}, filterBy.FilterBy.logLevel('error'))
318
319
await driver.get(Pages.logEntryAdded)
320
await driver.findElement({ id: 'jsException' }).click()
321
322
await inspector.close()
323
})
324
325
it('can call multiple callbacks for any log', async function () {
326
let logEntry1 = null
327
let logEntry2 = null
328
let logEntry3 = null
329
let logEntry4 = null
330
const inspector = await logInspector(driver)
331
await inspector.onLog(function (log) {
332
logEntry1 = log
333
})
334
335
await inspector.onLog(function (log) {
336
logEntry2 = log
337
})
338
339
await inspector.onLog(function (log) {
340
logEntry3 = log
341
}, filterBy.FilterBy.logLevel('error'))
342
343
await inspector.onLog(function (log) {
344
logEntry4 = log
345
}, filterBy.FilterBy.logLevel('info'))
346
347
await driver.get(Pages.logEntryAdded)
348
await driver.findElement({ id: 'jsException' }).click()
349
350
await delay(3000)
351
352
assert.notEqual(logEntry1, null)
353
assert.notEqual(logEntry2, null)
354
assert.notEqual(logEntry3, null)
355
assert.equal(logEntry4, null)
356
357
await inspector.close()
358
})
359
360
it('can call multiple callbacks for any log with filter', async function () {
361
let logEntry1 = null
362
let logEntry2 = null
363
const inspector = await logInspector(driver)
364
await inspector.onLog(function (log) {
365
logEntry1 = log
366
}, filterBy.FilterBy.logLevel('error'))
367
368
await inspector.onLog(function (log) {
369
logEntry2 = log
370
}, filterBy.FilterBy.logLevel('error'))
371
372
await driver.get(Pages.logEntryAdded)
373
await driver.findElement({ id: 'jsException' }).click()
374
375
await delay(3000)
376
377
assert.notEqual(logEntry1, null)
378
assert.notEqual(logEntry2, null)
379
380
await inspector.close()
381
})
382
})
383
},
384
{ browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] },
385
)
386
387