Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/bidi/generated/log_test.js
11822 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 { suite, ignore, Pages } = require('../../../lib/test')
22
const { Browser } = require('selenium-webdriver')
23
24
const { Log } = require('selenium-webdriver/bidi/generated/log')
25
26
suite(
27
function (env) {
28
let driver
29
let log
30
31
beforeEach(async function () {
32
driver = await env.builder().build()
33
log = await Log.create(driver)
34
})
35
36
afterEach(function () {
37
return driver.quit()
38
})
39
40
describe('onConsoleEntry', function () {
41
it('can listen to console.log', async function () {
42
let entry = null
43
44
await log.onConsoleEntry((params) => {
45
if (params.text === 'Hello, world!') {
46
entry = params
47
}
48
})
49
50
await driver.get(Pages.logEntryAdded)
51
await driver.findElement({ id: 'consoleLog' }).click()
52
await driver.wait(() => entry !== null, 5000)
53
54
assert.ok(entry, 'console log entry should have been captured')
55
assert.strictEqual(entry.type, 'console')
56
assert.strictEqual(entry.level, 'info')
57
assert.strictEqual(entry.method, 'log')
58
assert.strictEqual(entry.text, 'Hello, world!')
59
assert.ok(entry.source)
60
// Raw BiDi event params use source.realm (not source.realmId which is
61
// the hand-coded Source wrapper's getter)
62
assert.ok(entry.source.realm)
63
})
64
65
it('can listen to console.error', async function () {
66
let entry = null
67
68
await log.onConsoleEntry((params) => {
69
if (params.level === 'error' && params.text) {
70
entry = params
71
}
72
})
73
74
await driver.get(Pages.logEntryAdded)
75
await driver.findElement({ id: 'consoleError' }).click()
76
await driver.wait(() => entry !== null, 5000)
77
78
assert.ok(entry, 'console error entry should have been captured')
79
assert.strictEqual(entry.type, 'console')
80
assert.strictEqual(entry.level, 'error')
81
})
82
83
it('can listen to console.warn', async function () {
84
let entry = null
85
86
await log.onConsoleEntry((params) => {
87
if (params.level === 'warn' && params.text) {
88
entry = params
89
}
90
})
91
92
await driver.get(Pages.logEntryAdded)
93
// logEntryAdded.html has no warn button; trigger via script instead
94
await driver.executeScript('console.warn("test warn message")')
95
await driver.wait(() => entry !== null, 5000)
96
97
assert.ok(entry, 'console warn entry should have been captured')
98
assert.strictEqual(entry.level, 'warn')
99
})
100
101
it('captures args in console log entries', async function () {
102
let entry = null
103
104
await log.onConsoleEntry((params) => {
105
if (params.text === 'Hello, world!') {
106
entry = params
107
}
108
})
109
110
await driver.get(Pages.logEntryAdded)
111
await driver.findElement({ id: 'consoleLog' }).click()
112
await driver.wait(() => entry !== null, 5000)
113
114
assert.ok(entry)
115
assert.ok(Array.isArray(entry.args))
116
assert.ok(entry.args.length > 0)
117
})
118
})
119
120
describe('onJavascriptException', function () {
121
it('can listen to javascript exceptions', async function () {
122
let entry = null
123
124
await log.onJavascriptException((params) => {
125
entry = params
126
})
127
128
await driver.get(Pages.logEntryAdded)
129
await driver.findElement({ id: 'jsException' }).click()
130
await driver.wait(() => entry !== null, 5000)
131
132
assert.ok(entry, 'javascript exception entry should have been captured')
133
assert.strictEqual(entry.type, 'javascript')
134
assert.strictEqual(entry.level, 'error')
135
assert.ok(entry.text)
136
})
137
})
138
139
describe('onEntryAdded', function () {
140
it('receives all log entries including console', async function () {
141
const entries = []
142
143
await log.onEntryAdded((params) => {
144
entries.push(params)
145
})
146
147
await driver.get(Pages.logEntryAdded)
148
await driver.findElement({ id: 'consoleLog' }).click()
149
await driver.wait(() => entries.length > 0, 5000)
150
151
assert.ok(entries.length > 0, 'should have received at least one log entry')
152
})
153
154
it('can register multiple listeners', async function () {
155
let count1 = 0
156
let count2 = 0
157
158
await log.onConsoleEntry(() => {
159
count1++
160
})
161
await log.onConsoleEntry(() => {
162
count2++
163
})
164
165
await driver.get(Pages.logEntryAdded)
166
await driver.findElement({ id: 'consoleLog' }).click()
167
await driver.wait(() => count1 >= 1 && count2 >= 1, 5000)
168
169
assert.ok(count1 >= 1)
170
assert.ok(count2 >= 1)
171
})
172
})
173
174
describe('onJavascriptLog', function () {
175
it('can listen to javascript log entries', async function () {
176
const entries = []
177
178
await log.onJavascriptLog((params) => {
179
entries.push(params)
180
})
181
182
await driver.get(Pages.logEntryAdded)
183
184
// Trigger a JS error to produce a javascript log entry
185
await driver.findElement({ id: 'jsException' }).click()
186
await driver.wait(() => entries.length > 0, 5000)
187
188
assert.ok(entries.length > 0, 'onJavascriptLog should have received at least one entry')
189
assert.strictEqual(entries[0].type, 'javascript')
190
})
191
})
192
},
193
{ browsers: [Browser.CHROME, Browser.FIREFOX] },
194
)
195
196