UNPKG

skutter

Version:

Skutter: Opinionated BDD Browser based test framework

90 lines (89 loc) 3.49 kB
"use strict"; const Lazy = require("lazy.js"); const user_agent_reader_1 = require("../services/user-agent-reader"); class BrowserProvider { constructor(browser) { this.initialBrowser = null; this.initialBrowserId = null; this.featureBrowserCreated = false; this.featureBrowser = null; this.featureBrowserId = null; this.spawnedScenarioBrowser = []; this.scenarioBrowser = null; this.scenarioBrowserId = null; this.initialNew = false; this.initialBrowser = browser; this.initialNew = true; } get(feature, scenario) { let newBrowser = this.initialNew || false; this.initialNew = false; if (!this.featureBrowserCreated) { if (Lazy(feature.annotations).contains("freshbrowser")) { this.featureBrowserCreated = true; this.featureBrowser = this.initialBrowser.forkNewDriverInstance(false, true); newBrowser = true; } } if (Lazy(scenario.annotations).contains("freshbrowser")) { if (!Lazy(this.spawnedScenarioBrowser).contains(scenario.id)) { this.spawnedScenarioBrowser.push(scenario.id); this.scenarioBrowser = this.initialBrowser.forkNewDriverInstance(false, true); newBrowser = true; } } if (this.scenarioBrowser) { if (newBrowser) { return user_agent_reader_1.UserAgentReader.read(this.scenarioBrowser) .then((userAgent) => { this.scenarioBrowserId = userAgent; }) .then(() => { return { browser: this.scenarioBrowser, browserId: this.scenarioBrowserId }; }); } else { return Promise.resolve({ browser: this.scenarioBrowser, browserId: this.scenarioBrowserId }); } } if (this.featureBrowser) { if (newBrowser) { return user_agent_reader_1.UserAgentReader.read(this.featureBrowser) .then((userAgent) => { this.featureBrowserId = userAgent; }) .then(() => { return { browser: this.featureBrowser, browserId: this.featureBrowserId }; }); } else { return Promise.resolve({ browser: this.featureBrowser, browserId: this.featureBrowserId }); } } if (this.initialNew) { return user_agent_reader_1.UserAgentReader.read(this.featureBrowser) .then((userAgent) => { this.initialBrowserId = userAgent; }) .then(() => { return { browser: this.featureBrowser, browserId: this.initialBrowserId }; }); } else { return Promise.resolve({ browser: this.initialBrowser, browserId: this.initialBrowserId }); } } cleanScenario(scenarioId) { if (this.spawnedScenarioBrowser.indexOf(scenarioId) !== -1) { console.log("Killing Scenario specific browser"); this.scenarioBrowser.quit(); } } cleanFeature() { if (this.featureBrowserCreated) { console.log("Killing Feature specific browser"); this.featureBrowser.quit(); } } } exports.BrowserProvider = BrowserProvider;