11import { expect , type Locator , type Page } from "@playwright/test" ;
22
3- const NEXT_FRAME_DELAY = 64 ;
4-
53/**
64 * Get the last std output of the given type
5+ * If expectContent is provided, it will retry to get the last std output until it matches the expected content
76 */
87export async function getLastStd (
98 page : Page ,
109 type : "stdin" | "stdout" | "stderr" ,
10+ {
11+ expectContent,
12+ } : {
13+ expectContent ?: string ;
14+ } = { } ,
1115) {
12- return await page . locator ( `[data-stdtype='${ type } ']` ) . last ( ) ;
16+ const locator = await page . locator ( `[data-stdtype='${ type } ']` ) . last ( ) ;
17+ if ( expectContent ) {
18+ const text = await locator . textContent ( ) ;
19+ if ( text ?. includes ( expectContent ) ) {
20+ return locator ;
21+ }
22+ // if no match, do a hard expect that will fail the test with a clear error message
23+ // Sorry you landed here, you will most likely have to add some `sleep()` in your code 🥲
24+ await expect ( locator ) . toHaveText ( expectContent ) ;
25+ }
26+ return locator ;
1327}
1428
1529/**
@@ -28,8 +42,9 @@ export async function getLastStdAfter(
2842 . last ( ) ;
2943}
3044
31- async function sleep ( ms : number ) {
32- return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
45+ export async function sleep ( ms ?: number ) : Promise < void > {
46+ const DEFAULT_DELAY = 200 ; // taking into account the default delay necessary in the CI
47+ return new Promise ( ( resolve ) => setTimeout ( resolve , ms ?? DEFAULT_DELAY ) ) ;
3348}
3449
3550/**
@@ -40,21 +55,27 @@ export async function fillAndSubmitCommand(
4055 page : Page ,
4156 command : string ,
4257 {
43- expectStdin = command ,
58+ expectStdin,
4459 expectStdout,
4560 expectStderr,
61+ afterSubmit,
4662 } : {
4763 expectStdin ?: string ;
4864 expectStdout ?: string ;
4965 expectStderr ?: string ;
66+ afterSubmit ?: ( ) => Promise < void > ;
5067 } = { } ,
5168) {
69+ const expectedStdin = expectStdin ?? command ;
5270 const input = await page . getByPlaceholder ( "Type a command..." ) ;
5371 await input . fill ( command ) ;
5472 await input . press ( "Enter" ) ;
55- await sleep ( NEXT_FRAME_DELAY ) ;
56- const stdin = await getLastStd ( page , "stdin" ) ;
57- await expect ( stdin ) . toHaveText ( expectStdin ) ;
73+ if ( afterSubmit ) {
74+ await afterSubmit ( ) ;
75+ }
76+ const stdin = await getLastStd ( page , "stdin" , {
77+ expectContent : expectedStdin ,
78+ } ) ;
5879 if ( expectStdout ) {
5980 const stdout = await getLastStdAfter ( page , "stdout" , stdin ) ;
6081 await expect ( stdout ) . toHaveText ( expectStdout ) ;
@@ -73,7 +94,7 @@ export async function clickWandButton(
7394 page : Page ,
7495 command : string ,
7596 {
76- expectStdin = command ,
97+ expectStdin,
7798 expectStdout,
7899 expectStderr,
79100 } : {
@@ -82,13 +103,15 @@ export async function clickWandButton(
82103 expectStderr ?: string ;
83104 } = { } ,
84105) {
106+ const expectedStdin = expectStdin ?? command ;
85107 await page . getByTitle ( "Run example command" ) . click ( { force : true } ) ;
86108 const input = await page . getByPlaceholder ( "Type a command..." ) ;
87- await expect ( input ) . toHaveValue ( expectStdin ) ;
88- const stdin = await getLastStd ( page , "stdin" ) ;
89- await expect ( stdin ) . toHaveText ( expectStdin ) ;
109+ await expect ( input ) . toHaveValue ( expectedStdin ) ;
110+ const stdin = await getLastStd ( page , "stdin" , {
111+ expectContent : expectedStdin ,
112+ } ) ;
90113 if ( expectStdout ) {
91- const stdout = await getLastStd ( page , "stdout" ) ;
114+ const stdout = await getLastStdAfter ( page , "stdout" , stdin ) ;
92115 await expect ( stdout ) . toHaveText ( expectStdout ) ;
93116 }
94117 if ( expectStderr ) {
0 commit comments