11import fs from "fs" ;
2+ import path from "path" ;
23import { eventWithTime } from "@rrweb/types" ;
4+ import makeDebug from "debug" ;
35import type { Callstack } from "./callstack" ;
46import { MasterEvents } from "../../events" ;
57import type { SnapshotsData , Test , TestContext } from "../../types" ;
68import { runWithoutHistory } from "./index" ;
7- import path from "path" ;
9+
10+ const debug = makeDebug ( "testplane:time-travel:rrweb" ) ;
811
912// Built from branch https://github.com/gemini-testing/rrweb/tree/TESTPLANE-712.syntax_err
1013// PR: https://github.com/rrweb-io/rrweb/pull/1735
1114// Issue: https://github.com/rrweb-io/rrweb/issues/1734
1215const rrwebCode = fs . readFileSync ( path . join ( __dirname , "../client-scripts/rrweb-record.min.js" ) , "utf-8" ) ;
13- const sessionsWithRrwebRequested = new WeakSet < WebdriverIO . Browser > ( ) ;
16+ const sessionsWithRrwebSent = new WeakSet < WebdriverIO . Browser > ( ) ;
17+ const sessionsWithUnsupportedRrweb = new WeakSet < WebdriverIO . Browser > ( ) ;
1418
1519interface CollectRrwebEventsResult {
20+ isRrwebSupported ?: false ;
1621 isRrwebInstalled : boolean ;
1722 rrwebEvents : eventWithTime [ ] ;
23+ evalError ?: string ;
1824}
1925
2026/* eslint-disable @typescript-eslint/ban-ts-comment */
@@ -23,19 +29,47 @@ export async function installRrwebAndCollectEvents(
2329 callstack : Callstack ,
2430) : Promise < eventWithTime [ ] > {
2531 return runWithoutHistory < Promise < eventWithTime [ ] > > ( { callstack } , async ( ) => {
26- const shouldSendRrwebCode = ! sessionsWithRrwebRequested . has ( session ) ;
32+ if ( sessionsWithUnsupportedRrweb . has ( session ) ) {
33+ return [ ] ;
34+ }
35+
36+ const shouldSendRrwebCode = ! sessionsWithRrwebSent . has ( session ) ;
2737
2838 if ( shouldSendRrwebCode ) {
29- sessionsWithRrwebRequested . add ( session ) ;
39+ sessionsWithRrwebSent . add ( session ) ;
3040 }
3141
32- const result = await collectRrwebEvents ( session , shouldSendRrwebCode ? rrwebCode : null ) ;
42+ let result = await collectRrwebEvents ( session , shouldSendRrwebCode ? rrwebCode : undefined ) ;
3343
44+ let debugBrowserId = "" ;
45+ if ( debug . enabled ) {
46+ debugBrowserId = `${ session ?. capabilities ?. browserName } ${ session ?. capabilities ?. browserVersion } :${ session ?. sessionId } ` ;
47+ }
48+
49+ if ( result . isRrwebSupported === false ) {
50+ debug ( "rrweb is not supported in browser %s, error: %s" , debugBrowserId , result . evalError ) ;
51+ sessionsWithUnsupportedRrweb . add ( session ) ;
52+
53+ return [ ] ;
54+ }
55+
56+ // If rrweb is installed (success) or we already provided the code (no point in trying again)
3457 if ( result . isRrwebInstalled || shouldSendRrwebCode ) {
3558 return result . rrwebEvents ;
3659 }
3760
38- return ( await collectRrwebEvents ( session , rrwebCode ) ) . rrwebEvents ;
61+ debug ( "collectRrwebEvents, rrweb was not installed, sending code again" ) ;
62+
63+ result = await collectRrwebEvents ( session , rrwebCode ) ;
64+
65+ if ( result . isRrwebSupported === false ) {
66+ debug ( "rrweb is not supported in browser %s, error: %s" , debugBrowserId , result . evalError ) ;
67+ sessionsWithUnsupportedRrweb . add ( session ) ;
68+
69+ return [ ] ;
70+ }
71+
72+ return result . rrwebEvents ;
3973 } ) ;
4074}
4175
@@ -46,9 +80,9 @@ export async function cleanupRrweb(session: WebdriverIO.Browser, callstack: Call
4680 try {
4781 // @ts -expect-error
4882 const rrwebEvents = window . rrwebEvents ;
49- const rrwebData = rrwebEvents ? .testplane ;
83+ const rrwebData = rrwebEvents && rrwebEvents . testplane ;
5084
51- if ( rrwebData ? .stopRecording ) {
85+ if ( rrwebData && rrwebData . stopRecording ) {
5286 try {
5387 rrwebData . stopRecording ( ) ;
5488 } catch ( e ) {
@@ -57,8 +91,8 @@ export async function cleanupRrweb(session: WebdriverIO.Browser, callstack: Call
5791 }
5892
5993 try {
60- const colorSchemeMedia = rrwebData ? .colorSchemeMedia ;
61- const colorSchemeListener = rrwebData ? .colorSchemeListener ;
94+ const colorSchemeMedia = rrwebData && rrwebData . colorSchemeMedia ;
95+ const colorSchemeListener = rrwebData && rrwebData . colorSchemeListener ;
6296
6397 if ( colorSchemeMedia && colorSchemeListener ) {
6498 colorSchemeMedia . removeEventListener ( "change" , colorSchemeListener ) ;
@@ -67,7 +101,7 @@ export async function cleanupRrweb(session: WebdriverIO.Browser, callstack: Call
67101 /**/
68102 }
69103
70- if ( rrwebData ? .isInstalledByTestplane ) {
104+ if ( rrwebData && rrwebData . isInstalledByTestplane ) {
71105 // @ts -expect-error
72106 delete window . rrweb ;
73107
@@ -86,21 +120,22 @@ export async function cleanupRrweb(session: WebdriverIO.Browser, callstack: Call
86120 } catch ( e ) {
87121 /**/
88122 } finally {
89- sessionsWithRrwebRequested . delete ( session ) ;
123+ sessionsWithRrwebSent . delete ( session ) ;
124+ sessionsWithUnsupportedRrweb . delete ( session ) ;
90125 }
91126}
92127
93128function collectRrwebEvents (
94129 session : WebdriverIO . Browser ,
95- rrwebRecordFnCode : string | null ,
130+ rrwebRecordFnCode : string | undefined ,
96131) : Promise < CollectRrwebEventsResult > {
97132 return session . execute (
98133 ( rrwebRecordFnCode , serverTime ) => {
99134 const isRrwebInstalled = ( ) : boolean => {
100135 try {
101136 // @ts -expect-error
102137 return Boolean ( window . rrweb ) ;
103- } catch {
138+ } catch ( e ) {
104139 return false ;
105140 }
106141 } ;
@@ -112,7 +147,7 @@ function collectRrwebEvents(
112147 result = window . rrwebEvents . slice ( window . lastProcessedRrwebEvent + 1 ) ;
113148 // @ts -expect-error
114149 window . lastProcessedRrwebEvent = window . rrwebEvents . length - 1 ;
115- } catch {
150+ } catch ( e ) {
116151 result = [ ] ;
117152 }
118153
@@ -159,7 +194,17 @@ function collectRrwebEvents(
159194
160195 try {
161196 if ( ! isRrwebInstalled ( ) && rrwebRecordFnCode ) {
162- window . eval ( rrwebRecordFnCode ) ;
197+ try {
198+ window . eval ( rrwebRecordFnCode ) ;
199+ } catch ( e ) {
200+ return {
201+ isRrwebSupported : false ,
202+ isRrwebInstalled : false ,
203+ rrwebEvents : [ ] ,
204+ evalError : String ( e ) ,
205+ } ;
206+ }
207+
163208 // @ts -expect-error
164209 window . lastProcessedRrwebEvent = - 1 ;
165210 // @ts -expect-error
0 commit comments