Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/builder_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
22
const chrome = require('selenium-webdriver/chrome')
23
const edge = require('selenium-webdriver/edge')
24
const error = require('selenium-webdriver/lib/error')
25
const firefox = require('selenium-webdriver/firefox')
26
const ie = require('selenium-webdriver/ie')
27
const safari = require('selenium-webdriver/safari')
28
const test = require('../lib/test')
29
const { Browser } = require('selenium-webdriver/lib/capabilities')
30
const { Pages } = require('../lib/test')
31
const { Builder, Capabilities } = require('selenium-webdriver')
32
33
test.suite(function (env) {
34
const BROWSER_MAP = new Map([
35
[Browser.CHROME, chrome.Driver],
36
[Browser.EDGE, edge.Driver],
37
[Browser.FIREFOX, firefox.Driver],
38
[Browser.INTERNET_EXPLORER, ie.Driver],
39
[Browser.SAFARI, safari.Driver],
40
])
41
42
if (BROWSER_MAP.has(env.browser.name)) {
43
describe('builder creates thenable driver instances', function () {
44
let driver
45
46
after(() => driver && driver.quit())
47
48
it(env.browser.name, function () {
49
driver = env.builder().build()
50
51
const want = BROWSER_MAP.get(env.browser.name)
52
assert.ok(driver instanceof want, `want ${want.name}, but got ${driver.name}`)
53
assert.strictEqual(typeof driver.then, 'function')
54
55
return (
56
driver
57
.then((d) => assert.ok(d instanceof want, `want ${want.name}, but got ${d.name}`))
58
// Load something so the safari driver doesn't crash from starting and
59
// stopping in short time.
60
.then(() => driver.get(Pages.echoPage))
61
)
62
})
63
})
64
}
65
66
if (BROWSER_MAP.has(env.browser.name)) {
67
describe('builder allows to set a single capability', function () {
68
let driver
69
70
after(() => driver && driver.quit())
71
72
it(env.browser.name, async function () {
73
let timeouts = { implicit: 0, pageLoad: 1000, script: 1000 }
74
driver = new Builder().setCapability('timeouts', timeouts).forBrowser(env.browser.name).build()
75
76
let caps = await getCaps(driver)
77
assert.deepEqual(caps.get('timeouts'), timeouts)
78
})
79
})
80
}
81
82
async function getCaps(driver) {
83
return driver.getCapabilities()
84
}
85
86
describe('Builder', function () {
87
describe('catches incorrect use of browser options class', function () {
88
function test(key, options) {
89
it(key, async function () {
90
let builder = new Builder().withCapabilities(
91
new Capabilities().set('browserName', 'fake-browser-should-not-try-to-start').set(key, new options()),
92
)
93
try {
94
let driver = await builder.build()
95
await driver.quit()
96
return Promise.reject(Error('should have failed'))
97
} catch (ex) {
98
if (!(ex instanceof error.InvalidArgumentError)) {
99
throw ex
100
}
101
}
102
})
103
}
104
105
test('chromeOptions', chrome.Options)
106
test('moz:firefoxOptions', firefox.Options)
107
test('safari.options', safari.Options)
108
})
109
})
110
})
111
112