Skip to content

Commit 0f96f97

Browse files
committed
{feature} add isOpen variant to screen types
This makes it easier to write tests that span multiple pages, for example ```ts class ScreenOne extends ComponentHarness { static path = '/one'; #next = this.locatorFor('#next'); async goToNext() { await (await this.next()).click(); } } class ScreenTwo extends ComponentHarness { static async isOpen(page) { return page.url().endsWith('/two'); } } test('navigation', ({open}) => { const screenOne = await open(ScreenOne); await screenOne.goToNext(); const screenTwo = await open(ScreenTwo); // ... }); ``` Screens with a path or open function can also define an isOpen function. If that function is present and it yields a truthy result, then the open function will not be called or no navigation will be triggered to the path.
1 parent edd62cc commit 0f96f97

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

.yarn/versions/c84ee467.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
releases:
2+
"@ngx-playwright/test": patch

packages/test/src/screen.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,19 @@ export async function openScreen(baseURL, page, harnessEnvironment, screen) {
3232
);
3333
}
3434

35-
if (hasOpenFunction(screen)) {
36-
await screen.open(page, baseURL, (screen) =>
37-
openScreen(baseURL, page, harnessEnvironment, screen),
38-
);
39-
} else {
40-
await page.goto(new URL(screen.path, baseURL).href);
35+
if (
36+
typeof screen.isOpen !== "function" ||
37+
!(await screen.isOpen(page, baseURL))
38+
) {
39+
if (hasOpenFunction(screen)) {
40+
await screen.open(page, baseURL, (screen) =>
41+
openScreen(baseURL, page, harnessEnvironment, screen),
42+
);
43+
} else if ("path" in screen && typeof screen.path === "string") {
44+
await page.goto(new URL(screen.path, baseURL).href);
45+
} else {
46+
throw new Error("Expected screen to be open but it wasn't");
47+
}
4148
}
4249

4350
return harnessEnvironment.getHarness(screen);

packages/test/src/types.d.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {Page} from "@playwright/test";
88
export interface PlaywrightScreenWithPath<T extends AnyComponentHarness>
99
extends ComponentHarnessConstructor<T> {
1010
readonly path: string;
11+
12+
isOpen?(page: Page, baseUrl: string): Promise<boolean>;
1113
}
1214

1315
export interface PlaywrightScreenWithOpenFunction<T extends AnyComponentHarness>
@@ -17,11 +19,20 @@ export interface PlaywrightScreenWithOpenFunction<T extends AnyComponentHarness>
1719
baseUrl: string,
1820
opener: PlaywrightScreenOpener,
1921
): Promise<void>;
22+
23+
isOpen?(page: Page, baseUrl: string): Promise<boolean>;
24+
}
25+
26+
export interface PlaywrightScreenWithIsOpenFunction<
27+
T extends AnyComponentHarness,
28+
> extends ComponentHarnessConstructor<T> {
29+
isOpen(page: Page, baseUrl: string): Promise<boolean>;
2030
}
2131

2232
export type PlaywrightScreen<T extends AnyComponentHarness> =
2333
| PlaywrightScreenWithOpenFunction<T>
24-
| PlaywrightScreenWithPath<T>;
34+
| PlaywrightScreenWithPath<T>
35+
| PlaywrightScreenWithIsOpenFunction<T>;
2536

2637
export interface PlaywrightScreenOpener {
2738
<T extends AnyComponentHarness>(screen: PlaywrightScreen<T>): Promise<T>;

0 commit comments

Comments
 (0)