Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/logging_test.js
2884 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 test = require('../lib/test')
22
const { Browser, logging } = require('selenium-webdriver')
23
24
test.suite(function (env) {
25
// Logging API is not supported in IE.
26
// Logging API not supported in Marionette.
27
// Logging API not supported in Safari.
28
test
29
.ignore(env.browsers(Browser.INTERNET_EXPLORER, Browser.SAFARI, Browser.FIREFOX, Browser.CHROME))
30
.describe('logging', function () {
31
var driver
32
33
beforeEach(function () {
34
driver = null
35
})
36
37
afterEach(async function () {
38
if (driver) {
39
return driver.quit()
40
}
41
})
42
43
it('can be disabled', async function () {
44
var prefs = new logging.Preferences()
45
prefs.setLevel(logging.Type.BROWSER, logging.Level.OFF)
46
47
driver = await env.builder().setLoggingPrefs(prefs).build()
48
49
await driver.get(
50
dataUrl(
51
'<!DOCTYPE html><script>',
52
'console.info("hello");',
53
'console.warn("this is a warning");',
54
'console.error("and this is an error");',
55
'</script>',
56
),
57
)
58
return driver
59
.manage()
60
.logs()
61
.get(logging.Type.BROWSER)
62
.then((entries) => assert.strictEqual(entries.length, 0))
63
})
64
65
// Firefox does not capture JS error console log messages.
66
test.ignore(env.browsers(Browser.FIREFOX, 'legacy-firefox')).it('can be turned down', async function () {
67
var prefs = new logging.Preferences()
68
prefs.setLevel(logging.Type.BROWSER, logging.Level.SEVERE)
69
70
driver = await env.builder().setLoggingPrefs(prefs).build()
71
72
await driver.get(
73
dataUrl(
74
'<!DOCTYPE html><script>',
75
'console.info("hello");',
76
'console.warn("this is a warning");',
77
'console.error("and this is an error");',
78
'</script>',
79
),
80
)
81
return driver
82
.manage()
83
.logs()
84
.get(logging.Type.BROWSER)
85
.then(function (entries) {
86
assert.strictEqual(entries.length, 1)
87
assert.strictEqual(entries[0].level.name, 'SEVERE')
88
// eslint-disable-next-line no-useless-escape
89
assert.ok(/.*\"?and this is an error\"?/.test(entries[0].message))
90
})
91
})
92
93
// Firefox does not capture JS error console log messages.
94
test.ignore(env.browsers(Browser.FIREFOX, 'legacy-firefox')).it('can be made verbose', async function () {
95
var prefs = new logging.Preferences()
96
prefs.setLevel(logging.Type.BROWSER, logging.Level.DEBUG)
97
98
driver = await env.builder().setLoggingPrefs(prefs).build()
99
100
await driver.get(
101
dataUrl(
102
'<!DOCTYPE html><script>',
103
'console.debug("hello");',
104
'console.warn("this is a warning");',
105
'console.error("and this is an error");',
106
'</script>',
107
),
108
)
109
return driver
110
.manage()
111
.logs()
112
.get(logging.Type.BROWSER)
113
.then(function (entries) {
114
assert.strictEqual(entries.length, 3)
115
assert.strictEqual(entries[0].level.name, 'DEBUG')
116
// eslint-disable-next-line no-useless-escape
117
assert.ok(/.*\"?hello\"?/.test(entries[0].message))
118
119
assert.strictEqual(entries[1].level.name, 'WARNING')
120
// eslint-disable-next-line no-useless-escape
121
assert.ok(/.*\"?this is a warning\"?/.test(entries[1].message))
122
123
assert.strictEqual(entries[2].level.name, 'SEVERE')
124
// eslint-disable-next-line no-useless-escape
125
assert.ok(/.*\"?and this is an error\"?/.test(entries[2].message))
126
})
127
})
128
129
// Firefox does not capture JS error console log messages.
130
test
131
.ignore(env.browsers(Browser.FIREFOX, 'legacy-firefox'))
132
.it('clears records after retrieval', async function () {
133
var prefs = new logging.Preferences()
134
prefs.setLevel(logging.Type.BROWSER, logging.Level.DEBUG)
135
136
driver = await env.builder().setLoggingPrefs(prefs).build()
137
138
await driver.get(
139
dataUrl(
140
'<!DOCTYPE html><script>',
141
'console.debug("hello");',
142
'console.warn("this is a warning");',
143
'console.error("and this is an error");',
144
'</script>',
145
),
146
)
147
await driver
148
.manage()
149
.logs()
150
.get(logging.Type.BROWSER)
151
.then((entries) => assert.strictEqual(entries.length, 3))
152
return driver
153
.manage()
154
.logs()
155
.get(logging.Type.BROWSER)
156
.then((entries) => assert.strictEqual(entries.length, 0))
157
})
158
159
it('does not mix log types', async function () {
160
var prefs = new logging.Preferences()
161
prefs.setLevel(logging.Type.BROWSER, logging.Level.DEBUG)
162
prefs.setLevel(logging.Type.DRIVER, logging.Level.SEVERE)
163
164
driver = await env.builder().setLoggingPrefs(prefs).build()
165
166
await driver.get(
167
dataUrl(
168
'<!DOCTYPE html><script>',
169
'console.debug("hello");',
170
'console.warn("this is a warning");',
171
'console.error("and this is an error");',
172
'</script>',
173
),
174
)
175
return driver
176
.manage()
177
.logs()
178
.get(logging.Type.DRIVER)
179
.then((entries) => assert.strictEqual(entries.length, 0))
180
})
181
})
182
183
function dataUrl(...args) {
184
return 'data:text/html,' + args.join('')
185
}
186
})
187
188