@testring/plugin-selenium-driver 0.7.5 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,34 +1,40 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
10
24
  };
11
- var __rest = (this && this.__rest) || function (s, e) {
12
- var t = {};
13
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
14
- t[p] = s[p];
15
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
16
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
17
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
18
- t[p[i]] = s[p[i]];
19
- }
20
- return t;
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
21
27
  };
22
28
  Object.defineProperty(exports, "__esModule", { value: true });
23
29
  exports.SeleniumPlugin = void 0;
24
30
  exports.default = seleniumProxy;
25
31
  const webdriverio_1 = require("webdriverio");
26
- const deepmerge = require("deepmerge");
32
+ const deepmerge = __importStar(require("deepmerge"));
27
33
  const child_process_1 = require("@testring/child-process");
28
34
  const logger_1 = require("@testring/logger");
29
35
  const dwnld_collector_crx_1 = require("@testring/dwnld-collector-crx");
30
36
  const code_coverage_client_1 = require("@nullcc/code-coverage-client");
31
- const webdriver_1 = require("webdriver");
37
+ const webdriver_1 = __importDefault(require("webdriver"));
32
38
  const DEFAULT_CONFIG = {
33
39
  recorderExtension: false,
34
40
  clientCheckInterval: 5 * 1000,
@@ -38,7 +44,6 @@ const DEFAULT_CONFIG = {
38
44
  capabilities: {
39
45
  browserName: 'chrome',
40
46
  'goog:chromeOptions': {
41
- // for local ChromeDriver
42
47
  args: [],
43
48
  },
44
49
  'wdio:enforceWebDriverClassic': true,
@@ -55,8 +60,9 @@ function stringifyWindowFeatures(windowFeatures) {
55
60
  result = windowFeatures;
56
61
  }
57
62
  else {
58
- result = Object.keys(windowFeatures)
59
- .map((key) => `${key}=${windowFeatures[key]}`)
63
+ const features = windowFeatures;
64
+ result = Object.keys(features)
65
+ .map((key) => `${key}=${features[key]}`)
60
66
  .join(',');
61
67
  }
62
68
  return result;
@@ -66,6 +72,7 @@ class SeleniumPlugin {
66
72
  this.logger = logger_1.loggerClient.withPrefix('[selenium-browser-process]');
67
73
  this.expiredBrowserClients = new Set();
68
74
  this.browserClients = new Map();
75
+ this.customBrowserClientsConfigs = new Map();
69
76
  this.waitForReadyState = Promise.resolve();
70
77
  this.incrementWinId = 0;
71
78
  this.config = this.createConfig(config);
@@ -76,15 +83,16 @@ class SeleniumPlugin {
76
83
  }
77
84
  // eslint-disable-next-line sonarjs/cognitive-complexity
78
85
  createConfig(config) {
79
- var _a, _b;
86
+ var _a;
80
87
  const mergedConfig = deepmerge.all([DEFAULT_CONFIG, config], {
81
88
  clone: true,
82
89
  });
83
90
  if (!mergedConfig.hostname && mergedConfig.host) {
84
91
  mergedConfig.hostname = mergedConfig.host;
85
92
  }
86
- const googleChromeOptions = (_a = mergedConfig.capabilities) === null || _a === void 0 ? void 0 : _a['goog:chromeOptions'];
87
- if ((_b = googleChromeOptions === null || googleChromeOptions === void 0 ? void 0 : googleChromeOptions.args) === null || _b === void 0 ? void 0 : _b.includes('--headless=new')) {
93
+ const capabilities = mergedConfig.capabilities;
94
+ const googleChromeOptions = capabilities === null || capabilities === void 0 ? void 0 : capabilities['goog:chromeOptions'];
95
+ if ((_a = googleChromeOptions === null || googleChromeOptions === void 0 ? void 0 : googleChromeOptions.args) === null || _a === void 0 ? void 0 : _a.includes('--headless=new')) {
88
96
  const extensions = googleChromeOptions.extensions;
89
97
  const dowldMonitorCrx = (0, dwnld_collector_crx_1.getCrxBase64)();
90
98
  if (extensions) {
@@ -129,44 +137,41 @@ class SeleniumPlugin {
129
137
  }
130
138
  return [`-Dwebdriver.chrome.driver=${chromeDriverPath}`];
131
139
  }
132
- runLocalSelenium() {
133
- return __awaiter(this, void 0, void 0, function* () {
134
- const seleniumServer = require('selenium-server');
135
- const seleniumJarPath = seleniumServer.path;
136
- this.logger.debug('Init local selenium server');
137
- try {
138
- this.localSelenium = (0, child_process_1.spawnWithPipes)('java', [
139
- ...this.getChromeDriverArgs(),
140
- '-jar',
141
- seleniumJarPath,
142
- '-port',
143
- this.config.port,
144
- ]);
145
- this.waitForReadyState = new Promise((resolve, reject) => {
146
- if (this.localSelenium.stderr) {
147
- this.localSelenium.stderr.on('data', (data) => {
148
- const message = data.toString();
149
- this.logger.verbose(message);
150
- if (message.includes('SeleniumServer.boot')) {
151
- delay(500).then(resolve);
152
- }
153
- });
154
- }
155
- else {
156
- reject(new Error('There is no STDERR on selenium worker'));
157
- }
158
- });
159
- }
160
- catch (err) {
161
- this.logger.error('Local selenium server init failed', err);
162
- }
163
- });
140
+ async runLocalSelenium() {
141
+ const seleniumServer = require('selenium-server');
142
+ const seleniumJarPath = seleniumServer.path;
143
+ this.logger.debug('Init local selenium server');
144
+ try {
145
+ this.localSelenium = (0, child_process_1.spawnWithPipes)('java', [
146
+ ...this.getChromeDriverArgs(),
147
+ '-jar',
148
+ seleniumJarPath,
149
+ '-port',
150
+ this.config.port,
151
+ ]);
152
+ this.waitForReadyState = new Promise((resolve, reject) => {
153
+ var _a;
154
+ if ((_a = this.localSelenium) === null || _a === void 0 ? void 0 : _a.stderr) {
155
+ this.localSelenium.stderr.on('data', (data) => {
156
+ const message = data.toString();
157
+ this.logger.verbose(message);
158
+ if (message.includes('SeleniumServer.boot')) {
159
+ delay(500).then(resolve);
160
+ }
161
+ });
162
+ }
163
+ else {
164
+ reject(new Error('There is no STDERR on selenium worker'));
165
+ }
166
+ });
167
+ }
168
+ catch (err) {
169
+ this.logger.error('Local selenium server init failed', err);
170
+ }
164
171
  }
165
172
  getApplicantSessionId(applicant) {
166
173
  const item = this.browserClients.get(applicant);
167
- if (item) {
168
- return item.sessionId;
169
- }
174
+ return item === null || item === void 0 ? void 0 : item.sessionId;
170
175
  }
171
176
  hasBrowserClient(applicant) {
172
177
  return this.browserClients.has(applicant);
@@ -178,878 +183,736 @@ class SeleniumPlugin {
178
183
  }
179
184
  throw new Error('Browser client is not found');
180
185
  }
181
- pingClients() {
182
- return __awaiter(this, void 0, void 0, function* () {
183
- for (const [applicant] of this.browserClients) {
186
+ async pingClients() {
187
+ for (const [applicant] of this.browserClients) {
188
+ try {
189
+ await this.execute(applicant, '(function () {})()', []);
190
+ }
191
+ catch (e) {
192
+ /* ignore */
193
+ }
194
+ }
195
+ }
196
+ async closeExpiredClients() {
197
+ const timeLimit = Date.now() - this.config.clientTimeout;
198
+ for (const [applicant, clientData] of this.browserClients) {
199
+ if (clientData.initTime < timeLimit) {
200
+ this.logger.warn(`Session applicant ${applicant} marked as expired`);
184
201
  try {
185
- yield this.execute(applicant, '(function () {})()', []);
202
+ await this.end(applicant);
186
203
  }
187
204
  catch (e) {
188
- /* ignore */
205
+ this.logger.error(`Session applicant ${applicant} failed to stop`, e);
189
206
  }
207
+ this.expiredBrowserClients.add(applicant);
190
208
  }
191
- });
209
+ }
192
210
  }
193
- closeExpiredClients() {
194
- return __awaiter(this, void 0, void 0, function* () {
195
- const timeLimit = Date.now() - this.config.clientTimeout;
196
- for (const [applicant, clientData] of this.browserClients) {
197
- if (clientData.initTime < timeLimit) {
198
- this.logger.warn(`Session applicant ${applicant} marked as expired`);
199
- try {
200
- yield this.end(applicant);
201
- }
202
- catch (e) {
203
- this.logger.error(`Session applicant ${applicant} failed to stop`, e);
204
- }
205
- this.expiredBrowserClients.add(applicant);
206
- }
207
- }
208
- });
211
+ async checkClientsTimeout() {
212
+ if (this.config.clientTimeout === 0) {
213
+ await this.pingClients();
214
+ }
215
+ else {
216
+ await this.closeExpiredClients();
217
+ }
209
218
  }
210
- checkClientsTimeout() {
211
- return __awaiter(this, void 0, void 0, function* () {
212
- if (this.config.clientTimeout === 0) {
213
- yield this.pingClients();
214
- }
215
- else {
216
- yield this.closeExpiredClients();
217
- }
218
- });
219
+ setCustomBrowserClientConfig(applicant, config) {
220
+ this.customBrowserClientsConfigs.set(applicant, config);
219
221
  }
220
- createClient(applicant, config) {
221
- return __awaiter(this, void 0, void 0, function* () {
222
- yield this.waitForReadyState;
223
- const clientData = this.browserClients.get(applicant);
224
- if (clientData) {
225
- this.browserClients.set(applicant, Object.assign(Object.assign({}, clientData), { initTime: Date.now() }));
226
- return;
227
- }
228
- if (this.expiredBrowserClients.has(applicant)) {
229
- throw Error(`This session expired in ${this.config.clientTimeout}ms`);
230
- }
231
- const _config = deepmerge.all([
232
- {},
233
- this.config,
234
- config || {},
235
- ]);
236
- const client = yield (0, webdriverio_1.remote)(_config);
237
- let sessionId;
238
- if (client.sessionId) {
239
- sessionId = client.sessionId;
240
- }
241
- else {
242
- throw Error('Session can not be null');
243
- }
244
- const customClient = this.addCustromMethods(client);
245
- let cdpCoverageCollector;
246
- if (this.config.cdpCoverage) {
247
- this.logger.debug('Started to init cdp coverage....');
248
- cdpCoverageCollector = yield this.enableCDPCoverageClient(client);
249
- this.logger.debug('ended to init cdp coverage....');
250
- }
222
+ getCustomBrowserClientConfig(applicant) {
223
+ return this.customBrowserClientsConfigs.get(applicant);
224
+ }
225
+ async createClient(applicant, config) {
226
+ await this.waitForReadyState;
227
+ const clientData = this.browserClients.get(applicant);
228
+ if (clientData) {
251
229
  this.browserClients.set(applicant, {
252
- client: customClient,
253
- sessionId,
230
+ ...clientData,
254
231
  initTime: Date.now(),
255
- cdpCoverageCollector: cdpCoverageCollector
256
- ? cdpCoverageCollector
257
- : null,
258
232
  });
259
- this.logger.debug(`Started session for applicant: ${applicant}. Session id: ${sessionId}`);
260
- });
261
- }
262
- enableCDPCoverageClient(client) {
263
- return __awaiter(this, void 0, void 0, function* () {
264
- if (this.config.host === undefined) {
265
- return null;
266
- }
267
- //accurate
268
- if (!client.capabilities['se:cdp']) {
269
- return null;
270
- }
271
- const cdpAddress = client.capabilities['se:cdp'];
272
- const collector = new code_coverage_client_1.CDPCoverageCollector({
273
- wsEndpoint: cdpAddress,
274
- });
275
- yield collector.init();
276
- yield collector.start();
277
- return collector;
233
+ return;
234
+ }
235
+ if (this.expiredBrowserClients.has(applicant)) {
236
+ throw Error(`This session expired in ${this.config.clientTimeout}ms`);
237
+ }
238
+ const _config = deepmerge.all([
239
+ {},
240
+ this.config,
241
+ config || {},
242
+ this.customBrowserClientsConfigs.get(applicant) || {},
243
+ ]);
244
+ const client = await (0, webdriverio_1.remote)(_config);
245
+ let sessionId;
246
+ if (client.sessionId) {
247
+ sessionId = client.sessionId;
248
+ }
249
+ else {
250
+ throw Error('Session can not be null');
251
+ }
252
+ const customClient = this.addCustromMethods(client);
253
+ let cdpCoverageCollector;
254
+ if (this.config.cdpCoverage) {
255
+ this.logger.debug('Started to init cdp coverage....');
256
+ cdpCoverageCollector = await this.enableCDPCoverageClient(client);
257
+ this.logger.debug('ended to init cdp coverage....');
258
+ }
259
+ this.browserClients.set(applicant, {
260
+ client: customClient,
261
+ sessionId,
262
+ initTime: Date.now(),
263
+ cdpCoverageCollector: cdpCoverageCollector
264
+ ? cdpCoverageCollector
265
+ : null,
278
266
  });
267
+ this.logger.debug(`Started session for applicant: ${applicant}. Session id: ${sessionId}`);
279
268
  }
280
- getCdpCoverageFile(applicant) {
281
- return __awaiter(this, void 0, void 0, function* () {
282
- const clientData = this.browserClients.get(applicant);
283
- this.logger.debug(`start upload coverage for applicant ${applicant}`);
284
- if (!clientData) {
285
- return;
286
- }
287
- const coverageCollector = clientData.cdpCoverageCollector;
288
- if (!coverageCollector) {
289
- return;
290
- }
291
- const { coverage } = yield coverageCollector.collect();
292
- yield coverageCollector.stop();
293
- return [Buffer.from(JSON.stringify(coverage))];
294
- });
269
+ async enableCDPCoverageClient(client) {
270
+ if (this.config.host === undefined) {
271
+ return null;
272
+ }
273
+ //accurate
274
+ if (!client.capabilities['se:cdp']) {
275
+ return null;
276
+ }
277
+ const cdpAddress = client.capabilities['se:cdp'];
278
+ const collector = new code_coverage_client_1.CDPCoverageCollector({
279
+ wsEndpoint: cdpAddress,
280
+ });
281
+ await collector.init();
282
+ await collector.start();
283
+ return collector;
284
+ }
285
+ async getCdpCoverageFile(applicant) {
286
+ const clientData = this.browserClients.get(applicant);
287
+ this.logger.debug(`start upload coverage for applicant ${applicant}`);
288
+ if (!clientData) {
289
+ return;
290
+ }
291
+ const coverageCollector = clientData.cdpCoverageCollector;
292
+ if (!coverageCollector) {
293
+ return;
294
+ }
295
+ const { coverage } = await coverageCollector.collect();
296
+ await coverageCollector.stop();
297
+ return [Buffer.from(JSON.stringify(coverage))];
295
298
  }
296
299
  addCustromMethods(client) {
297
300
  return client;
298
301
  }
299
- end(applicant) {
300
- return __awaiter(this, void 0, void 0, function* () {
301
- yield this.waitForReadyState;
302
- if (!this.hasBrowserClient(applicant)) {
303
- this.logger.warn(`No ${applicant} is registered`);
304
- return;
305
- }
306
- const client = this.getBrowserClient(applicant);
307
- try {
308
- yield this.alertDismiss(applicant);
309
- }
310
- catch (_a) {
311
- /* ignore */
312
- }
313
- const startingSessionID = this.getApplicantSessionId(applicant);
314
- const sessionID = client.sessionId;
315
- if (startingSessionID === sessionID) {
316
- this.logger.debug(`Stopping sessions for applicant ${applicant}. Session id: ${sessionID}`);
317
- yield client.deleteSession();
318
- }
319
- else {
320
- yield this.logger.stepWarning(`Stopping sessions for applicant warning ${applicant}. ` +
321
- `Session ids are not equal, started with - ${startingSessionID}, ended with - ${sessionID}`, () => __awaiter(this, void 0, void 0, function* () {
322
- try {
323
- if (startingSessionID) {
324
- const attachedClient = webdriver_1.default.attachToSession({
325
- sessionId: startingSessionID,
326
- });
327
- yield attachedClient.deleteSession();
328
- }
329
- }
330
- catch (err) {
331
- this.logger.error(`Old session ${startingSessionID} delete error`, err);
332
- }
333
- try {
334
- yield client.deleteSession();
335
- }
336
- catch (err) {
337
- this.logger.error(`New session ${client.sessionId} delete error`, err);
302
+ async end(applicant) {
303
+ await this.waitForReadyState;
304
+ if (!this.hasBrowserClient(applicant)) {
305
+ this.logger.warn(`No ${applicant} is registered`);
306
+ return;
307
+ }
308
+ const client = this.getBrowserClient(applicant);
309
+ try {
310
+ await this.alertDismiss(applicant);
311
+ }
312
+ catch {
313
+ /* ignore */
314
+ }
315
+ const startingSessionID = this.getApplicantSessionId(applicant);
316
+ const sessionID = client.sessionId;
317
+ if (startingSessionID === sessionID) {
318
+ this.logger.debug(`Stopping sessions for applicant ${applicant}. Session id: ${sessionID}`);
319
+ await client.deleteSession();
320
+ }
321
+ else {
322
+ await this.logger.stepWarning(`Stopping sessions for applicant warning ${applicant}. ` +
323
+ `Session ids are not equal, started with - ${startingSessionID}, ended with - ${sessionID}`, async () => {
324
+ try {
325
+ if (startingSessionID) {
326
+ const attachedClient = webdriver_1.default.attachToSession({
327
+ sessionId: startingSessionID,
328
+ });
329
+ await attachedClient.deleteSession();
338
330
  }
339
- }));
340
- }
341
- // Empirically: pause after close() to let Selenium kill ChromeDriver cleanly (no zombie process).
342
- if (this.config.delayAfterSessionClose) {
343
- yield delay(this.config.delayAfterSessionClose);
344
- }
345
- this.browserClients.delete(applicant);
346
- });
347
- }
348
- kill() {
349
- return __awaiter(this, void 0, void 0, function* () {
350
- var _a, _b, _c, _d;
351
- this.logger.debug('Kill command is called');
352
- // Close all browser sessions
353
- for (const applicant of this.browserClients.keys()) {
331
+ }
332
+ catch (err) {
333
+ this.logger.error(`Old session ${startingSessionID} delete error`, err);
334
+ }
354
335
  try {
355
- yield this.end(applicant);
336
+ await client.deleteSession();
356
337
  }
357
- catch (e) {
358
- this.logger.error(e);
338
+ catch (err) {
339
+ this.logger.error(`New session ${client.sessionId} delete error`, err);
359
340
  }
341
+ });
342
+ }
343
+ // Empirically: pause after close() to let Selenium kill ChromeDriver cleanly (no zombie process).
344
+ if (this.config.delayAfterSessionClose) {
345
+ await delay(this.config.delayAfterSessionClose);
346
+ }
347
+ this.browserClients.delete(applicant);
348
+ this.customBrowserClientsConfigs.delete(applicant);
349
+ }
350
+ async kill() {
351
+ var _a, _b, _c, _d;
352
+ this.logger.debug('Kill command is called');
353
+ // Close all browser sessions
354
+ for (const applicant of this.browserClients.keys()) {
355
+ try {
356
+ await this.end(applicant);
360
357
  }
361
- // If using 'local' mode, stop all active sessions
362
- if (this.config.workerLimit === 'local') {
363
- yield this.stopAllSessions();
358
+ catch (e) {
359
+ this.logger.error(e);
364
360
  }
365
- if (this.localSelenium) {
366
- // remove listener
367
- if (this.localSelenium.stderr) {
368
- this.localSelenium.stderr.removeAllListeners('data');
369
- (_a = this.localSelenium.stdout) === null || _a === void 0 ? void 0 : _a.removeAllListeners();
370
- }
371
- // Ensure all pipes are closed
372
- (_b = this.localSelenium.stdout) === null || _b === void 0 ? void 0 : _b.destroy();
373
- (_c = this.localSelenium.stderr) === null || _c === void 0 ? void 0 : _c.destroy();
374
- (_d = this.localSelenium.stdin) === null || _d === void 0 ? void 0 : _d.destroy();
375
- this.logger.debug(`Stopping local Selenium server (PID: ${this.localSelenium.pid})`);
376
- // Try SIGTERM first
377
- this.localSelenium.kill('SIGTERM');
378
- // Wait for exit event with a timeout (ensures it does not hang forever)
379
- const waitForExit = new Promise((resolve) => {
380
- this.localSelenium.once('exit', () => {
381
- this.logger.debug('Selenium process exited.');
382
- resolve();
383
- });
384
- });
385
- // Force kill if not exiting within 3 seconds
386
- const forceKill = new Promise((resolve) => {
387
- setTimeout(() => {
388
- if (!this.localSelenium.killed) {
389
- this.logger.warn(`Selenium did not exit in time. Sending SIGKILL.`);
390
- this.localSelenium.kill('SIGKILL');
391
- }
392
- resolve();
393
- }, 3000);
361
+ }
362
+ // If using 'local' mode, stop all active sessions
363
+ if (this.config.workerLimit === 'local') {
364
+ await this.stopAllSessions();
365
+ }
366
+ if (this.localSelenium) {
367
+ // remove listener
368
+ if (this.localSelenium.stderr) {
369
+ this.localSelenium.stderr.removeAllListeners('data');
370
+ (_a = this.localSelenium.stdout) === null || _a === void 0 ? void 0 : _a.removeAllListeners();
371
+ }
372
+ // Ensure all pipes are closed
373
+ (_b = this.localSelenium.stdout) === null || _b === void 0 ? void 0 : _b.destroy();
374
+ (_c = this.localSelenium.stderr) === null || _c === void 0 ? void 0 : _c.destroy();
375
+ (_d = this.localSelenium.stdin) === null || _d === void 0 ? void 0 : _d.destroy();
376
+ this.logger.debug(`Stopping local Selenium server (PID: ${this.localSelenium.pid})`);
377
+ // Try SIGTERM first
378
+ this.localSelenium.kill('SIGTERM');
379
+ // Wait for exit event with a timeout (ensures it does not hang forever)
380
+ const waitForExit = new Promise((resolve) => {
381
+ var _a;
382
+ (_a = this.localSelenium) === null || _a === void 0 ? void 0 : _a.once('exit', () => {
383
+ this.logger.debug('Selenium process exited.');
384
+ resolve();
394
385
  });
395
- // Wait for either normal exit or force kill
396
- yield Promise.race([waitForExit, forceKill]);
397
- this.localSelenium.removeAllListeners();
398
- this.logger.debug('Selenium process and all associated pipes closed.');
399
- }
400
- });
401
- }
402
- refresh(applicant) {
403
- return __awaiter(this, void 0, void 0, function* () {
404
- yield this.createClient(applicant);
405
- const client = this.getBrowserClient(applicant);
406
- return client.refresh();
407
- });
408
- }
409
- click(applicant, selector, options) {
410
- return __awaiter(this, void 0, void 0, function* () {
411
- yield this.createClient(applicant);
412
- const client = this.getBrowserClient(applicant);
413
- const element = yield client.$(selector);
414
- return options && Object.keys(options).length > 0
415
- ? element.click(options)
416
- : element.click();
417
- });
418
- }
419
- getSize(applicant, selector) {
420
- return __awaiter(this, void 0, void 0, function* () {
421
- yield this.createClient(applicant);
422
- const client = this.getBrowserClient(applicant);
423
- const element = yield client.$(selector);
424
- return element.getSize();
425
- });
386
+ });
387
+ // Force kill if not exiting within 3 seconds
388
+ const forceKill = new Promise((resolve) => {
389
+ setTimeout(() => {
390
+ if (this.localSelenium && !this.localSelenium.killed) {
391
+ this.logger.warn(`Selenium did not exit in time. Sending SIGKILL.`);
392
+ this.localSelenium.kill('SIGKILL');
393
+ }
394
+ resolve();
395
+ }, 3000);
396
+ });
397
+ // Wait for either normal exit or force kill
398
+ await Promise.race([waitForExit, forceKill]);
399
+ this.localSelenium.removeAllListeners();
400
+ this.logger.debug('Selenium process and all associated pipes closed.');
401
+ }
426
402
  }
427
- url(applicant, val) {
428
- return __awaiter(this, void 0, void 0, function* () {
429
- yield this.createClient(applicant);
430
- const client = this.getBrowserClient(applicant);
431
- if (!val) {
432
- return client.getUrl();
433
- }
434
- return client.url(val);
435
- });
403
+ async refresh(applicant) {
404
+ await this.createClient(applicant);
405
+ const client = this.getBrowserClient(applicant);
406
+ return client.refresh();
407
+ }
408
+ async click(applicant, selector, options) {
409
+ await this.createClient(applicant);
410
+ const client = this.getBrowserClient(applicant);
411
+ const element = await client.$(selector);
412
+ return options && Object.keys(options).length > 0
413
+ ? element.click(options)
414
+ : element.click();
415
+ }
416
+ async getSize(applicant, selector) {
417
+ await this.createClient(applicant);
418
+ const client = this.getBrowserClient(applicant);
419
+ const element = await client.$(selector);
420
+ return element.getSize();
421
+ }
422
+ async url(applicant, val) {
423
+ await this.createClient(applicant);
424
+ const client = this.getBrowserClient(applicant);
425
+ if (!val) {
426
+ return client.getUrl();
427
+ }
428
+ return client.url(val);
436
429
  }
437
430
  generateWinId() {
438
431
  this.incrementWinId++;
439
432
  return `window-${this.incrementWinId}`;
440
433
  }
441
- newWindow(applicant_1, val_1, windowName_1) {
442
- return __awaiter(this, arguments, void 0, function* (applicant, val, windowName, windowFeatures = {}) {
443
- yield this.createClient(applicant);
444
- const client = this.getBrowserClient(applicant);
445
- const args = stringifyWindowFeatures(windowFeatures);
446
- const newWindow = yield client.newWindow(val, {
447
- windowName: windowName || this.generateWinId(),
448
- windowFeatures: args,
449
- });
450
- return (newWindow === null || newWindow === void 0 ? void 0 : newWindow.handle) || newWindow;
451
- });
452
- }
453
- waitForExist(applicant, xpath, timeout) {
454
- return __awaiter(this, void 0, void 0, function* () {
455
- yield this.createClient(applicant);
456
- const client = this.getBrowserClient(applicant);
457
- const selector = yield client.$(xpath);
458
- return selector.waitForExist({ timeout });
459
- });
460
- }
461
- waitForVisible(applicant, xpath, timeout) {
462
- return __awaiter(this, void 0, void 0, function* () {
463
- yield this.createClient(applicant);
464
- const client = this.getBrowserClient(applicant);
465
- const selector = yield client.$(xpath);
466
- return selector.waitForDisplayed({ timeout });
467
- });
468
- }
469
- isVisible(applicant, xpath) {
470
- return __awaiter(this, void 0, void 0, function* () {
471
- yield this.createClient(applicant);
472
- const client = this.getBrowserClient(applicant);
473
- const selector = yield client.$(xpath);
474
- return selector.isDisplayed();
475
- });
476
- }
477
- moveToObject(applicant_1, xpath_1) {
478
- return __awaiter(this, arguments, void 0, function* (applicant, xpath, xOffset = 0, yOffset = 0) {
479
- yield this.createClient(applicant);
480
- const client = this.getBrowserClient(applicant);
481
- const selector = yield client.$(xpath);
482
- return selector.moveTo({ xOffset, yOffset });
483
- });
484
- }
485
- execute(applicant, fn, args) {
486
- return __awaiter(this, void 0, void 0, function* () {
487
- yield this.createClient(applicant);
488
- const client = this.getBrowserClient(applicant);
489
- return client.execute(fn, ...args);
490
- });
491
- }
492
- executeAsync(applicant, fn, args) {
493
- return __awaiter(this, void 0, void 0, function* () {
494
- yield this.createClient(applicant);
495
- const client = this.getBrowserClient(applicant);
496
- return client.executeAsync(fn, ...args);
497
- });
498
- }
499
- getTitle(applicant) {
500
- return __awaiter(this, void 0, void 0, function* () {
501
- yield this.createClient(applicant);
502
- const client = this.getBrowserClient(applicant);
503
- return client.getTitle();
504
- });
505
- }
506
- clearValue(applicant, xpath) {
507
- return __awaiter(this, void 0, void 0, function* () {
508
- yield this.createClient(applicant);
509
- const client = this.getBrowserClient(applicant);
510
- const selector = yield client.$(xpath);
511
- return selector.clearValue();
512
- });
513
- }
514
- keys(applicant, value) {
515
- return __awaiter(this, void 0, void 0, function* () {
516
- yield this.createClient(applicant);
517
- const client = this.getBrowserClient(applicant);
518
- return client.keys(value);
519
- });
520
- }
521
- elementIdText(applicant, elementId) {
522
- return __awaiter(this, void 0, void 0, function* () {
523
- yield this.createClient(applicant);
524
- const client = this.getBrowserClient(applicant);
525
- return client.getElementText(elementId);
526
- });
527
- }
528
- elements(applicant, xpath) {
529
- return __awaiter(this, void 0, void 0, function* () {
530
- yield this.createClient(applicant);
531
- const client = this.getBrowserClient(applicant);
532
- const elements = (yield client.findElements('xpath', xpath));
533
- return elements.map((o) => {
534
- const keys = Object.keys(o);
535
- return { ELEMENT: o[keys[0]] };
536
- });
537
- });
538
- }
539
- frame(applicant, frameID) {
540
- return __awaiter(this, void 0, void 0, function* () {
541
- yield this.createClient(applicant);
542
- const client = this.getBrowserClient(applicant);
543
- return client.switchFrame(frameID);
544
- });
545
- }
546
- frameParent(applicant) {
547
- return __awaiter(this, void 0, void 0, function* () {
548
- yield this.createClient(applicant);
549
- const client = this.getBrowserClient(applicant);
550
- return client.switchToParentFrame();
551
- });
552
- }
553
- getValue(applicant, xpath) {
554
- return __awaiter(this, void 0, void 0, function* () {
555
- yield this.createClient(applicant);
556
- const client = this.getBrowserClient(applicant);
557
- const selector = yield client.$(xpath);
558
- return selector.getValue();
559
- });
560
- }
561
- setValue(applicant, xpath, value) {
562
- return __awaiter(this, void 0, void 0, function* () {
563
- yield this.createClient(applicant);
564
- const client = this.getBrowserClient(applicant);
565
- const selector = yield client.$(xpath);
566
- return selector.setValue(value);
567
- });
568
- }
569
- selectByIndex(applicant, xpath, value) {
570
- return __awaiter(this, void 0, void 0, function* () {
571
- yield this.createClient(applicant);
572
- const client = this.getBrowserClient(applicant);
573
- const selector = yield client.$(xpath);
574
- return selector.selectByIndex(value);
575
- });
576
- }
577
- selectByValue(applicant, xpath, value) {
578
- return __awaiter(this, void 0, void 0, function* () {
579
- yield this.createClient(applicant);
580
- const client = this.getBrowserClient(applicant);
581
- const selector = yield client.$(xpath);
582
- return selector.selectByAttribute('value', value);
583
- });
584
- }
585
- selectByVisibleText(applicant, xpath, str) {
586
- return __awaiter(this, void 0, void 0, function* () {
587
- yield this.createClient(applicant);
588
- const client = this.getBrowserClient(applicant);
589
- const selector = yield client.$(xpath);
590
- return selector.selectByVisibleText(str);
591
- });
592
- }
593
- getAttribute(applicant, xpath, attr) {
594
- return __awaiter(this, void 0, void 0, function* () {
595
- yield this.createClient(applicant);
596
- const client = this.getBrowserClient(applicant);
597
- const selector = yield client.$(xpath);
598
- return selector.getAttribute(attr);
599
- });
600
- }
601
- windowHandleMaximize(applicant) {
602
- return __awaiter(this, void 0, void 0, function* () {
603
- yield this.createClient(applicant);
604
- const client = this.getBrowserClient(applicant);
605
- return client.maximizeWindow();
606
- });
607
- }
608
- isEnabled(applicant, xpath) {
609
- return __awaiter(this, void 0, void 0, function* () {
610
- yield this.createClient(applicant);
611
- const client = this.getBrowserClient(applicant);
612
- const selector = yield client.$(xpath);
613
- return selector.isEnabled();
614
- });
615
- }
616
- scroll(applicant, xpath, xOffset, yOffset) {
617
- return __awaiter(this, void 0, void 0, function* () {
618
- yield this.createClient(applicant);
619
- const client = this.getBrowserClient(applicant);
620
- const element = yield client.$(xpath);
621
- yield element.scrollIntoView();
622
- return element.moveTo({ xOffset, yOffset });
623
- });
624
- }
625
- scrollIntoView(applicant, xpath, scrollIntoViewOptions) {
626
- return __awaiter(this, void 0, void 0, function* () {
627
- yield this.createClient(applicant);
628
- const client = this.getBrowserClient(applicant);
629
- const element = yield client.$(xpath);
630
- yield element.scrollIntoView(scrollIntoViewOptions !== null ? scrollIntoViewOptions : undefined);
631
- });
632
- }
633
- isAlertOpen(applicant) {
634
- return __awaiter(this, void 0, void 0, function* () {
635
- yield this.createClient(applicant);
636
- const client = this.getBrowserClient(applicant);
637
- return client.isAlertOpen();
638
- });
639
- }
640
- alertAccept(applicant) {
641
- return __awaiter(this, void 0, void 0, function* () {
642
- yield this.createClient(applicant);
643
- const client = this.getBrowserClient(applicant);
644
- if (yield this.isAlertOpen(applicant)) {
645
- return client.acceptAlert();
646
- }
647
- throw Error('There is no open alert');
648
- });
434
+ async newWindow(applicant, val, windowName, windowFeatures = {}) {
435
+ await this.createClient(applicant);
436
+ const client = this.getBrowserClient(applicant);
437
+ const args = stringifyWindowFeatures(windowFeatures);
438
+ const newWindow = await client.newWindow(val, {
439
+ windowName: windowName || this.generateWinId(),
440
+ windowFeatures: args,
441
+ });
442
+ return (newWindow === null || newWindow === void 0 ? void 0 : newWindow.handle) || newWindow;
443
+ }
444
+ async waitForExist(applicant, xpath, timeout) {
445
+ await this.createClient(applicant);
446
+ const client = this.getBrowserClient(applicant);
447
+ const selector = await client.$(xpath);
448
+ return selector.waitForExist({ timeout });
449
+ }
450
+ async waitForVisible(applicant, xpath, timeout) {
451
+ await this.createClient(applicant);
452
+ const client = this.getBrowserClient(applicant);
453
+ const selector = await client.$(xpath);
454
+ return selector.waitForDisplayed({ timeout });
455
+ }
456
+ async isVisible(applicant, xpath) {
457
+ await this.createClient(applicant);
458
+ const client = this.getBrowserClient(applicant);
459
+ const selector = await client.$(xpath);
460
+ return selector.isDisplayed();
461
+ }
462
+ async moveToObject(applicant, xpath, xOffset = 0, yOffset = 0) {
463
+ await this.createClient(applicant);
464
+ const client = this.getBrowserClient(applicant);
465
+ const selector = await client.$(xpath);
466
+ return selector.moveTo({ xOffset, yOffset });
467
+ }
468
+ async execute(applicant, fn, args) {
469
+ await this.createClient(applicant);
470
+ const client = this.getBrowserClient(applicant);
471
+ return client.execute(fn, ...args);
472
+ }
473
+ async executeAsync(applicant, fn, args) {
474
+ await this.createClient(applicant);
475
+ const client = this.getBrowserClient(applicant);
476
+ return client.executeAsync(fn, ...args);
477
+ }
478
+ async getTitle(applicant) {
479
+ await this.createClient(applicant);
480
+ const client = this.getBrowserClient(applicant);
481
+ return client.getTitle();
482
+ }
483
+ async clearValue(applicant, xpath) {
484
+ await this.createClient(applicant);
485
+ const client = this.getBrowserClient(applicant);
486
+ const selector = await client.$(xpath);
487
+ return selector.clearValue();
488
+ }
489
+ async keys(applicant, value) {
490
+ await this.createClient(applicant);
491
+ const client = this.getBrowserClient(applicant);
492
+ return client.keys(value);
493
+ }
494
+ async elementIdText(applicant, elementId) {
495
+ await this.createClient(applicant);
496
+ const client = this.getBrowserClient(applicant);
497
+ return client.getElementText(elementId);
498
+ }
499
+ async elements(applicant, xpath) {
500
+ await this.createClient(applicant);
501
+ const client = this.getBrowserClient(applicant);
502
+ const elements = (await client.findElements('xpath', xpath));
503
+ return elements.map((o) => {
504
+ const keys = Object.keys(o);
505
+ const firstKey = keys[0];
506
+ if (firstKey === undefined) {
507
+ return { ELEMENT: '' };
508
+ }
509
+ return { ELEMENT: o[firstKey] };
510
+ });
511
+ }
512
+ async frame(applicant, frameID) {
513
+ await this.createClient(applicant);
514
+ const client = this.getBrowserClient(applicant);
515
+ return client.switchFrame(frameID);
516
+ }
517
+ async frameParent(applicant) {
518
+ await this.createClient(applicant);
519
+ const client = this.getBrowserClient(applicant);
520
+ return client.switchToParentFrame();
521
+ }
522
+ async getValue(applicant, xpath) {
523
+ await this.createClient(applicant);
524
+ const client = this.getBrowserClient(applicant);
525
+ const selector = await client.$(xpath);
526
+ return selector.getValue();
527
+ }
528
+ async setValue(applicant, xpath, value) {
529
+ await this.createClient(applicant);
530
+ const client = this.getBrowserClient(applicant);
531
+ const selector = await client.$(xpath);
532
+ return selector.setValue(value);
533
+ }
534
+ async selectByIndex(applicant, xpath, value) {
535
+ await this.createClient(applicant);
536
+ const client = this.getBrowserClient(applicant);
537
+ const selector = await client.$(xpath);
538
+ return selector.selectByIndex(value);
539
+ }
540
+ async selectByValue(applicant, xpath, value) {
541
+ await this.createClient(applicant);
542
+ const client = this.getBrowserClient(applicant);
543
+ const selector = await client.$(xpath);
544
+ return selector.selectByAttribute('value', value);
545
+ }
546
+ async selectByVisibleText(applicant, xpath, str) {
547
+ await this.createClient(applicant);
548
+ const client = this.getBrowserClient(applicant);
549
+ const selector = await client.$(xpath);
550
+ return selector.selectByVisibleText(str);
551
+ }
552
+ async getAttribute(applicant, xpath, attr) {
553
+ await this.createClient(applicant);
554
+ const client = this.getBrowserClient(applicant);
555
+ const selector = await client.$(xpath);
556
+ return selector.getAttribute(attr);
557
+ }
558
+ async windowHandleMaximize(applicant) {
559
+ await this.createClient(applicant);
560
+ const client = this.getBrowserClient(applicant);
561
+ return client.maximizeWindow();
562
+ }
563
+ async isEnabled(applicant, xpath) {
564
+ await this.createClient(applicant);
565
+ const client = this.getBrowserClient(applicant);
566
+ const selector = await client.$(xpath);
567
+ return selector.isEnabled();
568
+ }
569
+ async scroll(applicant, xpath, xOffset, yOffset) {
570
+ await this.createClient(applicant);
571
+ const client = this.getBrowserClient(applicant);
572
+ const element = await client.$(xpath);
573
+ await element.scrollIntoView();
574
+ return element.moveTo({ xOffset, yOffset });
575
+ }
576
+ async scrollIntoView(applicant, xpath, scrollIntoViewOptions) {
577
+ await this.createClient(applicant);
578
+ const client = this.getBrowserClient(applicant);
579
+ const element = await client.$(xpath);
580
+ await element.scrollIntoView(scrollIntoViewOptions !== null ? scrollIntoViewOptions : undefined);
581
+ }
582
+ async isAlertOpen(applicant) {
583
+ await this.createClient(applicant);
584
+ const client = this.getBrowserClient(applicant);
585
+ return client.isAlertOpen();
586
+ }
587
+ async alertAccept(applicant) {
588
+ await this.createClient(applicant);
589
+ const client = this.getBrowserClient(applicant);
590
+ if (await this.isAlertOpen(applicant)) {
591
+ return client.acceptAlert();
592
+ }
593
+ throw Error('There is no open alert');
649
594
  }
650
- alertDismiss(applicant) {
651
- return __awaiter(this, void 0, void 0, function* () {
652
- yield this.createClient(applicant);
653
- const client = this.getBrowserClient(applicant);
654
- if (yield this.isAlertOpen(applicant)) {
655
- return client.dismissAlert();
656
- }
657
- throw Error('There is no open alert');
658
- });
595
+ async alertDismiss(applicant) {
596
+ await this.createClient(applicant);
597
+ const client = this.getBrowserClient(applicant);
598
+ if (await this.isAlertOpen(applicant)) {
599
+ return client.dismissAlert();
600
+ }
601
+ throw Error('There is no open alert');
659
602
  }
660
- alertText(applicant) {
661
- return __awaiter(this, void 0, void 0, function* () {
662
- yield this.createClient(applicant);
663
- const client = this.getBrowserClient(applicant);
664
- if (yield this.isAlertOpen(applicant)) {
665
- return client.getAlertText();
603
+ async alertText(applicant) {
604
+ await this.createClient(applicant);
605
+ const client = this.getBrowserClient(applicant);
606
+ if (await this.isAlertOpen(applicant)) {
607
+ return client.getAlertText();
608
+ }
609
+ throw Error('There is no open alert');
610
+ }
611
+ async dragAndDrop(applicant, xpathSource, xpathDestination) {
612
+ await this.createClient(applicant);
613
+ const client = this.getBrowserClient(applicant);
614
+ const sourceElement = await client.$(xpathSource);
615
+ const destinationElement = await client.$(xpathDestination);
616
+ return sourceElement.dragAndDrop(destinationElement);
617
+ }
618
+ async setCookie(applicant, cookieObj) {
619
+ await this.createClient(applicant);
620
+ const client = this.getBrowserClient(applicant);
621
+ return await client.setCookies(cookieObj);
622
+ }
623
+ async getCookie(applicant, cookieName) {
624
+ var _a;
625
+ await this.createClient(applicant);
626
+ const client = this.getBrowserClient(applicant);
627
+ if (cookieName) {
628
+ try {
629
+ const cookies = await client.getCookies([cookieName]);
630
+ return (_a = cookies[0]) === null || _a === void 0 ? void 0 : _a.value;
666
631
  }
667
- throw Error('There is no open alert');
668
- });
669
- }
670
- dragAndDrop(applicant, xpathSource, xpathDestination) {
671
- return __awaiter(this, void 0, void 0, function* () {
672
- yield this.createClient(applicant);
673
- const client = this.getBrowserClient(applicant);
674
- const sourceElement = yield client.$(xpathSource);
675
- const destinationElement = yield client.$(xpathDestination);
676
- return sourceElement.dragAndDrop(destinationElement);
677
- });
678
- }
679
- setCookie(applicant, cookieObj) {
680
- return __awaiter(this, void 0, void 0, function* () {
681
- yield this.createClient(applicant);
682
- const client = this.getBrowserClient(applicant);
683
- return yield client.setCookies(cookieObj);
684
- });
685
- }
686
- getCookie(applicant, cookieName) {
687
- return __awaiter(this, void 0, void 0, function* () {
688
- var _a;
689
- yield this.createClient(applicant);
690
- const client = this.getBrowserClient(applicant);
691
- if (cookieName) {
692
- try {
693
- const cookies = yield client.getCookies([cookieName]);
694
- return (_a = cookies[0]) === null || _a === void 0 ? void 0 : _a.value;
695
- }
696
- catch (e) {
697
- return undefined;
698
- }
632
+ catch (e) {
633
+ return undefined;
699
634
  }
700
- return client.getAllCookies();
701
- });
635
+ }
636
+ return client.getAllCookies();
702
637
  }
703
- deleteCookie(applicant, cookieName) {
704
- return __awaiter(this, void 0, void 0, function* () {
705
- yield this.createClient(applicant);
706
- const client = this.getBrowserClient(applicant);
707
- if (cookieName) {
708
- return client.deleteCookie(cookieName);
709
- }
710
- return client.deleteAllCookies();
711
- });
638
+ async deleteCookie(applicant, cookieName) {
639
+ await this.createClient(applicant);
640
+ const client = this.getBrowserClient(applicant);
641
+ if (cookieName) {
642
+ return client.deleteCookie(cookieName);
643
+ }
644
+ return client.deleteAllCookies();
712
645
  }
713
- getHTML(applicant, xpath, b) {
714
- return __awaiter(this, void 0, void 0, function* () {
715
- yield this.createClient(applicant);
716
- const client = this.getBrowserClient(applicant);
717
- const selector = yield client.$(xpath);
718
- return selector.getHTML(b);
719
- });
646
+ async getHTML(applicant, xpath, b) {
647
+ await this.createClient(applicant);
648
+ const client = this.getBrowserClient(applicant);
649
+ const selector = await client.$(xpath);
650
+ return selector.getHTML(b);
720
651
  }
721
- getCurrentTabId(applicant) {
722
- return __awaiter(this, void 0, void 0, function* () {
723
- yield this.createClient(applicant);
724
- const client = this.getBrowserClient(applicant);
725
- return client.getWindowHandle();
726
- });
652
+ async getCurrentTabId(applicant) {
653
+ await this.createClient(applicant);
654
+ const client = this.getBrowserClient(applicant);
655
+ return client.getWindowHandle();
727
656
  }
728
- getTabIds(applicant) {
729
- return __awaiter(this, void 0, void 0, function* () {
730
- yield this.createClient(applicant);
731
- const client = this.getBrowserClient(applicant);
732
- return client.getWindowHandles();
733
- });
657
+ async getTabIds(applicant) {
658
+ await this.createClient(applicant);
659
+ const client = this.getBrowserClient(applicant);
660
+ return client.getWindowHandles();
734
661
  }
735
662
  // @deprecated
736
- windowHandles(applicant) {
737
- return __awaiter(this, void 0, void 0, function* () {
738
- return this.getTabIds(applicant);
739
- });
740
- }
741
- window(applicant, tabId) {
742
- return __awaiter(this, void 0, void 0, function* () {
743
- yield this.createClient(applicant);
744
- const client = this.getBrowserClient(applicant);
745
- return client.switchToWindow(tabId);
746
- });
747
- }
748
- switchTab(applicant, tabId) {
749
- return __awaiter(this, void 0, void 0, function* () {
750
- yield this.createClient(applicant);
751
- const client = this.getBrowserClient(applicant);
752
- const result = yield client.switchToWindow(tabId);
753
- const body = yield client.$('body');
754
- yield client.waitUntil(() => __awaiter(this, void 0, void 0, function* () { return body.isExisting(); }), { timeout: 10000 });
755
- return result;
756
- });
757
- }
758
- close(applicant, tabId) {
759
- return __awaiter(this, void 0, void 0, function* () {
760
- yield this.createClient(applicant);
761
- const client = this.getBrowserClient(applicant);
762
- const tabs = yield this.getTabIds(applicant);
763
- if (tabs.length === 1 && tabs[0] === tabId) {
764
- return this.end(applicant);
765
- }
766
- yield client.switchToWindow(tabId);
767
- return client.closeWindow();
768
- });
769
- }
770
- getTagName(applicant, xpath) {
771
- return __awaiter(this, void 0, void 0, function* () {
772
- yield this.createClient(applicant);
773
- const client = this.getBrowserClient(applicant);
774
- const selector = yield client.$(xpath);
775
- return selector.getTagName();
776
- });
777
- }
778
- isSelected(applicant, xpath) {
779
- return __awaiter(this, void 0, void 0, function* () {
780
- yield this.createClient(applicant);
781
- const client = this.getBrowserClient(applicant);
782
- const selector = yield client.$(xpath);
783
- return selector.isSelected();
784
- });
785
- }
786
- getText(applicant, xpath) {
787
- return __awaiter(this, void 0, void 0, function* () {
788
- yield this.createClient(applicant);
789
- const client = this.getBrowserClient(applicant);
790
- const selector = yield client.$(xpath);
791
- return selector.getText();
792
- });
793
- }
794
- elementIdSelected(applicant, id) {
795
- return __awaiter(this, void 0, void 0, function* () {
796
- yield this.createClient(applicant);
797
- const client = this.getBrowserClient(applicant);
798
- return client.isElementSelected(id);
799
- });
800
- }
801
- makeScreenshot(applicant) {
802
- return __awaiter(this, void 0, void 0, function* () {
803
- yield this.createClient(applicant);
804
- const client = this.getBrowserClient(applicant);
805
- return client.takeScreenshot();
806
- });
807
- }
808
- uploadFile(applicant, filePath) {
809
- return __awaiter(this, void 0, void 0, function* () {
810
- yield this.createClient(applicant);
811
- const client = this.getBrowserClient(applicant);
812
- return client.uploadFile(filePath);
813
- });
814
- }
815
- getCssProperty(applicant, xpath, cssProperty) {
816
- return __awaiter(this, void 0, void 0, function* () {
817
- yield this.createClient(applicant);
818
- const client = this.getBrowserClient(applicant);
819
- const element = yield client.$(xpath);
820
- const property = yield element.getCSSProperty(cssProperty);
821
- return property.value;
822
- });
823
- }
824
- getSource(applicant) {
825
- return __awaiter(this, void 0, void 0, function* () {
826
- yield this.createClient(applicant);
827
- const client = this.getBrowserClient(applicant);
828
- return client.getPageSource();
829
- });
830
- }
831
- isExisting(applicant, xpath) {
832
- return __awaiter(this, void 0, void 0, function* () {
833
- yield this.createClient(applicant);
834
- const client = this.getBrowserClient(applicant);
835
- const selector = yield client.$(xpath);
836
- return selector.isExisting();
837
- });
838
- }
839
- waitForValue(applicant, xpath, timeout, reverse) {
840
- return __awaiter(this, void 0, void 0, function* () {
841
- yield this.createClient(applicant);
842
- const client = this.getBrowserClient(applicant);
843
- return client.waitUntil(() => __awaiter(this, void 0, void 0, function* () {
844
- const elemValue = yield (yield client.$(xpath)).getValue();
845
- return reverse ? !elemValue : !!elemValue;
846
- }), { timeout });
847
- });
848
- }
849
- waitForSelected(applicant, xpath, timeout, reverse) {
850
- return __awaiter(this, void 0, void 0, function* () {
851
- yield this.createClient(applicant);
852
- const client = this.getBrowserClient(applicant);
853
- return client.waitUntil(() => __awaiter(this, void 0, void 0, function* () {
854
- const isSelected = yield (yield client.$(xpath)).isSelected();
855
- return reverse ? !isSelected : isSelected;
856
- }), { timeout });
857
- });
858
- }
859
- waitUntil(applicant, condition, timeout, timeoutMsg, interval) {
860
- return __awaiter(this, void 0, void 0, function* () {
861
- yield this.createClient(applicant);
862
- const client = this.getBrowserClient(applicant);
863
- return client.waitUntil(condition, { timeout, timeoutMsg, interval });
864
- });
865
- }
866
- selectByAttribute(applicant, xpath, attribute, value) {
867
- return __awaiter(this, void 0, void 0, function* () {
868
- yield this.createClient(applicant);
869
- const client = this.getBrowserClient(applicant);
870
- const selector = yield client.$(xpath);
871
- return selector.selectByAttribute(attribute, value);
872
- });
873
- }
874
- gridTestSession(applicant) {
875
- return __awaiter(this, void 0, void 0, function* () {
876
- yield this.createClient(applicant);
877
- const client = this.getBrowserClient(applicant);
878
- if (this.localSelenium) {
879
- return {
880
- sessionId: client.sessionId,
881
- host: this.config.host,
882
- port: this.config.port,
883
- localSelenium: true,
884
- };
885
- }
886
- return client.gridTestSession(client.sessionId);
887
- });
888
- }
889
- getHubConfig(applicant) {
890
- return __awaiter(this, void 0, void 0, function* () {
891
- yield this.createClient(applicant);
892
- const client = this.getBrowserClient(applicant);
893
- if (this.localSelenium) {
894
- return {
895
- sessionId: client.sessionId,
896
- host: this.config.host,
897
- port: this.config.port,
898
- localSelenium: true,
899
- };
900
- }
901
- return client.getHubConfig();
902
- });
663
+ async windowHandles(applicant) {
664
+ return this.getTabIds(applicant);
665
+ }
666
+ async window(applicant, tabId) {
667
+ await this.createClient(applicant);
668
+ const client = this.getBrowserClient(applicant);
669
+ return client.switchToWindow(tabId);
670
+ }
671
+ async switchTab(applicant, tabId) {
672
+ await this.createClient(applicant);
673
+ const client = this.getBrowserClient(applicant);
674
+ const result = await client.switchToWindow(tabId);
675
+ const body = await client.$('body');
676
+ await client.waitUntil(async () => body.isExisting(), { timeout: 10000 });
677
+ return result;
678
+ }
679
+ async close(applicant, tabId) {
680
+ await this.createClient(applicant);
681
+ const client = this.getBrowserClient(applicant);
682
+ const tabs = await this.getTabIds(applicant);
683
+ if (tabs.length === 1 && tabs[0] === tabId) {
684
+ return this.end(applicant);
685
+ }
686
+ await client.switchToWindow(tabId);
687
+ return client.closeWindow();
688
+ }
689
+ async getTagName(applicant, xpath) {
690
+ await this.createClient(applicant);
691
+ const client = this.getBrowserClient(applicant);
692
+ const selector = await client.$(xpath);
693
+ return selector.getTagName();
694
+ }
695
+ async isSelected(applicant, xpath) {
696
+ await this.createClient(applicant);
697
+ const client = this.getBrowserClient(applicant);
698
+ const selector = await client.$(xpath);
699
+ return selector.isSelected();
700
+ }
701
+ async getText(applicant, xpath) {
702
+ await this.createClient(applicant);
703
+ const client = this.getBrowserClient(applicant);
704
+ const selector = await client.$(xpath);
705
+ return selector.getText();
706
+ }
707
+ async elementIdSelected(applicant, id) {
708
+ await this.createClient(applicant);
709
+ const client = this.getBrowserClient(applicant);
710
+ return client.isElementSelected(id);
711
+ }
712
+ async makeScreenshot(applicant) {
713
+ await this.createClient(applicant);
714
+ const client = this.getBrowserClient(applicant);
715
+ return client.takeScreenshot();
716
+ }
717
+ async uploadFile(applicant, filePath) {
718
+ await this.createClient(applicant);
719
+ const client = this.getBrowserClient(applicant);
720
+ return client.uploadFile(filePath);
721
+ }
722
+ async getCssProperty(applicant, xpath, cssProperty) {
723
+ await this.createClient(applicant);
724
+ const client = this.getBrowserClient(applicant);
725
+ const element = await client.$(xpath);
726
+ const property = await element.getCSSProperty(cssProperty);
727
+ return property.value;
728
+ }
729
+ async getSource(applicant) {
730
+ await this.createClient(applicant);
731
+ const client = this.getBrowserClient(applicant);
732
+ return client.getPageSource();
733
+ }
734
+ async isExisting(applicant, xpath) {
735
+ await this.createClient(applicant);
736
+ const client = this.getBrowserClient(applicant);
737
+ const selector = await client.$(xpath);
738
+ return selector.isExisting();
739
+ }
740
+ async waitForValue(applicant, xpath, timeout, reverse) {
741
+ await this.createClient(applicant);
742
+ const client = this.getBrowserClient(applicant);
743
+ return client.waitUntil(async () => {
744
+ const elemValue = await (await client.$(xpath)).getValue();
745
+ return reverse ? !elemValue : !!elemValue;
746
+ }, { timeout });
747
+ }
748
+ async waitForSelected(applicant, xpath, timeout, reverse) {
749
+ await this.createClient(applicant);
750
+ const client = this.getBrowserClient(applicant);
751
+ return client.waitUntil(async () => {
752
+ const isSelected = await (await client.$(xpath)).isSelected();
753
+ return reverse ? !isSelected : isSelected;
754
+ }, { timeout });
755
+ }
756
+ async waitUntil(applicant, condition, timeout, timeoutMsg, interval) {
757
+ await this.createClient(applicant);
758
+ const client = this.getBrowserClient(applicant);
759
+ const options = {
760
+ timeout: timeout || 5000,
761
+ };
762
+ if (timeoutMsg !== undefined) {
763
+ options.timeoutMsg = timeoutMsg;
764
+ }
765
+ if (interval !== undefined) {
766
+ options.interval = interval;
767
+ }
768
+ return client.waitUntil(condition, options);
769
+ }
770
+ async selectByAttribute(applicant, xpath, attribute, value) {
771
+ await this.createClient(applicant);
772
+ const client = this.getBrowserClient(applicant);
773
+ const selector = await client.$(xpath);
774
+ return selector.selectByAttribute(attribute, value);
775
+ }
776
+ async gridTestSession(applicant) {
777
+ await this.createClient(applicant);
778
+ const client = this.getBrowserClient(applicant);
779
+ if (this.localSelenium) {
780
+ return {
781
+ sessionId: client.sessionId,
782
+ host: this.config.host,
783
+ port: this.config.port,
784
+ localSelenium: true,
785
+ };
786
+ }
787
+ return client.gridTestSession(client.sessionId);
788
+ }
789
+ async getHubConfig(applicant) {
790
+ await this.createClient(applicant);
791
+ const client = this.getBrowserClient(applicant);
792
+ if (this.localSelenium) {
793
+ return {
794
+ sessionId: client.sessionId,
795
+ host: this.config.host,
796
+ port: this.config.port,
797
+ localSelenium: true,
798
+ };
799
+ }
800
+ return client.getHubConfig();
903
801
  }
904
802
  /**
905
803
  * @param overwrites should NOT be an arrow function, Otherwise it would throw an error
906
804
  */
907
- mock(applicant, url, overwrites, filterOptions, mockResponseParams) {
908
- return __awaiter(this, void 0, void 0, function* () {
909
- yield this.createClient(applicant);
910
- const client = this.getBrowserClient(applicant);
911
- const mock = yield client.mock(url, filterOptions);
912
- mock.respond(overwrites, mockResponseParams);
913
- });
914
- }
915
- emulateDevice(applicant, deviceName) {
916
- return __awaiter(this, void 0, void 0, function* () {
917
- yield this.createClient(applicant);
918
- const client = this.getBrowserClient(applicant);
919
- yield client.deleteSession();
920
- this.browserClients.delete(applicant);
921
- yield this.createClient(applicant, {
922
- capabilities: {
923
- 'goog:chromeOptions': {
924
- mobileEmulation: {
925
- deviceName,
926
- },
805
+ async mock(applicant, url, overwrites, filterOptions, mockResponseParams) {
806
+ await this.createClient(applicant);
807
+ const client = this.getBrowserClient(applicant);
808
+ const mock = await client.mock(url, filterOptions);
809
+ mock.respond(overwrites, mockResponseParams);
810
+ }
811
+ async emulateDevice(applicant, deviceName) {
812
+ await this.createClient(applicant);
813
+ const client = this.getBrowserClient(applicant);
814
+ await client.deleteSession();
815
+ this.browserClients.delete(applicant);
816
+ this.customBrowserClientsConfigs.delete(applicant);
817
+ await this.createClient(applicant, {
818
+ capabilities: {
819
+ 'goog:chromeOptions': {
820
+ mobileEmulation: {
821
+ deviceName,
927
822
  },
928
823
  },
929
- });
930
- });
931
- }
932
- status(applicant) {
933
- return __awaiter(this, void 0, void 0, function* () {
934
- yield this.createClient(applicant);
935
- const client = this.getBrowserClient(applicant);
936
- return client.status();
937
- });
938
- }
939
- back(applicant) {
940
- return __awaiter(this, void 0, void 0, function* () {
941
- yield this.createClient(applicant);
942
- const client = this.getBrowserClient(applicant);
943
- return client.back();
944
- });
945
- }
946
- forward(applicant) {
947
- return __awaiter(this, void 0, void 0, function* () {
948
- yield this.createClient(applicant);
949
- const client = this.getBrowserClient(applicant);
950
- return client.forward();
951
- });
952
- }
953
- getActiveElement(applicant) {
954
- return __awaiter(this, void 0, void 0, function* () {
955
- yield this.createClient(applicant);
956
- const client = this.getBrowserClient(applicant);
957
- return client.getActiveElement();
958
- });
959
- }
960
- getLocation(applicant, xpath) {
961
- return __awaiter(this, void 0, void 0, function* () {
962
- yield this.createClient(applicant);
963
- const client = this.getBrowserClient(applicant);
964
- const element = client.$(xpath);
965
- return element.getLocation();
966
- });
967
- }
968
- setTimeZone(applicant, timeZone) {
969
- return __awaiter(this, void 0, void 0, function* () {
970
- yield this.createClient(applicant);
971
- const client = this.getBrowserClient(applicant);
972
- return client.setTimeZone(timeZone);
973
- });
974
- }
975
- getWindowSize(applicant) {
976
- return __awaiter(this, void 0, void 0, function* () {
977
- yield this.createClient(applicant);
978
- const client = this.getBrowserClient(applicant);
979
- return client.getWindowSize();
980
- });
981
- }
982
- savePDF(applicant, options) {
983
- return __awaiter(this, void 0, void 0, function* () {
984
- yield this.createClient(applicant);
985
- const client = this.getBrowserClient(applicant);
986
- const { filepath } = options, restOptions = __rest(options, ["filepath"]);
987
- return client.savePDF(filepath, restOptions);
988
- });
989
- }
990
- addValue(applicant, xpath, value) {
991
- return __awaiter(this, void 0, void 0, function* () {
992
- yield this.createClient(applicant);
993
- const client = this.getBrowserClient(applicant);
994
- const selector = yield client.$(xpath);
995
- return selector.addValue(value);
996
- });
997
- }
998
- doubleClick(applicant, xpath) {
999
- return __awaiter(this, void 0, void 0, function* () {
1000
- yield this.createClient(applicant);
1001
- const client = this.getBrowserClient(applicant);
1002
- const selector = yield client.$(xpath);
1003
- return selector.doubleClick();
1004
- });
1005
- }
1006
- isClickable(applicant, xpath) {
1007
- return __awaiter(this, void 0, void 0, function* () {
1008
- yield this.createClient(applicant);
1009
- const client = this.getBrowserClient(applicant);
1010
- const selector = yield client.$(xpath);
1011
- return selector.isClickable();
1012
- });
1013
- }
1014
- waitForClickable(applicant, xpath, timeout) {
1015
- return __awaiter(this, void 0, void 0, function* () {
1016
- yield this.createClient(applicant);
1017
- const client = this.getBrowserClient(applicant);
1018
- const selector = yield client.$(xpath);
1019
- return selector.waitForClickable({ timeout });
1020
- });
1021
- }
1022
- isFocused(applicant, xpath) {
1023
- return __awaiter(this, void 0, void 0, function* () {
1024
- yield this.createClient(applicant);
1025
- const client = this.getBrowserClient(applicant);
1026
- const selector = yield client.$(xpath);
1027
- return selector.isFocused();
1028
- });
1029
- }
1030
- isStable(applicant, xpath) {
1031
- return __awaiter(this, void 0, void 0, function* () {
1032
- yield this.createClient(applicant);
1033
- const client = this.getBrowserClient(applicant);
1034
- const selector = yield client.$(xpath);
1035
- return selector.isStable();
1036
- });
1037
- }
1038
- waitForEnabled(applicant, xpath, timeout) {
1039
- return __awaiter(this, void 0, void 0, function* () {
1040
- yield this.createClient(applicant);
1041
- const client = this.getBrowserClient(applicant);
1042
- const selector = yield client.$(xpath);
1043
- return selector.waitForEnabled({ timeout });
1044
- });
1045
- }
1046
- waitForStable(applicant, xpath, timeout) {
1047
- return __awaiter(this, void 0, void 0, function* () {
1048
- yield this.createClient(applicant);
1049
- const client = this.getBrowserClient(applicant);
1050
- const selector = yield client.$(xpath);
1051
- return selector.waitForStable({ timeout });
1052
- });
824
+ },
825
+ });
826
+ }
827
+ async status(applicant) {
828
+ await this.createClient(applicant);
829
+ const client = this.getBrowserClient(applicant);
830
+ return client.status();
831
+ }
832
+ async back(applicant) {
833
+ await this.createClient(applicant);
834
+ const client = this.getBrowserClient(applicant);
835
+ return client.back();
836
+ }
837
+ async forward(applicant) {
838
+ await this.createClient(applicant);
839
+ const client = this.getBrowserClient(applicant);
840
+ return client.forward();
841
+ }
842
+ async getActiveElement(applicant) {
843
+ await this.createClient(applicant);
844
+ const client = this.getBrowserClient(applicant);
845
+ return client.getActiveElement();
846
+ }
847
+ async getLocation(applicant, xpath) {
848
+ await this.createClient(applicant);
849
+ const client = this.getBrowserClient(applicant);
850
+ const element = client.$(xpath);
851
+ return element.getLocation();
852
+ }
853
+ async setTimeZone(applicant, timeZone) {
854
+ await this.createClient(applicant);
855
+ const client = this.getBrowserClient(applicant);
856
+ return client.setTimeZone(timeZone);
857
+ }
858
+ async getWindowSize(applicant) {
859
+ await this.createClient(applicant);
860
+ const client = this.getBrowserClient(applicant);
861
+ return client.getWindowSize();
862
+ }
863
+ async savePDF(applicant, options) {
864
+ await this.createClient(applicant);
865
+ const client = this.getBrowserClient(applicant);
866
+ const { filepath, ...restOptions } = options;
867
+ return client.savePDF(filepath, restOptions);
868
+ }
869
+ async addValue(applicant, xpath, value) {
870
+ await this.createClient(applicant);
871
+ const client = this.getBrowserClient(applicant);
872
+ const selector = await client.$(xpath);
873
+ return selector.addValue(value);
874
+ }
875
+ async doubleClick(applicant, xpath) {
876
+ await this.createClient(applicant);
877
+ const client = this.getBrowserClient(applicant);
878
+ const selector = await client.$(xpath);
879
+ return selector.doubleClick();
880
+ }
881
+ async isClickable(applicant, xpath) {
882
+ await this.createClient(applicant);
883
+ const client = this.getBrowserClient(applicant);
884
+ const selector = await client.$(xpath);
885
+ return selector.isClickable();
886
+ }
887
+ async waitForClickable(applicant, xpath, timeout) {
888
+ await this.createClient(applicant);
889
+ const client = this.getBrowserClient(applicant);
890
+ const selector = await client.$(xpath);
891
+ return selector.waitForClickable({ timeout });
892
+ }
893
+ async isFocused(applicant, xpath) {
894
+ await this.createClient(applicant);
895
+ const client = this.getBrowserClient(applicant);
896
+ const selector = await client.$(xpath);
897
+ return selector.isFocused();
898
+ }
899
+ async isStable(applicant, xpath) {
900
+ await this.createClient(applicant);
901
+ const client = this.getBrowserClient(applicant);
902
+ const selector = await client.$(xpath);
903
+ return selector.isStable();
904
+ }
905
+ async waitForEnabled(applicant, xpath, timeout) {
906
+ await this.createClient(applicant);
907
+ const client = this.getBrowserClient(applicant);
908
+ const selector = await client.$(xpath);
909
+ return selector.waitForEnabled({ timeout });
910
+ }
911
+ async waitForStable(applicant, xpath, timeout) {
912
+ await this.createClient(applicant);
913
+ const client = this.getBrowserClient(applicant);
914
+ const selector = await client.$(xpath);
915
+ return selector.waitForStable({ timeout });
1053
916
  }
1054
917
  }
1055
918
  exports.SeleniumPlugin = SeleniumPlugin;