@@ -3,6 +3,10 @@ import type {Locator} from '@playwright/test';
33import type { PlaywrightFixture , WaitFixture } from './types' ;
44
55const DEFAULT_DELAY = 100 ;
6+ const MAX_STABLE_CHECKS = 50 ;
7+ const REQUIRED_STABLE_COUNT = 2 ;
8+
9+ type Snapshot = { text : string ; height : number } ;
610
711export const wait : PlaywrightFixture < WaitFixture > = async ( { page} , use ) => {
812 await use ( {
@@ -23,5 +27,36 @@ export const wait: PlaywrightFixture<WaitFixture> = async ({page}, use) => {
2327 timeout : async ( delay = DEFAULT_DELAY ) => {
2428 await page . waitForTimeout ( delay ) ;
2529 } ,
30+ markupRendered : async ( delay = DEFAULT_DELAY , stableThreshold = REQUIRED_STABLE_COUNT ) => {
31+ const locator = page . locator ( '.playground__markup' ) ;
32+ await locator . waitFor ( { state : 'visible' } ) ;
33+
34+ let prev : Snapshot = { text : '' , height : 0 } ;
35+ let stableCount = 0 ;
36+
37+ for ( let i = 0 ; i < MAX_STABLE_CHECKS ; i ++ ) {
38+ const curr = await getMarkupPreviewMetrics ( locator ) ;
39+
40+ if ( isStable ( prev , curr ) ) {
41+ stableCount ++ ;
42+ if ( stableCount >= stableThreshold ) break ;
43+ } else {
44+ stableCount = 0 ;
45+ prev = curr ;
46+ }
47+
48+ await page . waitForTimeout ( delay ) ;
49+ }
50+ } ,
2651 } ) ;
2752} ;
53+
54+ async function getMarkupPreviewMetrics ( locator : Locator ) : Promise < Snapshot > {
55+ const text = ( await locator . textContent ( ) ) ?. trim ( ) ?? '' ;
56+ const height = ( await locator . boundingBox ( ) ) ?. height ?? 0 ;
57+ return { text, height} ;
58+ }
59+
60+ function isStable ( prev : Snapshot , curr : Snapshot ) : boolean {
61+ return Boolean ( curr . text ) && prev . text === curr . text && prev . height === curr . height ;
62+ }
0 commit comments