@@ -52,6 +52,11 @@ export async function createYamlPlayer(
5252) : Promise < ScriptPlayer < MidsceneYamlScriptEnv > > {
5353 const yamlScript =
5454 script || parseYamlScript ( readFileSync ( file , 'utf-8' ) , file ) ;
55+
56+ // Deep clone the script to avoid mutation issues when the same file is executed multiple times
57+ // This ensures each ScriptPlayer instance has its own independent copy of the YAML data
58+ const clonedYamlScript = structuredClone ( yamlScript ) ;
59+
5560 const fileName = basename ( file , extname ( file ) ) ;
5661 const preference = {
5762 headed : options ?. headed ,
@@ -60,25 +65,27 @@ export async function createYamlPlayer(
6065 } ;
6166
6267 const player = new ScriptPlayer (
63- yamlScript ,
68+ clonedYamlScript ,
6469 async ( ) => {
6570 const freeFn : FreeFn [ ] = [ ] ;
66- const webTarget = yamlScript . web || yamlScript . target ;
71+ const webTarget = clonedYamlScript . web || clonedYamlScript . target ;
6772
6873 // Validate that only one target type is specified
6974 const targetCount = [
7075 typeof webTarget !== 'undefined' ,
71- typeof yamlScript . android !== 'undefined' ,
72- typeof yamlScript . ios !== 'undefined' ,
73- typeof yamlScript . interface !== 'undefined' ,
76+ typeof clonedYamlScript . android !== 'undefined' ,
77+ typeof clonedYamlScript . ios !== 'undefined' ,
78+ typeof clonedYamlScript . interface !== 'undefined' ,
7479 ] . filter ( Boolean ) . length ;
7580
7681 if ( targetCount > 1 ) {
7782 const specifiedTargets = [
7883 typeof webTarget !== 'undefined' ? 'web' : null ,
79- typeof yamlScript . android !== 'undefined' ? 'android' : null ,
80- typeof yamlScript . ios !== 'undefined' ? 'ios' : null ,
81- typeof yamlScript . interface !== 'undefined' ? 'interface' : null ,
84+ typeof clonedYamlScript . android !== 'undefined' ? 'android' : null ,
85+ typeof clonedYamlScript . ios !== 'undefined' ? 'ios' : null ,
86+ typeof clonedYamlScript . interface !== 'undefined'
87+ ? 'interface'
88+ : null ,
8289 ] . filter ( Boolean ) ;
8390
8491 throw new Error (
@@ -88,7 +95,7 @@ export async function createYamlPlayer(
8895
8996 // handle new web config
9097 if ( typeof webTarget !== 'undefined' ) {
91- if ( typeof yamlScript . target !== 'undefined' ) {
98+ if ( typeof clonedYamlScript . target !== 'undefined' ) {
9299 console . warn (
93100 'target is deprecated, please use web instead. See https://midscenejs.com/automate-with-scripts-in-yaml for more information. Sorry for the inconvenience.' ,
94101 ) ;
@@ -123,7 +130,7 @@ export async function createYamlPlayer(
123130 {
124131 ...preference ,
125132 cache : processCacheConfig (
126- yamlScript . agent ?. cache ,
133+ clonedYamlScript . agent ?. cache ,
127134 fileName ,
128135 fileName ,
129136 ) ,
@@ -156,7 +163,7 @@ export async function createYamlPlayer(
156163 const agent = new AgentOverChromeBridge ( {
157164 closeNewTabsAfterDisconnect : webTarget . closeNewTabsAfterDisconnect ,
158165 cache : processCacheConfig (
159- yamlScript . agent ?. cache ,
166+ clonedYamlScript . agent ?. cache ,
160167 fileName ,
161168 fileName ,
162169 ) ,
@@ -183,11 +190,11 @@ export async function createYamlPlayer(
183190 }
184191
185192 // handle android
186- if ( typeof yamlScript . android !== 'undefined' ) {
187- const androidTarget = yamlScript . android ;
193+ if ( typeof clonedYamlScript . android !== 'undefined' ) {
194+ const androidTarget = clonedYamlScript . android ;
188195 const agent = await agentFromAdbDevice ( androidTarget ?. deviceId , {
189196 cache : processCacheConfig (
190- yamlScript . agent ?. cache ,
197+ clonedYamlScript . agent ?. cache ,
191198 fileName ,
192199 fileName ,
193200 ) ,
@@ -206,8 +213,8 @@ export async function createYamlPlayer(
206213 }
207214
208215 // handle iOS
209- if ( typeof yamlScript . ios !== 'undefined' ) {
210- const iosTarget = yamlScript . ios ;
216+ if ( typeof clonedYamlScript . ios !== 'undefined' ) {
217+ const iosTarget = clonedYamlScript . ios ;
211218 const agent = await agentFromWebDriverAgent ( {
212219 wdaPort : iosTarget ?. wdaPort ,
213220 wdaHost : iosTarget ?. wdaHost ,
@@ -226,8 +233,8 @@ export async function createYamlPlayer(
226233 }
227234
228235 // handle general interface
229- if ( typeof yamlScript . interface !== 'undefined' ) {
230- const interfaceTarget = yamlScript . interface ;
236+ if ( typeof clonedYamlScript . interface !== 'undefined' ) {
237+ const interfaceTarget = clonedYamlScript . interface ;
231238
232239 const moduleSpecifier = interfaceTarget . module ;
233240 let finalModuleSpecifier : string ;
@@ -269,9 +276,9 @@ export async function createYamlPlayer(
269276 // create agent from device
270277 debug ( 'creating agent from device' , device ) ;
271278 const agent = createAgent ( device , {
272- ...yamlScript . agent ,
279+ ...clonedYamlScript . agent ,
273280 cache : processCacheConfig (
274- yamlScript . agent ?. cache ,
281+ clonedYamlScript . agent ?. cache ,
275282 fileName ,
276283 fileName ,
277284 ) ,
0 commit comments