Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/lib/testutil.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 sinon = require('sinon')
22
23
class StubError extends Error {
24
constructor(opt_msg) {
25
super(opt_msg)
26
this.name = this.constructor.name
27
}
28
}
29
30
exports.StubError = StubError
31
32
exports.throwStubError = function () {
33
throw new StubError()
34
}
35
36
exports.assertIsStubError = function (value) {
37
assert.ok(value instanceof StubError, value + ' is not a ' + StubError.name)
38
}
39
40
exports.assertIsInstance = function (ctor, value) {
41
assert.ok(value instanceof ctor, 'Not a ' + ctor.name + ': ' + value)
42
}
43
44
function callbackPair(cb, eb) {
45
if (cb && eb) {
46
throw new TypeError('can only specify one of callback or errback')
47
}
48
49
let callback = cb ? sinon.spy(cb) : sinon.spy()
50
let errback = eb ? sinon.spy(eb) : sinon.spy()
51
52
function assertCallback() {
53
assert.ok(callback.called, 'callback not called')
54
assert.ok(!errback.called, 'errback called')
55
if (callback.threw()) {
56
throw callback.exceptions[0]
57
}
58
}
59
60
function assertErrback() {
61
assert.ok(!callback.called, 'callback called')
62
assert.ok(errback.called, 'errback not called')
63
if (errback.threw()) {
64
throw errback.exceptions[0]
65
}
66
}
67
68
function assertNeither() {
69
assert.ok(!callback.called, 'callback called')
70
assert.ok(!errback.called, 'errback called')
71
}
72
73
return {
74
callback,
75
errback,
76
assertCallback,
77
assertErrback,
78
assertNeither,
79
}
80
}
81
82
exports.callbackPair = callbackPair
83
84
exports.callbackHelper = function (cb) {
85
let pair = callbackPair(cb)
86
let wrapped = pair.callback.bind(null)
87
wrapped.assertCalled = () => pair.assertCallback()
88
wrapped.assertNotCalled = () => pair.assertNeither()
89
return wrapped
90
}
91
92