Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/chrome/devtools_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 fs = require('node:fs')
22
const path = require('node:path')
23
24
const chrome = require('selenium-webdriver/chrome')
25
const by = require('selenium-webdriver/lib/by')
26
const error = require('selenium-webdriver/lib/error')
27
const fileServer = require('../../lib/test/fileserver')
28
const io = require('selenium-webdriver/io')
29
const test = require('../../lib/test')
30
const until = require('selenium-webdriver/lib/until')
31
32
test.suite(
33
function (env) {
34
let driver
35
36
beforeEach(async function () {
37
let options = env.builder().getChromeOptions() || new chrome.Options()
38
options.addArguments('--headless')
39
driver = await env.builder().setChromeOptions(options).build()
40
})
41
afterEach(async () => await driver.quit())
42
43
it('can send commands to devtools', async function () {
44
await driver.get(test.Pages.ajaxyPage)
45
assert.strictEqual(await driver.getCurrentUrl(), test.Pages.ajaxyPage)
46
47
await driver.sendDevToolsCommand('Page.navigate', {
48
url: test.Pages.echoPage,
49
})
50
assert.strictEqual(await driver.getCurrentUrl(), test.Pages.echoPage)
51
})
52
53
it('can send commands to devtools and get return', async function () {
54
await driver.get(test.Pages.ajaxyPage)
55
assert.strictEqual(await driver.getCurrentUrl(), test.Pages.ajaxyPage)
56
57
await driver.get(test.Pages.echoPage)
58
assert.strictEqual(await driver.getCurrentUrl(), test.Pages.echoPage)
59
60
let history = await driver.sendAndGetDevToolsCommand('Page.getNavigationHistory')
61
assert(history)
62
assert(history.currentIndex >= 2)
63
assert.strictEqual(history.entries[history.currentIndex].url, test.Pages.echoPage)
64
assert.strictEqual(history.entries[history.currentIndex - 1].url, test.Pages.ajaxyPage)
65
})
66
67
it('sends Page.enable command using devtools', async function () {
68
const cdpConnection = await driver.createCDPConnection('page')
69
cdpConnection.execute('Page.enable', {}, function (_res, err) {
70
assert(!err)
71
})
72
})
73
74
it('sends Network and Page command using devtools', async function () {
75
const cdpConnection = await driver.createCDPConnection('page')
76
cdpConnection.execute('Network.enable', {}, function (_res, err) {
77
assert(!err)
78
})
79
80
cdpConnection.execute('Page.navigate', { url: 'chrome://newtab/' }, function (_res, err) {
81
assert(!err)
82
})
83
})
84
85
describe('JS CDP events', function () {
86
it('calls the event listener for console.log', async function () {
87
const cdpConnection = await driver.createCDPConnection('page')
88
await driver.onLogEvent(cdpConnection, function (event) {
89
assert.strictEqual(event['args'][0]['value'], 'here')
90
})
91
await driver.executeScript('console.log("here")')
92
})
93
94
it('calls the event listener for js exceptions', async function () {
95
const cdpConnection = await driver.createCDPConnection('page')
96
await driver.onLogException(cdpConnection, function (event) {
97
assert.strictEqual(event['exceptionDetails']['stackTrace']['callFrames'][0]['functionName'], 'onmouseover')
98
})
99
await driver.get(test.Pages.javascriptPage)
100
let element = driver.findElement({ id: 'throwing-mouseover' })
101
await element.click()
102
})
103
})
104
105
describe('JS DOM events', function () {
106
it('calls the event listener on dom mutations', async function () {
107
const cdpConnection = await driver.createCDPConnection('page')
108
await driver.logMutationEvents(cdpConnection, function (event) {
109
assert.strictEqual(event['attribute_name'], 'style')
110
assert.strictEqual(event['current_value'], '')
111
assert.strictEqual(event['old_value'], 'display:none;')
112
})
113
114
await driver.get(fileServer.Pages.dynamicPage)
115
116
let element = driver.findElement({ id: 'reveal' })
117
await element.click()
118
let revealed = driver.findElement({ id: 'revealed' })
119
await driver.wait(until.elementIsVisible(revealed), 5000)
120
})
121
})
122
123
describe('Basic Auth Injection', function () {
124
it('denies entry if username and password do not match', async function () {
125
const pageCdpConnection = await driver.createCDPConnection('page')
126
127
await driver.register('random', 'random', pageCdpConnection)
128
await driver.get(fileServer.Pages.basicAuth)
129
let source = await driver.getPageSource()
130
console.log(source)
131
assert.strictEqual(source.includes('Access granted!'), false, source)
132
})
133
})
134
135
describe('Basic Auth Injection', function () {
136
it('grants access if username and password are a match', async function () {
137
const pageCdpConnection = await driver.createCDPConnection('page')
138
139
await driver.register('genie', 'bottle', pageCdpConnection)
140
await driver.get(fileServer.Pages.basicAuth)
141
let source = await driver.getPageSource()
142
assert.strictEqual(source.includes('Access granted!'), true)
143
})
144
})
145
146
describe('setDownloadPath', function () {
147
it('can enable downloads in headless mode', async function () {
148
const dir = await io.tmpDir()
149
await driver.setDownloadPath(dir)
150
151
const url = fileServer.whereIs('/data/chrome/download.bin')
152
await driver.get(`data:text/html,<!DOCTYPE html>
153
<div><a download="" href="${url}">Go!</a></div>`)
154
155
await driver.findElement({ css: 'a' }).click()
156
157
const downloadPath = path.join(dir, 'download.bin')
158
await driver.wait(() => io.exists(downloadPath), 5000)
159
160
const goldenPath = path.join(__dirname, '../../lib/test/data/chrome/download.bin')
161
assert.strictEqual(fs.readFileSync(downloadPath, 'binary'), fs.readFileSync(goldenPath, 'binary'))
162
})
163
164
it('throws if path is not a directory', async function () {
165
await assertInvalidArgumentError(() => driver.setDownloadPath())
166
await assertInvalidArgumentError(() => driver.setDownloadPath(null))
167
await assertInvalidArgumentError(() => driver.setDownloadPath(''))
168
await assertInvalidArgumentError(() => driver.setDownloadPath(1234))
169
170
const file = await io.tmpFile()
171
await assertInvalidArgumentError(() => driver.setDownloadPath(file))
172
173
async function assertInvalidArgumentError(fn) {
174
try {
175
await fn()
176
return Promise.reject(Error('should have failed'))
177
} catch (err) {
178
if (err instanceof error.InvalidArgumentError) {
179
return
180
}
181
throw err
182
}
183
}
184
})
185
})
186
187
describe('Script pinning', function () {
188
it('allows to pin script', async function () {
189
await driver.get(fileServer.Pages.xhtmlTestPage)
190
191
let script = await driver.pinScript('return document.title;')
192
193
const result = await driver.executeScript(script)
194
195
assert.strictEqual(result, 'XHTML Test Page')
196
})
197
198
it('ensures pinned script is available on new pages', async function () {
199
await driver.get(fileServer.Pages.xhtmlTestPage)
200
await driver.createCDPConnection('page')
201
202
let script = await driver.pinScript('return document.title;')
203
await driver.get(fileServer.Pages.formPage)
204
205
const result = await driver.executeScript(script)
206
207
assert.strictEqual(result, 'We Leave From Here')
208
})
209
210
it('allows to unpin script', async function () {
211
let script = await driver.pinScript('return document.title;')
212
await driver.unpinScript(script)
213
214
await assertJSError(() => driver.executeScript(script))
215
216
async function assertJSError(fn) {
217
try {
218
await fn()
219
return Promise.reject(Error('should have failed'))
220
} catch (err) {
221
if (err instanceof error.JavascriptError) {
222
return
223
}
224
throw err
225
}
226
}
227
})
228
229
it('ensures unpinned scripts are not available on new pages', async function () {
230
await driver.createCDPConnection('page')
231
232
let script = await driver.pinScript('return document.title;')
233
await driver.unpinScript(script)
234
235
await driver.get(fileServer.Pages.formPage)
236
237
await assertJSError(() => driver.executeScript(script))
238
239
async function assertJSError(fn) {
240
try {
241
await fn()
242
return Promise.reject(Error('should have failed'))
243
} catch (err) {
244
if (err instanceof error.JavascriptError) {
245
return
246
}
247
throw err
248
}
249
}
250
})
251
252
it('handles arguments in pinned script', async function () {
253
await driver.get(fileServer.Pages.xhtmlTestPage)
254
await driver.createCDPConnection('page')
255
256
let script = await driver.pinScript('return arguments;')
257
let element = await driver.findElement(by.By.id('id1'))
258
259
const result = await driver.executeScript(script, 1, true, element)
260
261
assert.deepEqual(result, [1, true, element])
262
})
263
264
it('supports async pinned scripts', async function () {
265
let script = await driver.pinScript('arguments[0]()')
266
await assertAsyncScriptPinned(() => driver.executeAsyncScript(script))
267
268
async function assertAsyncScriptPinned(fn) {
269
await fn()
270
}
271
})
272
})
273
},
274
{ browsers: ['chrome'] },
275
)
276
277