playwright-elements
Version:
This is Playwright extension.
177 lines (176 loc) • 5.85 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BrowserInstance = exports.Context = exports.BrowserName = void 0;
exports.usePage = usePage;
const playwright_core_1 = require("playwright-core");
const async_hooks_1 = require("async_hooks");
const customUserPage = new async_hooks_1.AsyncLocalStorage();
async function usePage(page, callBack) {
return customUserPage.run(page, async () => {
return (await callBack());
});
}
var BrowserName;
(function (BrowserName) {
BrowserName["CHROMIUM"] = "chromium";
BrowserName["CHROME"] = "chrome";
BrowserName["CHROME_BETA"] = "chrome-beta";
BrowserName["FIREFOX"] = "firefox";
BrowserName["WEBKIT"] = "webkit";
BrowserName["MSEDGE"] = "msedge";
BrowserName["MSEDGE_BETA"] = "msedge-beta";
BrowserName["MSEDGE_DEV"] = "msedge-dev";
})(BrowserName || (exports.BrowserName = BrowserName = {}));
class Context {
context;
_pages;
_previousPage;
_isMobile = false;
constructor(context) {
this.context = context;
this._pages = this.context.pages();
this.context.on('page', page => this._pages.push(page));
}
get get() {
return this.context;
}
get pages() {
return this._pages;
}
get previousPage() {
if (this._previousPage)
return this._previousPage;
throw new Error(`Previous page was not initialized.`);
}
set previousPage(page) {
this._previousPage = page;
}
get isMobile() {
return this._isMobile;
}
set isMobile(isMobile) {
this._isMobile = isMobile;
}
}
exports.Context = Context;
class BrowserInstance {
static browserName;
static _browser;
static _currentContext;
static _currentPage;
// eslint-disable-next-line @typescript-eslint/no-empty-function
constructor() {
}
static get isContextMobile() {
return this.context.isMobile;
}
static set isContextMobile(isMobile) {
this.context.isMobile = isMobile;
}
static get currentPage() {
const customPage = customUserPage.getStore();
if (customPage)
return customPage;
if (this._currentPage)
return this._currentPage;
throw new Error(`Page was not started`);
}
static set currentPage(page) {
this._currentPage = page;
}
static withPage(page) {
this.currentPage = page;
this.withContext(page.context());
}
static get context() {
if (this._currentContext)
return this._currentContext;
throw new Error(`Context was not started`);
}
static get currentContext() {
return this.context.get;
}
static set currentContext(context) {
if (context)
this._currentContext = new Context(context);
else
this._currentContext = context;
}
static withContext(context) {
this.currentContext = context;
this.currentContext.on('page', page => {
if (this._currentPage)
this.context.previousPage = this.currentPage;
this.currentPage = page;
});
if (this._browser)
return;
const currentBrowser = context.browser();
if (currentBrowser)
this.browser = currentBrowser;
else
throw new Error(`Browser is undefined and 'context.browser()' returns null.`);
}
static get browser() {
if (this._browser)
return this._browser;
throw new Error(`Browser was not started`);
}
static set browser(browser) {
this._browser = browser;
}
static withBrowser(browser) {
this.browser = browser;
}
static async launch(browserName, options) {
this.browserName = browserName;
switch (browserName) {
case BrowserName.CHROME:
return await playwright_core_1.chromium.launch({ ...options, ...{ channel: 'chrome' } });
case BrowserName.MSEDGE:
return await playwright_core_1.chromium.launch({ ...options, ...{ channel: 'msedge' } });
case BrowserName.WEBKIT:
return await playwright_core_1.webkit.launch({ ...options });
case BrowserName.FIREFOX:
return await playwright_core_1.firefox.launch({ ...options });
default:
return playwright_core_1.chromium.launch({ ...options });
}
}
static async start(browserName, options) {
this.browser = await this.launch(browserName, options);
return this.browser;
}
static async startNewContext(options) {
this.currentContext = await this.browser.newContext(options);
this.currentContext.on('page', page => {
if (this._currentPage)
this.context.previousPage = this.currentPage;
this.currentPage = page;
});
return this.currentContext;
}
static async startNewPage(options) {
if (!this._currentContext)
await this.startNewContext(options);
if (this._currentPage)
this.context.previousPage = this.currentPage;
this.currentPage = await this.currentContext.newPage();
return this.currentPage;
}
static async close() {
await this.browser.close();
this._currentPage = undefined;
this._currentContext = undefined;
this._browser = undefined;
}
static async switchToPreviousTab() {
this.currentPage = this.context.previousPage;
await this.currentPage.bringToFront();
}
static async switchToTabByIndex(index) {
this.currentPage = this.context.pages[index];
await this.currentPage.bringToFront();
}
}
exports.BrowserInstance = BrowserInstance;