diff --git a/.yarn/versions/c84ee467.yml b/.yarn/versions/c84ee467.yml new file mode 100644 index 0000000..bea3c27 --- /dev/null +++ b/.yarn/versions/c84ee467.yml @@ -0,0 +1,2 @@ +releases: + "@ngx-playwright/test": patch diff --git a/packages/test/src/screen.js b/packages/test/src/screen.js index a098f0e..e0ddc80 100644 --- a/packages/test/src/screen.js +++ b/packages/test/src/screen.js @@ -32,12 +32,19 @@ export async function openScreen(baseURL, page, harnessEnvironment, screen) { ); } - if (hasOpenFunction(screen)) { - await screen.open(page, baseURL, (screen) => - openScreen(baseURL, page, harnessEnvironment, screen), - ); - } else { - await page.goto(new URL(screen.path, baseURL).href); + if ( + typeof screen.isOpen !== "function" || + !(await screen.isOpen(page, baseURL)) + ) { + if (hasOpenFunction(screen)) { + await screen.open(page, baseURL, (screen) => + openScreen(baseURL, page, harnessEnvironment, screen), + ); + } else if ("path" in screen && typeof screen.path === "string") { + await page.goto(new URL(screen.path, baseURL).href); + } else { + throw new Error("Expected screen to be open but it wasn't"); + } } return harnessEnvironment.getHarness(screen); diff --git a/packages/test/src/types.d.ts b/packages/test/src/types.d.ts index d4fb39d..9531db6 100644 --- a/packages/test/src/types.d.ts +++ b/packages/test/src/types.d.ts @@ -8,6 +8,8 @@ import {Page} from "@playwright/test"; export interface PlaywrightScreenWithPath extends ComponentHarnessConstructor { readonly path: string; + + isOpen?(page: Page, baseUrl: string): Promise; } export interface PlaywrightScreenWithOpenFunction @@ -17,11 +19,20 @@ export interface PlaywrightScreenWithOpenFunction baseUrl: string, opener: PlaywrightScreenOpener, ): Promise; + + isOpen?(page: Page, baseUrl: string): Promise; +} + +export interface PlaywrightScreenWithIsOpenFunction< + T extends AnyComponentHarness, +> extends ComponentHarnessConstructor { + isOpen(page: Page, baseUrl: string): Promise; } export type PlaywrightScreen = | PlaywrightScreenWithOpenFunction - | PlaywrightScreenWithPath; + | PlaywrightScreenWithPath + | PlaywrightScreenWithIsOpenFunction; export interface PlaywrightScreenOpener { (screen: PlaywrightScreen): Promise;