Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/driver_factory.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
const fs = require('node:fs')
20
const os = require('node:os')
21
const path = require('node:path')
22
const { Browser } = require('selenium-webdriver/index')
23
const { Environment } = require('selenium-webdriver/testing')
24
const chrome = require('selenium-webdriver/chrome')
25
const firefox = require('selenium-webdriver/firefox')
26
const { runfiles } = require('@bazel/runfiles')
27
28
function GetBrowserForTests() {
29
let browser = process.env.SELENIUM_BROWSER
30
31
// If we have no browser set, fail the build
32
if (!browser) {
33
throw new Error('SELENIUM_BROWSER env var not set')
34
}
35
36
if (browser.indexOf(',') !== -1) {
37
throw new Error('SELENIUM_BROWSER env var must only be a single browser')
38
}
39
40
/** @type !TargetBrowser */
41
const targetBrowser = { name: browser, capabilities: undefined }
42
const builder = new Environment(targetBrowser).builder()
43
builder.disableEnvironmentOverrides()
44
let binary = process.env.BROWSER_BINARY
45
let driverBinary = process.env.DRIVER_BINARY
46
47
let resolvedBinary = binary ? runfiles.resolve(driverBinary) : undefined
48
let resolvedDriver = driverBinary ? runfiles.resolve(binary) : undefined
49
50
// Create a temporary directory we can use as a home dir
51
// process.env["USER"] = "nobody"
52
process.env['HOME'] = fs.mkdtempSync(path.join(os.tmpdir(), 'jasmine-test'))
53
54
switch (browser) {
55
case 'chrome':
56
builder.forBrowser(Browser.CHROME)
57
if (resolvedDriver) {
58
let sb = new chrome.ServiceBuilder(resolvedDriver)
59
sb.enableVerboseLogging()
60
sb.setStdio('inherit')
61
builder.setChromeService(sb)
62
}
63
if (resolvedBinary) {
64
let options = new chrome.Options()
65
options.setChromeBinaryPath(resolvedBinary)
66
options.setAcceptInsecureCerts(true)
67
options.addArguments('disable-infobars', 'disable-breakpad', 'disable-dev-shm-usage', 'no-sandbox')
68
builder.setChromeOptions(options)
69
}
70
break
71
72
// case 'edge':
73
// builder = builder.forBrowser(webdriver.Browser.EDGE)
74
// break
75
//
76
case 'firefox':
77
builder.forBrowser(Browser.FIREFOX)
78
if (resolvedDriver) {
79
let sb = new firefox.ServiceBuilder(resolvedDriver)
80
sb.enableVerboseLogging(true)
81
sb.setStdio('inherit')
82
builder.setFirefoxService(sb)
83
}
84
if (resolvedBinary) {
85
let options = new firefox.Options()
86
options.setBinary(resolvedBinary)
87
options.enableDebugger()
88
builder.setFirefoxOptions(options)
89
}
90
break
91
92
case 'safari':
93
builder.forBrowser(Browser.SAFARI)
94
break
95
96
default:
97
throw new Error('SELENIUM_BROWSER does not list a supported browser')
98
}
99
100
return builder.build()
101
}
102
103
module.exports = {
104
GetBrowserForTests,
105
}
106
107