@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
54 lines • 2.44 kB
JavaScript
import { CorrelationId } from '@serenity-js/core/model';
import { BrowsingSession } from '@serenity-js/web';
import { SerenitySelectorEngines } from '../../selector-engines/index.js';
import { PlaywrightCookie } from './PlaywrightCookie.js';
import { PlaywrightPage } from './PlaywrightPage.js';
/**
* Playwright-specific implementation of [`BrowsingSession`](https://serenity-js.org/api/web/class/BrowsingSession/).
*
* @group Models
*/
export class PlaywrightBrowsingSession extends BrowsingSession {
extraBrowserContextOptions;
selectors;
serenitySelectorEngines = new SerenitySelectorEngines();
currentPlaywrightBrowserContext;
constructor(extraBrowserContextOptions, selectors) {
super();
this.extraBrowserContextOptions = extraBrowserContextOptions;
this.selectors = selectors;
}
async cookie(name) {
const context = await this.browserContext();
return new 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 PlaywrightPage(this, page, this.extraBrowserContextOptions, 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;
}
}
//# sourceMappingURL=PlaywrightBrowsingSession.js.map