@testring/plugin-selenium-driver 0.7.6 → 0.8.1

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