@@ -85,60 +85,55 @@ export class ToolBaseGenUIPage {
8585 }
8686
8787 async extractMainDisplayHaikuContent ( page : Page ) : Promise < string > {
88- // Wait for main haiku lines to be present
89- const mainDisplayLines = page . locator ( '[data-testid="main-haiku-line"]' ) ;
90- const mainCount = await mainDisplayLines . count ( ) ;
88+ const activeCard = page . locator ( '[data-testid="main-haiku-display"].active' ) . last ( ) ;
89+
90+ try {
91+ await activeCard . waitFor ( { state : 'visible' , timeout : 5000 } ) ;
92+ } catch ( error ) {
93+ // Fallback to any visible haiku lines if the active card isn't available yet
94+ const fallbackLines = page . locator ( '[data-testid="main-haiku-line"]' ) ;
95+ const fallbackCount = await fallbackLines . count ( ) ;
96+ if ( fallbackCount === 0 ) {
97+ return '' ;
98+ }
99+
100+ const fallbackLineTexts : string [ ] = [ ] ;
101+ for ( let i = 0 ; i < fallbackCount ; i ++ ) {
102+ const fallbackLine = fallbackLines . nth ( i ) ;
103+ const japaneseText = await fallbackLine . locator ( 'p' ) . first ( ) . innerText ( ) ;
104+ fallbackLineTexts . push ( japaneseText ) ;
105+ }
106+
107+ return fallbackLineTexts . join ( '' ) . replace ( / \s / g, '' ) ;
108+ }
91109
92- if ( mainCount === 0 ) {
110+ const mainDisplayLines = activeCard . locator ( '[data-testid="main-haiku-line"]' ) ;
111+ const count = await mainDisplayLines . count ( ) ;
112+ if ( count === 0 ) {
93113 return '' ;
94114 }
95115
96- // Take only the last 3 lines (most recent haiku)
97- // Haikus are 3 lines, and they're appended in order
98- const startIndex = Math . max ( 0 , mainCount - 3 ) ;
99116 const lines : string [ ] = [ ] ;
100-
101- for ( let i = startIndex ; i < mainCount ; i ++ ) {
117+ for ( let i = 0 ; i < count ; i ++ ) {
102118 const haikuLine = mainDisplayLines . nth ( i ) ;
103119 const japaneseText = await haikuLine . locator ( 'p' ) . first ( ) . innerText ( ) ;
104120 lines . push ( japaneseText ) ;
105121 }
106122
107- const mainHaikuContent = lines . join ( "" ) . replace ( / \s / g, "" ) ;
108- return mainHaikuContent ;
123+ return lines . join ( '' ) . replace ( / \s / g, '' ) ;
109124 }
110125
111126 async checkHaikuDisplay ( page : Page ) : Promise < void > {
112127 const chatHaikuContent = await this . extractChatHaikuContent ( page ) ;
113128
114- await page . waitForTimeout ( 3000 ) ;
115-
116- // Check that the haiku exists somewhere in the carousel
117- const carousel = page . locator ( '[data-testid="haiku-carousel"]' ) ;
118- await carousel . waitFor ( { state : "visible" , timeout : 10000 } ) ;
119-
120- const allCarouselCards = carousel . locator ( '[data-testid="haiku-card"]' ) ;
121- const cardCount = await allCarouselCards . count ( ) ;
122-
123- let foundMatch = false ;
124- for ( let i = 0 ; i < cardCount ; i ++ ) {
125- const card = allCarouselCards . nth ( i ) ;
126- const lines = card . locator ( '[data-testid="haiku-japanese-line"]' ) ;
127- const lineCount = await lines . count ( ) ;
128- const cardLines : string [ ] = [ ] ;
129-
130- for ( let j = 0 ; j < lineCount ; j ++ ) {
131- const text = await lines . nth ( j ) . innerText ( ) ;
132- cardLines . push ( text ) ;
133- }
134-
135- const cardContent = cardLines . join ( "" ) . replace ( / \s / g, "" ) ;
136- if ( cardContent === chatHaikuContent ) {
137- foundMatch = true ;
138- break ;
139- }
140- }
141-
142- expect ( foundMatch ) . toBe ( true ) ;
129+ await expect
130+ . poll ( async ( ) => {
131+ const content = await this . extractMainDisplayHaikuContent ( page ) ;
132+ return content ;
133+ } , {
134+ timeout : 10000 ,
135+ message : 'Main display did not match the latest chat haiku' ,
136+ } )
137+ . toBe ( chatHaikuContent ) ;
143138 }
144139}
0 commit comments