UNPKG

@web/test-runner-webdriver

Version:
219 lines 7.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.webdriverLauncher = exports.WebdriverLauncher = void 0; const webdriverio_1 = require("webdriverio"); const IFrameManager_js_1 = require("./IFrameManager.js"); const SessionManager_js_1 = require("./SessionManager.js"); const utils_js_1 = require("./utils.js"); function getMouseButtonCode(button) { switch (button) { case 'left': return 0; case 'middle': return 1; case 'right': return 2; } } class WebdriverLauncher { constructor(options) { this.options = options; this.name = 'Initializing...'; this.type = 'webdriver'; this.debugDriver = undefined; this.isIE = false; } async initialize(config) { var _a; this.config = config; const cap = this.options.capabilities; this.name = (0, utils_js_1.getBrowserLabel)(cap); const browserName = ((_a = cap.browserName) === null || _a === void 0 ? void 0 : _a.toLowerCase().replace(/_/g, ' ')) || ''; this.isIE = (browserName.includes('internet') && browserName.includes('explorer')) || browserName === 'ie' || browserName === 'ie11'; } async stop() { var _a, _b; try { if (this.pendingHeartbeat != null) { clearInterval(this.pendingHeartbeat); } await ((_a = this.driver) === null || _a === void 0 ? void 0 : _a.deleteSession()); await ((_b = this.debugDriver) === null || _b === void 0 ? void 0 : _b.deleteSession()); this.driver = undefined; this.debugDriver = undefined; this.driverManager = undefined; } catch (_c) { // } } async startSession(id, url) { await this.ensureManagerInitialized(); return this.driverManager.queueStartSession(id, url); } isActive(id) { var _a; return !!((_a = this.driverManager) === null || _a === void 0 ? void 0 : _a.isActive(id)); } getBrowserUrl(sessionId) { if (!this.driverManager) { throw new Error('Not initialized'); } return this.driverManager.getBrowserUrl(sessionId); } async stopSession(id) { return this.driverManager.queueStopSession(id); } async startDebugSession(_, url) { if (this.debugDriver) { await this.debugDriver.deleteSession(); } this.debugDriver = (await (0, webdriverio_1.remote)(this.options)); await this.debugDriver.navigateTo(url); } async ensureManagerInitialized() { if (this.driverManager) { return; } if (this.__managerPromise) { await this.__managerPromise; return; } this.__managerPromise = this.createDriverManager(); await this.__managerPromise; this.__managerPromise = undefined; } async createDriverManager() { if (!this.config) throw new Error('Not initialized'); const options = Object.assign({ logLevel: 'error' }, this.options); try { this.driver = (await (0, webdriverio_1.remote)(options)); this.driverManager = this.config.concurrency === 1 ? new SessionManager_js_1.SessionManager(this.config, this.driver, this.isIE) : new IFrameManager_js_1.IFrameManager(this.config, this.driver, this.isIE); this.setupHeartbeat(); return this.driverManager; } catch (e) { this.stop(); throw e; } } /** * Sets up a heartbeat to avoid the session from expiring due to * inactivity because of a long running test. */ setupHeartbeat() { this.pendingHeartbeat = setInterval(async () => { if (!this.driver) return; try { await this.driver.getTitle(); } catch (e) { // Do nothing, just clear the timeout if (this.pendingHeartbeat != null) { clearInterval(this.pendingHeartbeat); } } }, 60000); } sendMouseMove(sessionId, x, y) { if (!this.driverManager) { throw new Error('Not initialized'); } return this.driverManager.performActions(sessionId, [ { type: 'pointer', id: 'finger1', actions: [{ type: 'pointerMove', duration: 0, x, y }], }, ]); } sendMouseClick(sessionId, x, y, button = 'left') { if (!this.driverManager) { throw new Error('Not initialized'); } const buttonCode = getMouseButtonCode(button); return this.driverManager.performActions(sessionId, [ { type: 'pointer', id: 'finger1', actions: [ { type: 'pointerMove', duration: 0, x, y }, { type: 'pointerDown', button: buttonCode }, { type: 'pointerUp', button: buttonCode }, ], }, ]); } sendMouseDown(sessionId, button = 'left') { if (!this.driverManager) { throw new Error('Not initialized'); } const buttonCode = getMouseButtonCode(button); return this.driverManager.performActions(sessionId, [ { type: 'pointer', id: 'finger1', actions: [{ type: 'pointerDown', button: buttonCode }], }, ]); } sendMouseUp(sessionId, button = 'left') { if (!this.driverManager) { throw new Error('Not initialized'); } const buttonCode = getMouseButtonCode(button); return this.driverManager.performActions(sessionId, [ { type: 'pointer', id: 'finger1', actions: [{ type: 'pointerUp', button: buttonCode }], }, ]); } resetMouse(sessionId) { if (!this.driverManager) { throw new Error('Not initialized'); } return this.driverManager.performActions(sessionId, [ { type: 'pointer', id: 'finger1', actions: [ { type: 'pointerUp', button: getMouseButtonCode('left') }, { type: 'pointerUp', button: getMouseButtonCode('middle') }, { type: 'pointerUp', button: getMouseButtonCode('right') }, { type: 'pointerMove', duration: 0, x: 0, y: 0 }, ], }, ]); } sendKeys(sessionId, keys) { if (!this.driverManager) { throw new Error('Not initialized'); } return this.driverManager.sendKeys(sessionId, keys); } takeScreenshot(sessionId, locator) { if (!this.driverManager) { throw new Error('Not initialized'); } return this.driverManager.takeScreenshot(sessionId, locator); } } exports.WebdriverLauncher = WebdriverLauncher; function webdriverLauncher(options) { if (!(options === null || options === void 0 ? void 0 : options.capabilities)) { throw new Error(`Webdriver launcher requires a capabilities property.`); } return new WebdriverLauncher(options); } exports.webdriverLauncher = webdriverLauncher; //# sourceMappingURL=webdriverLauncher.js.map