1- import { expect , type Page } from "@playwright/test" ;
1+ import { expect , type Locator , type Page } from "@playwright/test" ;
2+
3+ const NEXT_FRAME_DELAY = 64 ;
24
35/**
46 * Get the last std output of the given type
@@ -10,6 +12,26 @@ export async function getLastStd(
1012 return await page . locator ( `[data-stdtype='${ type } ']` ) . last ( ) ;
1113}
1214
15+ /**
16+ * Get the last std output of the given type after the given locator
17+ * This is useful to get the last std output after a command has been submitted
18+ * This ensures you don't have false positives when checking the last std output
19+ */
20+ export async function getLastStdAfter (
21+ page : Page ,
22+ type : "stdin" | "stdout" | "stderr" ,
23+ stdLocator : Locator ,
24+ ) {
25+ const stdinIndex = await stdLocator . getAttribute ( "data-std-index" ) ;
26+ return await page
27+ . locator ( `[data-std-index='${ stdinIndex } '] ~ [data-stdtype='${ type } ']` )
28+ . last ( ) ;
29+ }
30+
31+ async function sleep ( ms : number ) {
32+ return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
33+ }
34+
1335/**
1436 * Fill the input with the command and submit it
1537 * Pass the expected stdin, stdout and stderr to check the results
@@ -30,14 +52,15 @@ export async function fillAndSubmitCommand(
3052 const input = await page . getByPlaceholder ( "Type a command..." ) ;
3153 await input . fill ( command ) ;
3254 await input . press ( "Enter" ) ;
55+ await sleep ( NEXT_FRAME_DELAY ) ;
3356 const stdin = await getLastStd ( page , "stdin" ) ;
3457 await expect ( stdin ) . toHaveText ( expectStdin ) ;
3558 if ( expectStdout ) {
36- const stdout = await getLastStd ( page , "stdout" ) ;
59+ const stdout = await getLastStdAfter ( page , "stdout" , stdin ) ;
3760 await expect ( stdout ) . toHaveText ( expectStdout ) ;
3861 }
3962 if ( expectStderr ) {
40- const stderr = await getLastStd ( page , "stderr" ) ;
63+ const stderr = await getLastStdAfter ( page , "stderr" , stdin ) ;
4164 await expect ( stderr ) . toHaveText ( expectStderr ) ;
4265 }
4366}
0 commit comments