@@ -65,7 +65,8 @@ pub(crate) async fn execute<R: Runtime>(
6565 let error_tx = tx;
6666
6767 // Listen for the result event from the frontend - must listen on the window for window.emit to work
68- let listener_id = window. listen ( & event_id, move |event| {
68+ let window_clone = window. clone ( ) ;
69+ let listener_id = window_clone. listen ( & event_id, move |event| {
6970 log:: trace!( "Received result event payload: {}" , event. payload( ) ) ;
7071
7172 if let Ok ( payload) = serde_json:: from_str :: < serde_json:: Value > ( event. payload ( ) ) {
@@ -96,7 +97,7 @@ pub(crate) async fn execute<R: Runtime>(
9697 // Wrap the script to:
9798 // 1. Wait for window.__TAURI__.core.invoke to be available (handles race condition)
9899 // 2. Execute the user's script
99- // 3. Emit the result via window .emit (not app-level emit )
100+ // 3. Emit the result via getCurrentWindow() .emit (window-specific event )
100101 let script_with_result = format ! (
101102 r#"
102103 (async () => {{
@@ -114,11 +115,18 @@ pub(crate) async fn execute<R: Runtime>(
114115 // Execute the user's script
115116 const result = await ({});
116117
117- // Emit the result via window.emit (webview window event)
118- window.emit('{}', {{ success: true, value: result }});
118+ // Emit the result via getCurrentWindow().emit (window-specific event)
119+ // This matches window.listen() on the Rust side
120+ const {{ getCurrentWindow }} = await import('@tauri-apps/api/window');
121+ await getCurrentWindow().emit('{}', {{ success: true, value: result }});
119122 }} catch (error) {{
120- // Emit error via window.emit
121- window.emit('{}', {{ success: false, error: error.message || String(error) }});
123+ // Emit error via getCurrentWindow().emit
124+ try {{
125+ const {{ getCurrentWindow }} = await import('@tauri-apps/api/window');
126+ await getCurrentWindow().emit('{}', {{ success: false, error: error.message || String(error) }});
127+ }} catch (emitError) {{
128+ console.error('[WDIO Execute] Failed to emit result:', emitError);
129+ }}
122130 }}
123131 }})();
124132 "# ,
@@ -130,7 +138,7 @@ pub(crate) async fn execute<R: Runtime>(
130138 // Evaluate the script
131139 if let Err ( e) = window. eval ( & script_with_result) {
132140 log:: error!( "Failed to eval script: {}" , e) ;
133- window . unlisten ( listener_id) ;
141+ window_clone . unlisten ( listener_id) ;
134142 return Err ( crate :: Error :: ExecuteError ( format ! ( "Failed to eval script: {}" , e) ) ) ;
135143 }
136144
@@ -141,18 +149,18 @@ pub(crate) async fn execute<R: Runtime>(
141149 Ok ( Ok ( result) ) => {
142150 log:: debug!( "Execute completed successfully" ) ;
143151 log:: trace!( "Result: {:?}" , result) ;
144- window . unlisten ( listener_id) ;
152+ window_clone . unlisten ( listener_id) ;
145153 Ok ( result)
146154 }
147155 Ok ( Err ( e) ) => {
148156 log:: error!( "JS error during execution: {}" , e) ;
149- window . unlisten ( listener_id) ;
157+ window_clone . unlisten ( listener_id) ;
150158 Err ( e)
151159 }
152160 Err ( _) => {
153161 log:: error!( "Timeout waiting for execute result after 10s. Event ID: {}. Window: {}" ,
154162 event_id, window_label) ;
155- window . unlisten ( listener_id) ;
163+ window_clone . unlisten ( listener_id) ;
156164 Err ( crate :: Error :: ExecuteError ( format ! (
157165 "Script execution timed out after 10s. Event ID: {}. Window: {}" ,
158166 event_id, window_label
0 commit comments