@@ -638,23 +638,27 @@ test.describe('survey', () => {
638638 // Important to set this up *before* interacting with the page
639639 // in case of possible race conditions.
640640 await page . route ( '**/api/events' , ( route , request ) => {
641- route . fulfill ( { } )
642- expect ( request . method ( ) ) . toBe ( 'POST' )
643- const postData = JSON . parse ( request . postData ( ) || '{}' )
644- // Skip the exit event
645- if ( postData . type === 'exit' ) {
646- return
647- }
648- fulfilled ++
649- if ( postData . type === 'survey' && postData . survey_vote === true ) {
650- hasSurveyPressedEvent = true
651- }
652- if (
653- postData . type === 'survey' &&
654- postData . survey_vote === true &&
655- postData . survey_comment === surveyComment
656- ) {
657- hasSurveySubmittedEvent = true
641+ const postData = request . postData ( )
642+ if ( postData ) {
643+ const postDataArray = JSON . parse ( postData )
644+ route . fulfill ( { } )
645+ expect ( request . method ( ) ) . toBe ( 'POST' )
646+ fulfilled = postDataArray . length
647+ for ( const eventBody of postDataArray ) {
648+ if ( eventBody . type === 'survey' && eventBody . survey_vote === true ) {
649+ hasSurveyPressedEvent = true
650+ }
651+ if ( eventBody . type === 'survey' && eventBody . survey_vote === true ) {
652+ hasSurveyPressedEvent = true
653+ }
654+ if (
655+ eventBody . type === 'survey' &&
656+ eventBody . survey_vote === true &&
657+ eventBody . survey_comment === surveyComment
658+ ) {
659+ hasSurveySubmittedEvent = true
660+ }
661+ }
658662 }
659663 // At the time of writing you can't get the posted payload
660664 // when you use `navigator.sendBeacon(url, data)`.
@@ -673,16 +677,28 @@ test.describe('survey', () => {
673677 await expect ( page . getByRole ( 'button' , { name : 'Cancel' } ) ) . toBeVisible ( )
674678 await expect ( page . getByRole ( 'button' , { name : 'Send' } ) ) . toBeVisible ( )
675679
676- await page . locator ( '[for=survey-comment]' ) . click ( )
677680 await page . locator ( '[for=survey-comment]' ) . fill ( surveyComment )
678681 await page . locator ( '[name=survey-email]' ) . click ( )
679682 await page . locator ( '[name=survey-email]' ) . fill ( 'test@example.com' )
680683 await page . getByRole ( 'button' , { name : 'Send' } ) . click ( )
684+ // simulate sending an exit event to trigger sending all queued events
685+ await page . evaluate ( ( ) => {
686+ Object . defineProperty ( document , 'visibilityState' , {
687+ configurable : true ,
688+ get : function ( ) {
689+ return 'hidden'
690+ } ,
691+ } )
692+ document . dispatchEvent ( new Event ( 'visibilitychange' ) )
693+ return new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) )
694+ } )
695+
681696 // Events:
682697 // 1. page view event when navigating to the page
683698 // 2. Survey thumbs up event
684699 // 3. Survey submit event
685- expect ( fulfilled ) . toBe ( 1 + 1 + 1 )
700+ // 4. Exit event
701+ expect ( fulfilled ) . toBe ( 1 + 1 + 1 + 1 )
686702 expect ( hasSurveyPressedEvent ) . toBe ( true )
687703 expect ( hasSurveySubmittedEvent ) . toBe ( true )
688704 await expect ( page . getByTestId ( 'survey-end' ) ) . toBeVisible ( )
@@ -691,20 +707,22 @@ test.describe('survey', () => {
691707 test ( 'thumbs up without filling in the form sends an API POST' , async ( { page } ) => {
692708 let fulfilled = 0
693709 let hasSurveyEvent = false
710+
694711 // Important to set this up *before* interacting with the page
695712 // in case of possible race conditions.
696713 await page . route ( '**/api/events' , ( route , request ) => {
697- route . fulfill ( { } )
698- expect ( request . method ( ) ) . toBe ( 'POST' )
699- const postData = JSON . parse ( request . postData ( ) || '{}' )
700- // Skip the exit event
701- if ( postData . type === 'exit' ) {
702- return
703- }
704- if ( postData . type === 'survey' && postData . survey_vote === true ) {
705- hasSurveyEvent = true
714+ const postData = request . postData ( )
715+ if ( postData ) {
716+ const postDataArray = JSON . parse ( postData )
717+ route . fulfill ( { } )
718+ expect ( request . method ( ) ) . toBe ( 'POST' )
719+ fulfilled = postDataArray . length
720+ for ( const eventBody of postDataArray ) {
721+ if ( eventBody . type === 'survey' && eventBody . survey_vote === true ) {
722+ hasSurveyEvent = true
723+ }
724+ }
706725 }
707- fulfilled ++
708726 // At the time of writing you can't get the posted payload
709727 // when you use `navigator.sendBeacon(url, data)`.
710728 // So we can't make assertions about the payload.
@@ -718,10 +736,22 @@ test.describe('survey', () => {
718736 await page . goto ( '/get-started/foo/for-playwright' )
719737
720738 await page . locator ( '[for=survey-yes]' ) . click ( )
739+ // simulate sending an exit event to trigger sending all queued events
740+ await page . evaluate ( ( ) => {
741+ Object . defineProperty ( document , 'visibilityState' , {
742+ configurable : true ,
743+ get : function ( ) {
744+ return 'hidden'
745+ } ,
746+ } )
747+ document . dispatchEvent ( new Event ( 'visibilitychange' ) )
748+ return new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) )
749+ } )
721750 // Events:
722751 // 1. page view event when navigating to the page
723752 // 2. the thumbs up click
724- expect ( fulfilled ) . toBe ( 1 + 1 )
753+ // 3. the exit event
754+ expect ( fulfilled ) . toBe ( 1 + 1 + 1 )
725755 expect ( hasSurveyEvent ) . toBe ( true )
726756
727757 await expect ( page . getByRole ( 'button' , { name : 'Send' } ) ) . toBeVisible ( )
0 commit comments