@serenity-js/playwright
Version:
Adapter that integrates @serenity-js/web with Playwright, enabling Serenity/JS reporting and using the Screenplay Pattern to write component and end-to-end test scenarios
57 lines • 2.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlaywrightBrowsingSession = void 0;
const model_1 = require("@serenity-js/core/lib/model");
const web_1 = require("@serenity-js/web");
const selector_engines_1 = require("../../selector-engines");
const models_1 = require("../models");
/**
* Playwright-specific implementation of [`BrowsingSession`](https://serenity-js.org/api/web/class/BrowsingSession/).
*
* @group Models
*/
class PlaywrightBrowsingSession extends web_1.BrowsingSession {
extraBrowserContextOptions;
selectors;
serenitySelectorEngines = new selector_engines_1.SerenitySelectorEngines();
currentPlaywrightBrowserContext;
constructor(extraBrowserContextOptions, selectors) {
super();
this.extraBrowserContextOptions = extraBrowserContextOptions;
this.selectors = selectors;
}
async cookie(name) {
const context = await this.browserContext();
return new models_1.PlaywrightCookie(context, name);
}
async setCookie(cookie) {
const context = await this.browserContext();
await context.addCookies([cookie]);
}
async deleteAllCookies() {
const context = await this.browserContext();
await context.clearCookies();
}
async browserContext() {
if (!this.currentPlaywrightBrowserContext) {
await this.serenitySelectorEngines.ensureRegisteredWith(this.selectors);
this.currentPlaywrightBrowserContext = await this.createBrowserContext();
this.currentPlaywrightBrowserContext.on('page', async (page) => {
const playwrightPage = new models_1.PlaywrightPage(this, page, this.extraBrowserContextOptions, model_1.CorrelationId.create());
this.register(playwrightPage);
page.on('close', _closedPage => {
this.deregister(playwrightPage.id);
});
});
if (this.extraBrowserContextOptions?.defaultNavigationTimeout) {
this.currentPlaywrightBrowserContext.setDefaultNavigationTimeout(this.extraBrowserContextOptions?.defaultNavigationTimeout);
}
if (this.extraBrowserContextOptions?.defaultTimeout) {
this.currentPlaywrightBrowserContext.setDefaultTimeout(this.extraBrowserContextOptions?.defaultTimeout);
}
}
return this.currentPlaywrightBrowserContext;
}
}
exports.PlaywrightBrowsingSession = PlaywrightBrowsingSession;
//# sourceMappingURL=PlaywrightBrowsingSession.js.map