@serenity-js/playwright
Advanced tools
Comparing version 3.14.1 to 3.14.2
@@ -6,2 +6,14 @@ # Change Log | ||
## [3.14.2](https://github.com/serenity-js/serenity-js/compare/v3.14.1...v3.14.2) (2023-12-12) | ||
### Bug Fixes | ||
* **playwright-test:** fixed switching between multiple pages ([375f3aa](https://github.com/serenity-js/serenity-js/commit/375f3aaac05843b71a88d56ae0f5e4d99522f10e)) | ||
* **playwright:** ignore taking the screenshot if the page is already closed ([fdedeb8](https://github.com/serenity-js/serenity-js/commit/fdedeb8d8ca5fe6406101be930c17ad281a8f26d)) | ||
## [3.14.1](https://github.com/serenity-js/serenity-js/compare/v3.14.0...v3.14.1) (2023-12-10) | ||
@@ -8,0 +20,0 @@ |
@@ -128,3 +128,3 @@ "use strict"; | ||
static usingPage(page, options) { | ||
return new BrowseTheWebWithPlaywright(new models_1.PlaywrightBrowsingSessionWithPage(page, options)); | ||
return new BrowseTheWebWithPlaywright(new models_1.PlaywrightBrowsingSessionWithPage(page, options, playwright.selectors)); | ||
} | ||
@@ -131,0 +131,0 @@ /** |
@@ -5,2 +5,3 @@ import type { BrowserCapabilities } from '@serenity-js/web'; | ||
import { type PlaywrightOptions } from '../../PlaywrightOptions'; | ||
import { SerenitySelectorEngines } from '../../selector-engines'; | ||
import { PlaywrightPage } from '../models'; | ||
@@ -14,4 +15,6 @@ /** | ||
protected readonly browserContextOptions: PlaywrightOptions; | ||
protected readonly selectors: playwright.Selectors; | ||
protected readonly serenitySelectorEngines: SerenitySelectorEngines; | ||
private currentPlaywrightBrowserContext; | ||
protected constructor(browserContextOptions: PlaywrightOptions); | ||
protected constructor(browserContextOptions: PlaywrightOptions, selectors: playwright.Selectors); | ||
/** | ||
@@ -28,3 +31,2 @@ * Returns {@apilink BrowserCapabilities|basic meta-data} about the browser associated with this ability. | ||
deleteAllCookies(): Promise<void>; | ||
protected registerCurrentPage(): Promise<PlaywrightPage>; | ||
protected browserContext(): Promise<playwright.BrowserContext>; | ||
@@ -31,0 +33,0 @@ protected abstract createBrowserContext(options: PlaywrightOptions): Promise<playwright.BrowserContext>; |
@@ -6,2 +6,3 @@ "use strict"; | ||
const web_1 = require("@serenity-js/web"); | ||
const selector_engines_1 = require("../../selector-engines"); | ||
const models_1 = require("../models"); | ||
@@ -15,6 +16,9 @@ /** | ||
browserContextOptions; | ||
selectors; | ||
serenitySelectorEngines = new selector_engines_1.SerenitySelectorEngines(); | ||
currentPlaywrightBrowserContext; | ||
constructor(browserContextOptions) { | ||
constructor(browserContextOptions, selectors) { | ||
super(); | ||
this.browserContextOptions = browserContextOptions; | ||
this.selectors = selectors; | ||
} | ||
@@ -33,13 +37,5 @@ async cookie(name) { | ||
} | ||
async registerCurrentPage() { | ||
const context = await this.browserContext(); | ||
await context.newPage(); | ||
// calling context.newPage() triggers a callback registered via browserContext(), | ||
// which wraps playwright.Page in PlaywrightPage and adds it to the list of pages | ||
// returned by this.allPages() | ||
const allPages = await this.allPages(); | ||
return allPages.at(-1); | ||
} | ||
async browserContext() { | ||
if (!this.currentPlaywrightBrowserContext) { | ||
await this.serenitySelectorEngines.ensureRegisteredWith(this.selectors); | ||
this.currentPlaywrightBrowserContext = await this.createBrowserContext(this.browserContextOptions); | ||
@@ -46,0 +42,0 @@ this.currentPlaywrightBrowserContext.on('page', async (page) => { |
@@ -5,2 +5,3 @@ import type { BrowserCapabilities } from '@serenity-js/web'; | ||
import { PlaywrightBrowsingSession } from './PlaywrightBrowsingSession'; | ||
import type { PlaywrightPage } from './PlaywrightPage'; | ||
/** | ||
@@ -11,6 +12,5 @@ * @group Models | ||
protected readonly browser: playwright.Browser; | ||
private readonly selectors; | ||
private readonly serenitySelectorEngines; | ||
constructor(browser: playwright.Browser, browserContextOptions: PlaywrightOptions, selectors: playwright.Selectors); | ||
protected createBrowserContext(options: PlaywrightOptions): Promise<playwright.BrowserContext>; | ||
protected registerCurrentPage(): Promise<PlaywrightPage>; | ||
closeAllPages(): Promise<void>; | ||
@@ -17,0 +17,0 @@ browserCapabilities(): Promise<BrowserCapabilities>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.PlaywrightBrowsingSessionWithBrowser = void 0; | ||
const selector_engines_1 = require("../../selector-engines"); | ||
const PlaywrightBrowsingSession_1 = require("./PlaywrightBrowsingSession"); | ||
@@ -11,13 +10,18 @@ /** | ||
browser; | ||
selectors; | ||
serenitySelectorEngines = new selector_engines_1.SerenitySelectorEngines(); | ||
constructor(browser, browserContextOptions, selectors) { | ||
super(browserContextOptions); | ||
super(browserContextOptions, selectors); | ||
this.browser = browser; | ||
this.selectors = selectors; | ||
} | ||
async createBrowserContext(options) { | ||
await this.serenitySelectorEngines.ensureRegisteredWith(this.selectors); | ||
return this.browser.newContext(this.browserContextOptions); | ||
} | ||
async registerCurrentPage() { | ||
const context = await this.browserContext(); | ||
await context.newPage(); | ||
// calling context.newPage() triggers a callback registered via browserContext(), | ||
// which wraps playwright.Page in PlaywrightPage and adds it to the list of pages | ||
// returned by this.allPages() | ||
const allPages = await this.allPages(); | ||
return allPages.at(-1); | ||
} | ||
async closeAllPages() { | ||
@@ -24,0 +28,0 @@ await super.closeAllPages(); |
@@ -5,2 +5,3 @@ import type { BrowserCapabilities } from '@serenity-js/web'; | ||
import { PlaywrightBrowsingSession } from './PlaywrightBrowsingSession'; | ||
import { PlaywrightPage } from './PlaywrightPage'; | ||
/** | ||
@@ -11,6 +12,12 @@ * @group Models | ||
protected readonly page: playwright.Page; | ||
constructor(page: playwright.Page, browserContextOptions: PlaywrightOptions); | ||
private readonly playwrightManagedPageId; | ||
constructor(page: playwright.Page, browserContextOptions: PlaywrightOptions, selectors: playwright.Selectors); | ||
protected registerCurrentPage(): Promise<PlaywrightPage>; | ||
protected createBrowserContext(options: PlaywrightOptions): Promise<playwright.BrowserContext>; | ||
/** | ||
* Closes any newly opened pages, leaving only the original one managed by Playwright Test. | ||
*/ | ||
closeAllPages(): Promise<void>; | ||
browserCapabilities(): Promise<BrowserCapabilities>; | ||
} | ||
//# sourceMappingURL=PlaywrightBrowsingSessionWithPage.d.ts.map |
@@ -12,10 +12,28 @@ "use strict"; | ||
page; | ||
constructor(page, browserContextOptions) { | ||
super(browserContextOptions); | ||
playwrightManagedPageId = model_1.CorrelationId.create(); | ||
constructor(page, browserContextOptions, selectors) { | ||
super(browserContextOptions, selectors); | ||
this.page = page; | ||
this.currentBrowserPage = new PlaywrightPage_1.PlaywrightPage(this, this.page, this.browserContextOptions, model_1.CorrelationId.create()); | ||
} | ||
async registerCurrentPage() { | ||
await this.browserContext(); | ||
const playwrightPage = new PlaywrightPage_1.PlaywrightPage(this, this.page, this.browserContextOptions, this.playwrightManagedPageId); | ||
this.register(playwrightPage); | ||
return playwrightPage; | ||
} | ||
async createBrowserContext(options) { | ||
return this.page.context(); | ||
} | ||
/** | ||
* Closes any newly opened pages, leaving only the original one managed by Playwright Test. | ||
*/ | ||
async closeAllPages() { | ||
for (const page of this.pages.values()) { | ||
if (!page.id.equals(this.playwrightManagedPageId)) { | ||
await page.close(); | ||
} | ||
} | ||
this.pages.clear(); | ||
this.currentBrowserPage = undefined; | ||
} | ||
async browserCapabilities() { | ||
@@ -22,0 +40,0 @@ const browser = await this.page.context().browser(); |
@@ -105,4 +105,12 @@ "use strict"; | ||
async takeScreenshot() { | ||
const screenshot = await this.page.screenshot(); | ||
return screenshot.toString('base64'); | ||
try { | ||
const screenshot = await this.page.screenshot(); | ||
return screenshot.toString('base64'); | ||
} | ||
catch (error) { | ||
if (error?.message.includes('Target page, context or browser has been closed')) { | ||
throw new web_1.BrowserWindowClosedError(`Couldn't take screenshot since the browser window is already closed`, error); | ||
} | ||
throw error; | ||
} | ||
} | ||
@@ -109,0 +117,0 @@ async cookie(name) { |
{ | ||
"name": "@serenity-js/playwright", | ||
"version": "3.14.1", | ||
"version": "3.14.2", | ||
"description": "Serenity/JS Screenplay Pattern library for Playwright", | ||
@@ -47,4 +47,4 @@ "author": { | ||
"dependencies": { | ||
"@serenity-js/core": "3.14.1", | ||
"@serenity-js/web": "3.14.1", | ||
"@serenity-js/core": "3.14.2", | ||
"@serenity-js/web": "3.14.2", | ||
"playwright-core": "1.40.1", | ||
@@ -63,3 +63,3 @@ "tiny-types": "1.21.0" | ||
}, | ||
"gitHead": "27c466fdd19ebeb766b8517c847f1163e8ed8d6f" | ||
"gitHead": "b2b052ea1120bf33c2848956f2f0ea5f0ffdfc24" | ||
} |
@@ -111,3 +111,3 @@ import type { Discardable } from '@serenity-js/core'; | ||
static usingPage(page: playwright.Page, options?: PlaywrightOptions): BrowseTheWebWithPlaywright { | ||
return new BrowseTheWebWithPlaywright(new PlaywrightBrowsingSessionWithPage(page, options)); | ||
return new BrowseTheWebWithPlaywright(new PlaywrightBrowsingSessionWithPage(page, options, playwright.selectors)); | ||
} | ||
@@ -114,0 +114,0 @@ |
@@ -7,2 +7,3 @@ import { CorrelationId } from '@serenity-js/core/lib/model'; | ||
import { type PlaywrightOptions } from '../../PlaywrightOptions'; | ||
import { SerenitySelectorEngines } from '../../selector-engines'; | ||
import { PlaywrightCookie, PlaywrightPage } from '../models'; | ||
@@ -17,5 +18,9 @@ | ||
protected readonly serenitySelectorEngines = new SerenitySelectorEngines(); | ||
private currentPlaywrightBrowserContext: playwright.BrowserContext; | ||
protected constructor(protected readonly browserContextOptions: PlaywrightOptions) { | ||
protected constructor( | ||
protected readonly browserContextOptions: PlaywrightOptions, | ||
protected readonly selectors: playwright.Selectors | ||
) { | ||
super(); | ||
@@ -49,18 +54,5 @@ } | ||
protected override async registerCurrentPage(): Promise<PlaywrightPage> { | ||
const context = await this.browserContext(); | ||
await context.newPage(); | ||
// calling context.newPage() triggers a callback registered via browserContext(), | ||
// which wraps playwright.Page in PlaywrightPage and adds it to the list of pages | ||
// returned by this.allPages() | ||
const allPages = await this.allPages() | ||
return allPages.at(-1); | ||
} | ||
protected async browserContext(): Promise<playwright.BrowserContext> { | ||
if (! this.currentPlaywrightBrowserContext) { | ||
await this.serenitySelectorEngines.ensureRegisteredWith(this.selectors); | ||
this.currentPlaywrightBrowserContext = await this.createBrowserContext(this.browserContextOptions); | ||
@@ -67,0 +59,0 @@ |
@@ -5,4 +5,4 @@ import type { BrowserCapabilities } from '@serenity-js/web'; | ||
import type { PlaywrightOptions } from '../../PlaywrightOptions'; | ||
import { SerenitySelectorEngines } from '../../selector-engines'; | ||
import { PlaywrightBrowsingSession } from './PlaywrightBrowsingSession'; | ||
import type { PlaywrightPage } from './PlaywrightPage'; | ||
@@ -13,3 +13,2 @@ /** | ||
export class PlaywrightBrowsingSessionWithBrowser extends PlaywrightBrowsingSession { | ||
private readonly serenitySelectorEngines = new SerenitySelectorEngines(); | ||
@@ -19,13 +18,25 @@ constructor( | ||
browserContextOptions: PlaywrightOptions, | ||
private readonly selectors: playwright.Selectors, | ||
selectors: playwright.Selectors, | ||
) { | ||
super(browserContextOptions); | ||
super(browserContextOptions, selectors); | ||
} | ||
protected override async createBrowserContext(options: PlaywrightOptions): Promise<playwright.BrowserContext> { | ||
await this.serenitySelectorEngines.ensureRegisteredWith(this.selectors); | ||
return this.browser.newContext(this.browserContextOptions); | ||
} | ||
protected override async registerCurrentPage(): Promise<PlaywrightPage> { | ||
const context = await this.browserContext(); | ||
await context.newPage(); | ||
// calling context.newPage() triggers a callback registered via browserContext(), | ||
// which wraps playwright.Page in PlaywrightPage and adds it to the list of pages | ||
// returned by this.allPages() | ||
const allPages = await this.allPages() | ||
return allPages.at(-1); | ||
} | ||
override async closeAllPages(): Promise<void> { | ||
@@ -32,0 +43,0 @@ await super.closeAllPages(); |
@@ -13,10 +13,22 @@ import { CorrelationId } from '@serenity-js/core/lib/model'; | ||
export class PlaywrightBrowsingSessionWithPage extends PlaywrightBrowsingSession { | ||
private readonly playwrightManagedPageId: CorrelationId = CorrelationId.create(); | ||
constructor( | ||
protected readonly page: playwright.Page, | ||
browserContextOptions: PlaywrightOptions, | ||
selectors: playwright.Selectors, | ||
) { | ||
super(browserContextOptions); | ||
this.currentBrowserPage = new PlaywrightPage(this, this.page, this.browserContextOptions, CorrelationId.create()); | ||
super(browserContextOptions, selectors); | ||
} | ||
protected override async registerCurrentPage(): Promise<PlaywrightPage> { | ||
await this.browserContext(); | ||
const playwrightPage = new PlaywrightPage(this, this.page, this.browserContextOptions, this.playwrightManagedPageId); | ||
this.register(playwrightPage); | ||
return playwrightPage; | ||
} | ||
protected override async createBrowserContext(options: PlaywrightOptions): Promise<playwright.BrowserContext> { | ||
@@ -26,2 +38,16 @@ return this.page.context(); | ||
/** | ||
* Closes any newly opened pages, leaving only the original one managed by Playwright Test. | ||
*/ | ||
async closeAllPages(): Promise<void> { | ||
for (const page of this.pages.values()) { | ||
if (! page.id.equals(this.playwrightManagedPageId)) { | ||
await page.close(); | ||
} | ||
} | ||
this.pages.clear(); | ||
this.currentBrowserPage = undefined; | ||
} | ||
override async browserCapabilities(): Promise<BrowserCapabilities> { | ||
@@ -36,2 +62,1 @@ const browser = await this.page.context().browser(); | ||
} | ||
import { List, LogicError, type QuestionAdapter } from '@serenity-js/core'; | ||
import { asyncMap } from '@serenity-js/core/lib/io'; | ||
import type { CorrelationId } from '@serenity-js/core/lib/model'; | ||
import type { Cookie, CookieData, PageElement, PageElements,Selector } from '@serenity-js/web'; | ||
import { ByDeepCss, Key, Page, PageElementsLocator } from '@serenity-js/web'; | ||
import type { Cookie, CookieData, PageElement, PageElements, Selector } from '@serenity-js/web'; | ||
import { BrowserWindowClosedError, ByDeepCss, Key, Page, PageElementsLocator } from '@serenity-js/web'; | ||
import type * as playwright from 'playwright-core'; | ||
@@ -177,5 +177,18 @@ import { URL } from 'url'; | ||
async takeScreenshot(): Promise<string> { | ||
const screenshot: Buffer = await this.page.screenshot(); | ||
try { | ||
const screenshot: Buffer = await this.page.screenshot(); | ||
return screenshot.toString('base64'); | ||
return screenshot.toString('base64'); | ||
} | ||
catch(error) { | ||
if (error?.message.includes('Target page, context or browser has been closed')) { | ||
throw new BrowserWindowClosedError( | ||
`Couldn't take screenshot since the browser window is already closed`, | ||
error | ||
); | ||
} | ||
throw error; | ||
} | ||
} | ||
@@ -182,0 +195,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
191853
2595
+ Added@serenity-js/assertions@3.14.2(transitive)
+ Added@serenity-js/core@3.14.2(transitive)
+ Added@serenity-js/web@3.14.2(transitive)
- Removed@serenity-js/assertions@3.14.1(transitive)
- Removed@serenity-js/core@3.14.1(transitive)
- Removed@serenity-js/web@3.14.1(transitive)
Updated@serenity-js/core@3.14.2
Updated@serenity-js/web@3.14.2