forked from jquery/jquery-test-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateDriver.js
More file actions
147 lines (120 loc) · 4.49 KB
/
createDriver.js
File metadata and controls
147 lines (120 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { Builder, Capabilities, logging } from "selenium-webdriver";
import Chrome from "selenium-webdriver/chrome.js";
import Edge from "selenium-webdriver/edge.js";
import Firefox from "selenium-webdriver/firefox.js";
import Safari from "selenium-webdriver/safari.js";
import IE from "selenium-webdriver/ie.js";
import { browserSupportsHeadless } from "../lib/getBrowserString.js";
// Set script timeout to 10min
const DRIVER_SCRIPT_TIMEOUT = 1000 * 60 * 10;
export default async function createDriver( {
browserName, url,
headless = false,
verbose = false,
safariTp = false
} ) {
const capabilities = Capabilities[ browserName ]();
// Support: IE 11+
// When those are set for IE, the process crashes with an error:
// "Unable to match capability set 0: goog:loggingPrefs is an unknown
// extension capability for IE".
if ( browserName !== "ie" ) {
const prefs = new logging.Preferences();
prefs.setLevel( logging.Type.BROWSER, logging.Level.ALL );
capabilities.setLoggingPrefs( prefs );
}
const chromeOptions = new Chrome.Options();
chromeOptions.addArguments( "--enable-chrome-browser-cloud-management" );
// Alter the chrome binary path if
// the CHROME_BIN environment variable is set
if ( process.env.CHROME_BIN ) {
if ( verbose ) {
console.log( `Setting chrome binary to ${ process.env.CHROME_BIN }` );
}
chromeOptions.setChromeBinaryPath( process.env.CHROME_BIN );
}
const firefoxOptions = new Firefox.Options();
if ( process.env.FIREFOX_BIN ) {
if ( verbose ) {
console.log( `Setting firefox binary to ${ process.env.FIREFOX_BIN }` );
}
firefoxOptions.setBinary( process.env.FIREFOX_BIN );
}
const edgeOptions = new Edge.Options();
edgeOptions.addArguments( "--enable-chrome-browser-cloud-management" );
// Alter the edge binary path if
// the EDGE_BIN environment variable is set
if ( process.env.EDGE_BIN ) {
if ( verbose ) {
console.log( `Setting edge binary to ${ process.env.EDGE_BIN }` );
}
edgeOptions.setEdgeChromiumBinaryPath( process.env.EDGE_BIN );
}
const safariOptions = new Safari.Options();
// Use Safari Technology Preview if --safari-tp flag is provided
if ( safariTp ) {
if ( verbose ) {
console.log( "Using Safari Technology Preview" );
}
safariOptions.setTechnologyPreview( true );
// Without it, we're getting an error:
// SessionNotCreatedError: Could not create a session: Browser name does
// not match (requested: safari; available: Safari Technology Preview)
safariOptions.set( "browserName", "Safari Technology Preview" );
}
const ieOptions = new IE.Options();
ieOptions.setEdgeChromium( true );
ieOptions.setEdgePath( "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe" );
// Set IEDriver path from environment variable or GitHub Actions default
let ieService;
let ieDriverPath = process.env.IEWEBDRIVER;
if ( !ieDriverPath && process.env.GITHUB_ACTIONS ) {
// The default in GitHub Actions Windows runners
ieDriverPath = "C:\\SeleniumWebDrivers\\IEDriver";
}
if ( ieDriverPath ) {
// Append the executable name if only a directory was provided
if ( !ieDriverPath.endsWith( ".exe" ) ) {
ieDriverPath = `${ ieDriverPath }\\IEDriverServer.exe`;
}
if ( verbose ) {
console.log( `Setting IEDriver path to ${ ieDriverPath }` );
}
ieService = new IE.ServiceBuilder( ieDriverPath );
}
if ( headless ) {
chromeOptions.addArguments( "--headless=new" );
firefoxOptions.addArguments( "--headless" );
edgeOptions.addArguments( "--headless=new" );
if ( !browserSupportsHeadless( browserName ) ) {
console.log(
`Headless mode is not supported for ${ browserName }.` +
"Running in normal mode instead."
);
}
}
const builder = new Builder().withCapabilities( capabilities )
.setChromeOptions( chromeOptions )
.setFirefoxOptions( firefoxOptions )
.setEdgeOptions( edgeOptions )
.setSafariOptions( safariOptions )
.setIeOptions( ieOptions );
if ( ieService ) {
builder.setIeService( ieService );
}
const driver = builder.build();
if ( verbose ) {
const driverCapabilities = await driver.getCapabilities();
const name = driverCapabilities.getBrowserName();
const version = driverCapabilities.getBrowserVersion();
console.log( `\nDriver created for ${ name } ${ version }` );
}
// Increase script timeout to 10min
await driver.manage().setTimeouts( { script: DRIVER_SCRIPT_TIMEOUT } );
// Set the first URL for the browser,
// but don't wait for the page to load
// so the worker is set up in time
// for the ack request.
driver.get( url );
return driver;
}