@@ -3,8 +3,9 @@ import { Callstack } from "./callstack";
33import * as cmds from "./commands" ;
44import { isGroup , normalizeCommandArgs , runWithHooks , shouldRecordSnapshots } from "./utils" ;
55import { BrowserConfig } from "../../config/browser-config" ;
6- import { TestStep , TestStepKey } from "../../types" ;
7- import { filterEvents , installRrwebAndCollectEvents , sendFilteredEvents } from "./rrweb" ;
6+ import { TestStepKey } from "../../types" ;
7+ import type { Test , TestStep } from "../../types" ;
8+ import { cleanupRrweb , filterEvents , installRrwebAndCollectEvents , sendFilteredEvents } from "./rrweb" ;
89import { getHistoryContext , runWithHistoryContext } from "./async-local-storage" ;
910
1011const debug = makeDebug ( "testplane:browser:history" ) ;
@@ -61,10 +62,64 @@ interface RunWithHistoryHooksData<T> extends HooksData {
6162 fn : ( ) => T ;
6263}
6364
65+ interface RequestDomSnapshotsData extends HooksData {
66+ attempt ?: number ;
67+ currentTest ?: Test ;
68+ }
69+
6470export const runWithoutHistory = async < T > ( _ : unknown , fn : ( ) => T ) : Promise < T > => {
6571 return runWithHistoryContext ( { shouldBypassHistory : true } , fn ) as T ;
6672} ;
6773
74+ export const requestDomSnapshots = ( {
75+ session,
76+ callstack,
77+ snapshotsPromiseRef,
78+ config,
79+ attempt,
80+ currentTest,
81+ } : RequestDomSnapshotsData ) : void => {
82+ try {
83+ if ( ! callstack ) {
84+ return ;
85+ }
86+
87+ const timeTravelMode = config . timeTravel . mode ;
88+ const isRetry = ( attempt ?? session . executionContext ?. ctx ?. attempt ?? 0 ) > 0 ;
89+ const shouldRecord = shouldRecordSnapshots ( timeTravelMode , isRetry ) ;
90+ const test = currentTest ?? session . executionContext ?. ctx ?. currentTest ;
91+
92+ if ( shouldRecord && process . send && test ) {
93+ const rrwebPromise = installRrwebAndCollectEvents ( session , callstack )
94+ . then ( rrwebEvents => {
95+ const rrwebEventsFiltered = filterEvents ( rrwebEvents ) ;
96+ sendFilteredEvents ( test , rrwebEventsFiltered ) ;
97+ } )
98+ . catch ( e => {
99+ debug ( "An error occurred during capturing snapshots in browser: %O" , e ) ;
100+ } ) ;
101+
102+ snapshotsPromiseRef . current = snapshotsPromiseRef . current . then ( ( ) => rrwebPromise ) ;
103+ }
104+ } catch ( e ) {
105+ debug ( "An error occurred during capturing snapshots in browser: %O" , e ) ;
106+ }
107+ } ;
108+
109+ type CleanupDomSnapshotsData = Pick < HooksData , "session" | "callstack" > ;
110+
111+ export const cleanupDomSnapshots = async ( { session, callstack } : CleanupDomSnapshotsData ) : Promise < void > => {
112+ if ( ! callstack ) {
113+ return ;
114+ }
115+
116+ try {
117+ await cleanupRrweb ( session , callstack ) ;
118+ } catch ( e ) {
119+ debug ( "An error occurred during cleaning up snapshots in browser: %O" , e ) ;
120+ }
121+ } ;
122+
68123const runWithHistoryHooks = < T > ( {
69124 session,
70125 callstack,
@@ -88,30 +143,14 @@ const runWithHistoryHooks = <T>({
88143
89144 if ( typeof ( result as Promise < unknown > | undefined ) ?. then === "function" ) {
90145 try {
91- const timeTravelMode = config . timeTravel . mode ;
92- const isRetry = ( session . executionContext ?. ctx ?. attempt ?? 0 ) > 0 ;
93- const shouldRecord = shouldRecordSnapshots ( timeTravelMode , isRetry ) ;
94146 const isInterestingStep =
95147 ! nodeData . name . startsWith ( "is" ) &&
96148 ! nodeData . name . startsWith ( "get" ) &&
97149 ! nodeData . name . startsWith ( "$" ) &&
98150 ! nodeData . name . startsWith ( "wait" ) ;
99151
100- if (
101- shouldRecord &&
102- process . send &&
103- session . executionContext ?. ctx ?. currentTest &&
104- isInterestingStep
105- ) {
106- const rrwebPromise = installRrwebAndCollectEvents ( session , callstack )
107- ?. then ( rrwebEvents => {
108- const rrwebEventsFiltered = filterEvents ( rrwebEvents ) ;
109- sendFilteredEvents ( session , rrwebEventsFiltered ) ;
110- } )
111- . catch ( e => {
112- debug ( "An error occurred during capturing snapshots in browser: %O" , e ) ;
113- } ) ;
114- snapshotsPromiseRef . current = snapshotsPromiseRef . current . then ( ( ) => rrwebPromise ) ;
152+ if ( isInterestingStep ) {
153+ requestDomSnapshots ( { session, callstack, snapshotsPromiseRef, config } ) ;
115154 }
116155 } catch ( e ) {
117156 debug ( "An error occurred during capturing snapshots in browser: %O" , e ) ;
0 commit comments