forked from shaka-project/karma-local-wd-launcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
169 lines (137 loc) · 4.64 KB
/
index.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*! @license
* Karma Local WebDriver Launcher
* Copyright 2022 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview
*
* Launches local web browsers using WebDriver, to enable screenshots and other
* advanced tests to be executed in-browser. If you don't need WebDriver to
* enable some test scenario in Karma, you can just use typical local browser
* launchers.
*
* Supports Chrome, Firefox, and Safari.
*/
const os = require('os');
const url = require('url');
const wd = require('wd');
// Map nodejs OS names to Selenium platform names.
const PLATFORM_MAP = {
'darwin': 'Mac',
'win32': 'Windows',
'linux': 'Linux',
};
const LocalWebDriverBase =
function(browserName, driverCommand, baseBrowserDecorator, logger) {
baseBrowserDecorator(this);
this.browserName = browserName;
this.DEFAULT_CMD = {
linux: driverCommand,
darwin: driverCommand,
win32: driverCommand,
};
this.ENV_CMD = this.browserName.toUpperCase() + '_CMD';
const config = {
protocol: 'http:',
hostname: '127.0.0.1',
port: Math.floor((Math.random() * 1000)) + 4000,
pathname: '/'
};
const webDriver = url.format(config);
this.name = `${this.browserName} via WebDriver at ` + webDriver;
const log = logger.create(this.name);
log.debug('config:', JSON.stringify(config));
// These names ("browser" and "spec") are needed for compatibility with
// karma-webdriver-launcher.
this.browser = wd.remote(config);
this.spec = {
browserName: this.browserName.toLowerCase(),
platform: PLATFORM_MAP[os.platform()],
};
this.browser.on('status', (info) => {
log.debug('Status: ' + info);
});
this.browser.on('command', (eventType, command, response) => {
log.debug('[command] ' + eventType + ' ' + command + ' ' + (response || ''));
});
this.browser.on('http', (meth, path, data) => {
log.debug('[http] ' + meth + ' ' + path + ' ' + (data || ''));
});
this._getOptions = () => ['-p', config.port.toString()];
this.on('start', (url) => {
this.browser.init(this.spec, (error) => {
if (error) {
log.error(`Could not connect to ${this.browserName} WebDriver`);
log.error(error);
} else {
log.debug(`Connected to ${this.browserName} WebDriver`);
log.debug('Connecting to ' + url);
this.browser.get(url);
}
});
});
// The base decorators will listen for the 'kill' event to close the process
// for the driver. Once that happens, we can no longer stop the webdriver
// connection and close the open browser window. There is no way to register
// a listener ahead of the base class's, so to shut down the browser
// properly, we need to reimplement all the methods that could trigger a
// 'kill' event.
this.kill = async () => {
this.state = 'BEING_KILLED';
await this.stopWebdriver_();
};
this.forceKill = async () => {
this.state = 'BEING_FORCE_KILLED';
await this.stopWebdriver_();
};
const originalStart = this.start;
let previousUrl = null;
this.start = (url) => {
previousUrl = url;
originalStart.call(this, url);
};
this.restart = async () => {
if (this.state == 'BEING_FORCE_KILLED') {
return;
}
this.state = 'RESTARTING';
await this.stopWebDriver();
if (this.state != 'BEING_FORCE_KILLED') {
log.debug(`Restarting ${this.name}`)
this.start(previousUrl);
}
};
this.stopWebdriver_ = async () => {
if (this.browser) {
await new Promise(resolve => this.browser.quit(resolve));
}
// Now that the driver connection and browser are closed, emit the signal
// that shuts down the driver executable.
await this.emitAsync('kill');
this.state = 'FINISHED';
};
}
const LocalWebDriverChrome = function(baseBrowserDecorator, logger) {
LocalWebDriverBase.call(this,
'Chrome', 'chromedriver', baseBrowserDecorator, logger);
};
const LocalWebDriverFirefox = function(baseBrowserDecorator, logger) {
LocalWebDriverBase.call(this,
'Firefox', 'geckodriver', baseBrowserDecorator, logger);
};
const LocalWebDriverSafari = function(baseBrowserDecorator, logger) {
LocalWebDriverBase.call(this,
'Safari', 'safaridriver', baseBrowserDecorator, logger);
};
LocalWebDriverChrome.$inject = ['baseBrowserDecorator', 'logger'];
LocalWebDriverFirefox.$inject = ['baseBrowserDecorator', 'logger'];
LocalWebDriverSafari.$inject = ['baseBrowserDecorator', 'logger'];
module.exports = {
'launcher:Chrome': ['type', LocalWebDriverChrome],
'launcher:Firefox': ['type', LocalWebDriverFirefox],
};
// Safari is only supported on Mac.
if (os.platform() == 'darwin') {
module.exports['launcher:Safari'] = ['type', LocalWebDriverSafari];
}