@@ -23,6 +23,7 @@ const testSuiteStartCh = channel('ci:playwright:test-suite:start')
2323const testSuiteFinishCh = channel ( 'ci:playwright:test-suite:finish' )
2424
2525const workerReportCh = channel ( 'ci:playwright:worker:report' )
26+ const testPageGotoCh = channel ( 'ci:playwright:test:page-goto' )
2627
2728const testToAr = new WeakMap ( )
2829const testSuiteToAr = new Map ( )
@@ -783,6 +784,37 @@ addHook({
783784 return processHostPackage
784785} )
785786
787+ addHook ( {
788+ name : 'playwright-core' ,
789+ file : 'lib/client/page.js' ,
790+ versions : [ '>=1.38.0' ]
791+ } , ( pagePackage ) => {
792+ shimmer . wrap ( pagePackage . Page . prototype , 'goto' , goto => async function ( url , options ) {
793+ const response = await goto . apply ( this , arguments )
794+
795+ const page = this
796+
797+ const isRumActive = await page . evaluate ( ( ) => {
798+ if ( window . DD_RUM && window . DD_RUM . getInternalContext ) {
799+ return ! ! window . DD_RUM . getInternalContext ( )
800+ } else {
801+ return false
802+ }
803+ } )
804+
805+ if ( isRumActive ) {
806+ testPageGotoCh . publish ( {
807+ isRumActive,
808+ page
809+ } )
810+ }
811+
812+ return response
813+ } )
814+
815+ return pagePackage
816+ } )
817+
786818// Only in worker
787819addHook ( {
788820 name : 'playwright' ,
@@ -822,6 +854,55 @@ addHook({
822854 browserName
823855 } )
824856
857+ let existAfterEachHook = false
858+
859+ // We try to find an existing afterEach hook with _ddHook to avoid adding a new one
860+ for ( const hook of test . parent . _hooks ) {
861+ if ( hook . type === 'afterEach' && hook . _ddHook ) {
862+ existAfterEachHook = true
863+ break
864+ }
865+ }
866+
867+ // In cases where there is no afterEach hook with _ddHook, we need to add one
868+ if ( ! existAfterEachHook ) {
869+ test . parent . _hooks . push ( {
870+ type : 'afterEach' ,
871+ fn : async function ( { page } ) {
872+ try {
873+ if ( page ) {
874+ const isRumActive = await page . evaluate ( ( ) => {
875+ if ( window . DD_RUM && window . DD_RUM . stopSession ) {
876+ window . DD_RUM . stopSession ( )
877+ return true
878+ } else {
879+ return false
880+ }
881+ } )
882+
883+ if ( isRumActive ) {
884+ const url = page . url ( )
885+ if ( url ) {
886+ const domain = new URL ( url ) . hostname
887+ await page . context ( ) . addCookies ( [ {
888+ name : 'datadog-ci-visibility-test-execution-id' ,
889+ value : '' ,
890+ domain,
891+ expires : 0 ,
892+ path : '/'
893+ } ] )
894+ }
895+ }
896+ }
897+ } catch ( e ) {
898+ // ignore errors
899+ }
900+ } ,
901+ title : 'afterEach hook' ,
902+ _ddHook : true
903+ } )
904+ }
905+
825906 res = _runTest . apply ( this , arguments )
826907
827908 testInfo = this . _currentTest
0 commit comments