@web/test-runner-webdriver
Version:
Webdriver browser launcher for Web Test Runner
83 lines • 2.79 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SessionManager = void 0;
const coverage_1 = require("./coverage");
/**
* Manages tests to be executed in one session (concurrency: 1).
*/
class SessionManager {
constructor(config, driver, isIE) {
this.urlMap = new Map();
this.config = config;
this.driver = driver;
this.isIE = isIE;
}
isActive(id) {
return this.urlMap.has(id);
}
async getBrowserUrl(id) {
return this.urlMap.get(id);
}
async scheduleCommand(fn) {
if (!this.isIE) {
return fn();
}
while (this.locked) {
await this.locked;
}
const fnPromise = fn();
this.locked = fnPromise;
const result = await fnPromise;
this.locked = undefined;
return result;
}
async queueStartSession(id, url) {
this.scheduleCommand(() => this.startSession(id, url));
}
async startSession(id, url) {
this.urlMap.set(id, url);
await this.driver.navigateTo(url);
}
async queueStopSession(id) {
return this.scheduleCommand(() => this.stopSession(id));
}
async stopSession(id) {
// Retrieve test results from iframe. Note: IIFE is used to prevent
// WebdriverIO from crashing failure with Puppeteer (default mode):
// Error: Evaluation failed: SyntaxError: Illegal return statement
// See https://github.com/webdriverio/webdriverio/pull/4829
const returnValue = await this.driver.execute(`
return (function() {
return { testCoverage: window.__coverage__ };
})();
`);
if (!(0, coverage_1.validateBrowserResult)(returnValue)) {
throw new Error();
}
const { testCoverage } = returnValue;
// navigate to an empty page to kill any running code on the page
await this.driver.navigateTo('about:blank');
this.urlMap.delete(id);
return { testCoverage: this.config.coverage ? testCoverage : undefined };
}
async performActions(_, actions) {
return this.driver.performActions(actions);
}
async sendKeys(_, keys) {
return this.driver.keys(keys);
}
async takeScreenshot(_, locator) {
const elementData = (await this.driver.execute(locator, []));
const element = await this.driver.$(elementData);
let base64 = '';
try {
base64 = await this.driver.takeElementScreenshot(element.elementId);
}
catch (err) {
console.log('Failed to take a screenshot:', err);
}
return Buffer.from(base64, 'base64');
}
}
exports.SessionManager = SessionManager;
//# sourceMappingURL=SessionManager.js.map