@@ -33,39 +33,51 @@ export class ToolBaseGenUIPage {
33
33
}
34
34
35
35
async extractChatHaikuContent ( page : Page ) : Promise < string > {
36
- await page . waitForTimeout ( 3000 ) ;
37
- await page . locator ( '[data-testid="haiku-card"]' ) . first ( ) . waitFor ( { state : 'visible' } ) ;
36
+ // Wait for haiku cards to be visible
37
+ await page . waitForSelector ( '[data-testid="haiku-card"]' , { state : 'visible' } ) ;
38
+
38
39
const allHaikuCards = page . locator ( '[data-testid="haiku-card"]' ) ;
39
40
const cardCount = await allHaikuCards . count ( ) ;
40
41
let chatHaikuContainer ;
41
42
let chatHaikuLines ;
42
43
44
+ // Find the most recent haiku card with lines
43
45
for ( let cardIndex = cardCount - 1 ; cardIndex >= 0 ; cardIndex -- ) {
44
46
chatHaikuContainer = allHaikuCards . nth ( cardIndex ) ;
45
47
chatHaikuLines = chatHaikuContainer . locator ( '[data-testid="haiku-line"]' ) ;
46
- const linesCount = await chatHaikuLines . count ( ) ;
47
-
48
- if ( linesCount > 0 ) {
49
- try {
50
- await chatHaikuLines . first ( ) . waitFor ( { state : 'visible' , timeout : 5000 } ) ;
51
- break ;
52
- } catch ( error ) {
53
- continue ;
54
- }
48
+
49
+ try {
50
+ // Wait for at least 3 haiku lines to be present in this card
51
+ await page . waitForFunction ( ( cardIdx ) => {
52
+ const cards = document . querySelectorAll ( '[data-testid="haiku-card"]' ) ;
53
+ if ( cards [ cardIdx ] ) {
54
+ const lines = cards [ cardIdx ] . querySelectorAll ( '[data-testid="haiku-line"]' ) ;
55
+ return lines . length >= 3 ;
56
+ }
57
+ return false ;
58
+ } , cardIndex , { timeout : 10000 } ) ;
59
+
60
+ // Verify the lines are visible
61
+ await chatHaikuLines . first ( ) . waitFor ( { state : 'visible' , timeout : 5000 } ) ;
62
+ break ;
63
+ } catch ( error ) {
64
+ continue ;
55
65
}
56
66
}
57
67
58
68
if ( ! chatHaikuLines ) {
59
- throw new Error ( 'No haiku cards with visible lines found' ) ;
69
+ throw new Error ( 'No haiku cards with 3 visible lines found' ) ;
60
70
}
61
71
62
72
const count = await chatHaikuLines . count ( ) ;
63
73
const lines : string [ ] = [ ] ;
64
74
65
- for ( let i = 0 ; i < count ; i ++ ) {
66
- const haikuLine = chatHaikuLines . nth ( i ) ;
67
- const japaneseText = await haikuLine . locator ( 'p' ) . first ( ) . innerText ( ) ;
68
- lines . push ( japaneseText ) ;
75
+ if ( count > 0 ) {
76
+ for ( let i = 0 ; i < count ; i ++ ) {
77
+ const haikuLine = chatHaikuLines . nth ( i ) ;
78
+ const japaneseText = await haikuLine . locator ( 'p' ) . first ( ) . innerText ( ) ;
79
+ lines . push ( japaneseText ) ;
80
+ }
69
81
}
70
82
71
83
const chatHaikuContent = lines . join ( '' ) . replace ( / \s / g, '' ) ;
0 commit comments