Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/lib/webdriver_script_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 fileServer = require('../../lib/test/fileserver')
24
const until = require('selenium-webdriver/lib/until')
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('script()', function () {
43
it('can listen to console log', async function () {
44
let log = null
45
const handler = await driver.script().addConsoleMessageHandler((logEntry) => {
46
log = logEntry
47
})
48
49
await driver.get(Pages.logEntryAdded)
50
await driver.findElement({ id: 'consoleLog' }).click()
51
52
await delay(3000)
53
54
assert.equal(log.text, 'Hello, world!')
55
assert.equal(log.realm, null)
56
assert.equal(log.type, 'console')
57
assert.equal(log.level, 'info')
58
assert.equal(log.method, 'log')
59
assert.equal(log.args.length, 1)
60
await driver.script().removeConsoleMessageHandler(handler)
61
})
62
63
it('can listen to javascript error', async function () {
64
let log = null
65
const handler = await driver.script().addJavaScriptErrorHandler((logEntry) => {
66
log = logEntry
67
})
68
69
await driver.get(Pages.logEntryAdded)
70
await driver.findElement({ id: 'jsException' }).click()
71
72
await delay(3000)
73
74
assert.equal(log.text, 'Error: Not working')
75
assert.equal(log.type, 'javascript')
76
assert.equal(log.level, 'error')
77
78
await driver.script().removeJavaScriptErrorHandler(handler)
79
})
80
81
it('throws an error while removing a handler that does not exist', async function () {
82
try {
83
await driver.script().removeJavaScriptErrorHandler(10)
84
assert.fail('Expected error not thrown. Non-existent handler cannot be removed')
85
} catch (e) {
86
assert.strictEqual(e.message, 'Callback with id 10 not found')
87
}
88
})
89
90
it('can listen to dom mutations', async function () {
91
let message = null
92
await driver.script().addDomMutationHandler((m) => {
93
message = m
94
})
95
96
await driver.get(fileServer.Pages.dynamicPage)
97
98
let element = driver.findElement({ id: 'reveal' })
99
await element.click()
100
let revealed = driver.findElement({ id: 'revealed' })
101
await driver.wait(until.elementIsVisible(revealed), 5000)
102
103
assert.strictEqual(message['attribute_name'], 'style')
104
assert.strictEqual(message['current_value'], '')
105
assert.strictEqual(message['old_value'], 'display:none;')
106
})
107
108
it('can remove to dom mutation handler', async function () {
109
let message = null
110
let id = await driver.script().addDomMutationHandler((m) => {
111
message = m
112
})
113
114
await driver.get(fileServer.Pages.dynamicPage)
115
116
await driver.script().removeDomMutationHandler(id)
117
118
let element = driver.findElement({ id: 'reveal' })
119
await element.click()
120
let revealed = driver.findElement({ id: 'revealed' })
121
await driver.wait(until.elementIsVisible(revealed), 5000)
122
123
assert.strictEqual(message, null)
124
})
125
126
it('can pin script', async function () {
127
await driver.script().pin("() => { console.log('Hello!'); }")
128
let log
129
130
await driver.script().addConsoleMessageHandler((logEntry) => {
131
log = logEntry
132
})
133
134
await driver.get(Pages.logEntryAdded)
135
136
await delay(3000)
137
138
assert.equal(log.text, 'Hello!')
139
})
140
141
it('can unpin script', async function () {
142
const id = await driver.script().pin("() => { console.log('Hello!'); }")
143
144
let count = 0
145
await driver.script().addConsoleMessageHandler((logEntry) => {
146
count++
147
})
148
149
await driver.get(Pages.logEntryAdded)
150
151
await driver.script().unpin(id)
152
153
await driver.get(Pages.logEntryAdded)
154
155
assert.equal(count, 1)
156
})
157
})
158
},
159
{ browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] },
160
)
161
162