tauri-plugin-playwright 0.2.2

Tauri plugin that enables Playwright E2E testing by embedding a control server in the app
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::sync::{oneshot, Mutex};
use tauri::{AppHandle, Manager, Runtime, WebviewWindow};

use crate::commands::{Command, Response};
use crate::native_capture::RecordingSession;

static COUNTER: AtomicU64 = AtomicU64::new(0);

pub type PendingResults = Arc<Mutex<HashMap<String, oneshot::Sender<String>>>>;
pub type RecordingState = Arc<Mutex<Option<RecordingSession>>>;

pub fn start<R: Runtime>(
    app: AppHandle<R>,
    pending: PendingResults,
    socket_path: Option<String>,
    tcp_port: Option<u16>,
    window_label: Option<String>,
) {
    let app = Arc::new(app);
    let recording: RecordingState = Arc::new(Mutex::new(None));
    let window_label = Arc::new(window_label.unwrap_or_else(|| "main".to_string()));

    #[cfg(unix)]
    if let Some(path) = socket_path {
        let app = Arc::clone(&app);
        let pending = Arc::clone(&pending);
        let recording = Arc::clone(&recording);
        let window_label = Arc::clone(&window_label);
        tauri::async_runtime::spawn(async move {
            if let Err(e) = run_unix_server(app, pending, recording, &window_label, &path).await {
                eprintln!("tauri-plugin-playwright: unix server error: {}", e);
            }
        });
        return;
    }

    let port = tcp_port.unwrap_or(6274);
    let pending = Arc::clone(&pending);
    let recording = Arc::clone(&recording);
    let window_label = Arc::clone(&window_label);
    tauri::async_runtime::spawn(async move {
        if let Err(e) = run_tcp_server(app, pending, recording, &window_label, port).await {
            eprintln!("tauri-plugin-playwright: tcp server error: {}", e);
        }
    });
}

#[cfg(unix)]
async fn run_unix_server<R: Runtime>(
    app: Arc<AppHandle<R>>,
    pending: PendingResults,
    recording: RecordingState,
    window_label: &str,
    path: &str,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let _ = std::fs::remove_file(path);
    let listener = tokio::net::UnixListener::bind(path)?;
    eprintln!("tauri-plugin-playwright: listening on unix:{}", path);

    loop {
        let (stream, _) = listener.accept().await?;
        let app = Arc::clone(&app);
        let pending = Arc::clone(&pending);
        let recording = Arc::clone(&recording);
        let wl = window_label.to_string();
        tauri::async_runtime::spawn(async move {
            let (reader, writer) = stream.into_split();
            handle_connection(app, pending, recording, &wl, reader, writer).await;
        });
    }
}

async fn run_tcp_server<R: Runtime>(
    app: Arc<AppHandle<R>>,
    pending: PendingResults,
    recording: RecordingState,
    window_label: &str,
    port: u16,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let listener = tokio::net::TcpListener::bind(format!("127.0.0.1:{}", port)).await?;
    eprintln!("tauri-plugin-playwright: listening on tcp://127.0.0.1:{}", port);

    loop {
        let (stream, _) = listener.accept().await?;
        let app = Arc::clone(&app);
        let pending = Arc::clone(&pending);
        let recording = Arc::clone(&recording);
        let wl = window_label.to_string();
        tauri::async_runtime::spawn(async move {
            let (reader, writer) = stream.into_split();
            handle_connection(app, pending, recording, &wl, reader, writer).await;
        });
    }
}

async fn handle_connection<R: Runtime, Reader, Writer>(
    app: Arc<AppHandle<R>>,
    pending: PendingResults,
    recording: RecordingState,
    window_label: &str,
    reader: Reader,
    mut writer: Writer,
) where
    Reader: tokio::io::AsyncRead + Unpin,
    Writer: tokio::io::AsyncWrite + Unpin,
{
    let mut lines = BufReader::new(reader).lines();

    while let Ok(Some(line)) = lines.next_line().await {
        let line = line.trim().to_string();
        if line.is_empty() { continue; }

        let response = match serde_json::from_str::<Command>(&line) {
            Ok(cmd) => execute_command(&app, &pending, &recording, window_label, cmd).await,
            Err(e) => Response::err(format!("invalid command: {}", e)),
        };

        let mut json = serde_json::to_string(&response).unwrap_or_else(|_| {
            r#"{"ok":false,"error":"serialize error"}"#.to_string()
        });
        json.push('\n');

        if writer.write_all(json.as_bytes()).await.is_err() { break; }
        if writer.flush().await.is_err() { break; }
    }
}

fn json_str(s: &str) -> String {
    serde_json::to_string(s).unwrap()
}

/// Generate JS that auto-waits for an element to be visible + enabled, then runs an action.
/// The action_body has access to `el` (the found element).
fn action_js(selector: &str, timeout_ms: u64, action_body: &str) -> String {
    let s = json_str(selector);
    format!(
        r#"(async function(){{ var dl=Date.now()+{t}; while(Date.now()<dl){{ var el=document.querySelector({s}); if(el){{ var r=el.getBoundingClientRect(); var st=getComputedStyle(el); if(r.width>0&&r.height>0&&st.visibility!=='hidden'&&st.display!=='none'&&parseFloat(st.opacity)>0){{ {action}; }} }} await new Promise(function(r){{setTimeout(r,50)}}); }} throw new Error('timeout ('+{t}+'ms) waiting for '+{s}); }})()"#,
        s=s, t=timeout_ms, action=action_body
    )
}

/// Generate JS that auto-waits for an element to exist, then returns a value.
/// The return_expr has access to `el` (the found element).
fn query_js(selector: &str, timeout_ms: u64, return_expr: &str) -> String {
    let s = json_str(selector);
    format!(
        r#"(async function(){{ var dl=Date.now()+{t}; while(Date.now()<dl){{ var el=document.querySelector({s}); if(el){{ return {ret}; }} await new Promise(function(r){{setTimeout(r,50)}}); }} throw new Error('timeout ('+{t}+'ms) waiting for '+{s}); }})()"#,
        s=s, t=timeout_ms, ret=return_expr
    )
}

async fn execute_command<R: Runtime>(
    app: &Arc<AppHandle<R>>,
    pending: &PendingResults,
    recording: &RecordingState,
    window_label: &str,
    cmd: Command,
) -> Response {
    match cmd {
        Command::Ping => Response::ok(serde_json::json!("pong")),
        Command::Eval { script } => eval_js(app, pending, window_label, &script).await,

        // ── Actions (auto-wait for visible + enabled) ─────────────────

        Command::Click { selector, timeout_ms } => {
            eval_js(app, pending, window_label, &action_js(&selector, timeout_ms,
                "el.scrollIntoView({block:'center'}); el.click(); return null"
            )).await
        }
        Command::Dblclick { selector, timeout_ms } => {
            eval_js(app, pending, window_label, &action_js(&selector, timeout_ms,
                "el.scrollIntoView({block:'center'}); el.dispatchEvent(new MouseEvent('dblclick',{bubbles:true})); return null"
            )).await
        }
        Command::Hover { selector, timeout_ms } => {
            eval_js(app, pending, window_label, &action_js(&selector, timeout_ms,
                "el.scrollIntoView({block:'center'}); var r2=el.getBoundingClientRect(); var cx=r2.left+r2.width/2; var cy=r2.top+r2.height/2; el.dispatchEvent(new MouseEvent('mouseenter',{bubbles:true,clientX:cx,clientY:cy})); el.dispatchEvent(new MouseEvent('mouseover',{bubbles:true,clientX:cx,clientY:cy})); return null"
            )).await
        }
        Command::Fill { selector, text, timeout_ms } => {
            let t = json_str(&text);
            eval_js(app, pending, window_label, &action_js(&selector, timeout_ms, &format!(
                "el.focus(); var desc=Object.getOwnPropertyDescriptor(HTMLInputElement.prototype,'value'); if(desc&&desc.set) desc.set.call(el,{t}); else el.value={t}; el.dispatchEvent(new Event('input',{{bubbles:true}})); el.dispatchEvent(new Event('change',{{bubbles:true}})); return null", t=t
            ))).await
        }
        Command::TypeText { selector, text, timeout_ms } => {
            let t = json_str(&text);
            eval_js(app, pending, window_label, &action_js(&selector, timeout_ms, &format!(
                "el.focus(); for(var i=0;i<{t}.length;i++){{ var ch={t}[i]; el.dispatchEvent(new KeyboardEvent('keydown',{{key:ch,bubbles:true}})); var desc=Object.getOwnPropertyDescriptor(HTMLInputElement.prototype,'value'); if(desc&&desc.set) desc.set.call(el,el.value+ch); else el.value+=ch; el.dispatchEvent(new Event('input',{{bubbles:true}})); el.dispatchEvent(new KeyboardEvent('keyup',{{key:ch,bubbles:true}})); }} return null", t=t
            ))).await
        }
        Command::Press { selector, key, timeout_ms } => {
            let k = json_str(&key);
            eval_js(app, pending, window_label, &action_js(&selector, timeout_ms, &format!(
                "el.focus(); var o={{key:{k},bubbles:true}}; el.dispatchEvent(new KeyboardEvent('keydown',o)); el.dispatchEvent(new KeyboardEvent('keypress',o)); el.dispatchEvent(new KeyboardEvent('keyup',o)); return null", k=k
            ))).await
        }
        Command::Check { selector, timeout_ms } => {
            eval_js(app, pending, window_label, &action_js(&selector, timeout_ms,
                "if(!el.checked){ el.click(); } return null"
            )).await
        }
        Command::Uncheck { selector, timeout_ms } => {
            eval_js(app, pending, window_label, &action_js(&selector, timeout_ms,
                "if(el.checked){ el.click(); } return null"
            )).await
        }
        Command::SelectOption { selector, value, timeout_ms } => {
            let v = json_str(&value);
            eval_js(app, pending, window_label, &action_js(&selector, timeout_ms, &format!(
                "el.value={v}; el.dispatchEvent(new Event('change',{{bubbles:true}})); return el.value", v=v
            ))).await
        }
        Command::Focus { selector, timeout_ms } => {
            eval_js(app, pending, window_label, &query_js(&selector, timeout_ms,
                "(function(){ el.focus(); return null; })()"
            )).await
        }
        Command::Blur { selector, timeout_ms } => {
            eval_js(app, pending, window_label, &query_js(&selector, timeout_ms,
                "(function(){ el.blur(); return null; })()"
            )).await
        }
        Command::DragAndDrop { source, target, timeout_ms } => {
            let src = json_str(&source);
            let tgt = json_str(&target);
            eval_js(app, pending, window_label, &format!(
                r#"(async function(){{
                    var dl=Date.now()+{t};
                    while(Date.now()<dl){{
                        var s=document.querySelector({src}); var t=document.querySelector({tgt});
                        if(s&&t){{
                            s.scrollIntoView({{block:'center'}});
                            var sr=s.getBoundingClientRect(); var tr=t.getBoundingClientRect();
                            var sx=sr.left+sr.width/2,sy=sr.top+sr.height/2;
                            var tx=tr.left+tr.width/2,ty=tr.top+tr.height/2;
                            var dt=new DataTransfer();
                            s.dispatchEvent(new DragEvent('dragstart',{{bubbles:true,clientX:sx,clientY:sy,dataTransfer:dt}}));
                            s.dispatchEvent(new DragEvent('drag',{{bubbles:true,clientX:sx,clientY:sy,dataTransfer:dt}}));
                            t.dispatchEvent(new DragEvent('dragenter',{{bubbles:true,clientX:tx,clientY:ty,dataTransfer:dt}}));
                            t.dispatchEvent(new DragEvent('dragover',{{bubbles:true,clientX:tx,clientY:ty,dataTransfer:dt}}));
                            t.dispatchEvent(new DragEvent('drop',{{bubbles:true,clientX:tx,clientY:ty,dataTransfer:dt}}));
                            s.dispatchEvent(new DragEvent('dragend',{{bubbles:true,clientX:tx,clientY:ty,dataTransfer:dt}}));
                            return null;
                        }}
                        await new Promise(function(r){{setTimeout(r,50)}});
                    }}
                    throw new Error('timeout waiting for drag source/target');
                }})()"#, src=src, tgt=tgt, t=timeout_ms
            )).await
        }
        Command::SetInputFiles { selector, files, timeout_ms } => {
            let files_json: Vec<String> = files.iter().map(|f| {
                format!(r#"{{"name":{},"mime":{},"b64":{}}}"#,
                    json_str(&f.name), json_str(&f.mime_type), json_str(&f.base64))
            }).collect();
            let arr = format!("[{}]", files_json.join(","));
            eval_js(app, pending, window_label, &query_js(&selector, timeout_ms, &format!(
                r#"(function(){{ var files={arr}; var dt=new DataTransfer(); for(var i=0;i<files.length;i++){{ var f=files[i]; var bin=atob(f.b64); var bytes=new Uint8Array(bin.length); for(var j=0;j<bin.length;j++) bytes[j]=bin.charCodeAt(j); dt.items.add(new File([bytes],f.name,{{type:f.mime}})); }} el.files=dt.files; el.dispatchEvent(new Event('change',{{bubbles:true}})); return el.files.length; }})()"#, arr=arr
            ))).await
        }

        // ── Queries (auto-wait for element to exist) ──────────────────

        Command::TextContent { selector, timeout_ms } => {
            eval_js(app, pending, window_label, &query_js(&selector, timeout_ms, "el.textContent")).await
        }
        Command::InnerHtml { selector, timeout_ms } => {
            eval_js(app, pending, window_label, &query_js(&selector, timeout_ms, "el.innerHTML")).await
        }
        Command::InnerText { selector, timeout_ms } => {
            eval_js(app, pending, window_label, &query_js(&selector, timeout_ms, "el.innerText")).await
        }
        Command::GetAttribute { selector, name, timeout_ms } => {
            let n = json_str(&name);
            eval_js(app, pending, window_label, &query_js(&selector, timeout_ms, &format!("el.getAttribute({n})", n=n))).await
        }
        Command::InputValue { selector, timeout_ms } => {
            eval_js(app, pending, window_label, &query_js(&selector, timeout_ms, "el.value||''")).await
        }
        Command::BoundingBox { selector, timeout_ms } => {
            eval_js(app, pending, window_label, &query_js(&selector, timeout_ms,
                "(function(){ var r2=el.getBoundingClientRect(); return {x:r2.left,y:r2.top,width:r2.width,height:r2.height}; })()"
            )).await
        }

        // ── State checks (instant, no retry) ──────────────────────────

        Command::IsVisible { selector } => {
            let s = json_str(&selector);
            eval_js(app, pending, window_label, &format!(
                r#"(function(){{ var el=document.querySelector({s}); if(!el) return false; var r=el.getBoundingClientRect(); var st=getComputedStyle(el); return r.width>0&&r.height>0&&st.visibility!=='hidden'&&st.display!=='none'&&parseFloat(st.opacity)>0; }})()"#, s=s
            )).await
        }
        Command::IsChecked { selector } => {
            let s = json_str(&selector);
            eval_js(app, pending, window_label, &format!(
                r#"(function(){{ var el=document.querySelector({s}); if(!el) return false; return !!el.checked; }})()"#, s=s
            )).await
        }
        Command::IsDisabled { selector } => {
            let s = json_str(&selector);
            eval_js(app, pending, window_label, &format!(
                r#"(function(){{ var el=document.querySelector({s}); if(!el) return true; return el.disabled===true||el.hasAttribute('disabled'); }})()"#, s=s
            )).await
        }
        Command::IsEditable { selector } => {
            let s = json_str(&selector);
            eval_js(app, pending, window_label, &format!(
                r#"(function(){{ var el=document.querySelector({s}); if(!el) return false; if(el.disabled||el.readOnly) return false; var tag=el.tagName; return tag==='INPUT'||tag==='TEXTAREA'||tag==='SELECT'||el.isContentEditable; }})()"#, s=s
            )).await
        }

        // ── Bulk queries (no retry, work on zero matches) ─────────────

        Command::AllTextContents { selector } => {
            let s = json_str(&selector);
            eval_js(app, pending, window_label, &format!(
                r#"Array.from(document.querySelectorAll({s})).map(function(el){{ return el.textContent||''; }})"#, s=s
            )).await
        }
        Command::AllInnerTexts { selector } => {
            let s = json_str(&selector);
            eval_js(app, pending, window_label, &format!(
                r#"Array.from(document.querySelectorAll({s})).map(function(el){{ return el.innerText||''; }})"#, s=s
            )).await
        }
        Command::Count { selector } => {
            let s = json_str(&selector);
            eval_js(app, pending, window_label, &format!(r#"document.querySelectorAll({s}).length"#, s=s)).await
        }

        // ── Waiting ───────────────────────────────────────────────────

        Command::WaitForSelector { selector, timeout_ms } => {
            let s = json_str(&selector);
            eval_js(app, pending, window_label, &format!(
                r#"(async function(){{ var dl=Date.now()+{t}; while(Date.now()<dl){{ var el=document.querySelector({s}); if(el){{ var r=el.getBoundingClientRect(); var st=getComputedStyle(el); if(r.width>0&&r.height>0&&st.visibility!=='hidden'&&st.display!=='none') return true; }} await new Promise(function(r){{setTimeout(r,50)}}); }} throw new Error('timeout waiting for '+{s}); }})()"#,
                s=s, t=timeout_ms
            )).await
        }
        Command::WaitForFunction { expression, timeout_ms } => {
            let e = json_str(&expression);
            eval_js(app, pending, window_label, &format!(
                r#"(async function(){{ var dl=Date.now()+{t}; while(Date.now()<dl){{ try{{ var r=({expr}); if(r) return r; }}catch(ex){{}} await new Promise(function(r){{setTimeout(r,100)}}); }} throw new Error('waitForFunction timeout: '+{e}); }})()"#,
                expr=expression, e=e, t=timeout_ms
            )).await
        }
        Command::Content => {
            eval_js(app, pending, window_label, "document.documentElement.outerHTML").await
        }
        Command::InstallDialogHandler { default_confirm, default_prompt_text } => {
            let confirm_val = if default_confirm { "true" } else { "false" };
            let prompt_val = match &default_prompt_text {
                Some(t) => json_str(t),
                None => "null".to_string(),
            };
            eval_js(app, pending, window_label, &format!(
                r#"(function(){{
                    window.__pw_dialogs=window.__pw_dialogs||[];
                    window.__pw_confirm_val={cv};
                    window.__pw_prompt_val={pv};
                    window.alert=function(m){{ window.__pw_dialogs.push({{type:'alert',message:String(m)}}); }};
                    window.confirm=function(m){{ window.__pw_dialogs.push({{type:'confirm',message:String(m)}}); return window.__pw_confirm_val; }};
                    window.prompt=function(m,d){{ window.__pw_dialogs.push({{type:'prompt',message:String(m),default:d||''}}); return window.__pw_prompt_val; }};
                    return true;
                }})()"#, cv=confirm_val, pv=prompt_val
            )).await
        }
        Command::GetDialogs => {
            eval_js(app, pending, window_label, "window.__pw_dialogs||[]").await
        }
        Command::ClearDialogs => {
            eval_js(app, pending, window_label, "(function(){ window.__pw_dialogs=[]; return null; })()").await
        }
        Command::AddNetworkRoute { pattern, status, body, content_type } => {
            let p = json_str(&pattern);
            let b = json_str(&body);
            let ct = json_str(content_type.as_deref().unwrap_or("application/json"));
            eval_js(app, pending, window_label, &format!(
                r#"(function(){{
                    if(!window.__pw_routes){{
                        window.__pw_routes=[];
                        window.__pw_net_requests=[];
                        var origFetch=window.fetch;
                        window.fetch=function(input,init){{
                            var url=typeof input==='string'?input:(input&&input.url?input.url:'');
                            var method=(init&&init.method)||'GET';
                            window.__pw_net_requests.push({{url:url,method:method,timestamp:Date.now()}});
                            for(var i=0;i<window.__pw_routes.length;i++){{
                                var r=window.__pw_routes[i];
                                if(url.includes(r.pattern)||new RegExp(r.pattern).test(url)){{
                                    return Promise.resolve(new Response(r.body,{{status:r.status,headers:{{'Content-Type':r.ct}}}}));
                                }}
                            }}
                            return origFetch.apply(this,arguments);
                        }};
                        var origOpen=XMLHttpRequest.prototype.open;
                        var origSend=XMLHttpRequest.prototype.send;
                        XMLHttpRequest.prototype.open=function(m,u){{
                            this.__pw_method=m;this.__pw_url=u;
                            return origOpen.apply(this,arguments);
                        }};
                        XMLHttpRequest.prototype.send=function(){{
                            var self=this;
                            window.__pw_net_requests.push({{url:self.__pw_url,method:self.__pw_method,timestamp:Date.now()}});
                            for(var i=0;i<window.__pw_routes.length;i++){{
                                var r=window.__pw_routes[i];
                                if(self.__pw_url&&(self.__pw_url.includes(r.pattern)||new RegExp(r.pattern).test(self.__pw_url))){{
                                    Object.defineProperty(self,'status',{{get:function(){{return r.status}}}});
                                    Object.defineProperty(self,'responseText',{{get:function(){{return r.body}}}});
                                    Object.defineProperty(self,'response',{{get:function(){{return r.body}}}});
                                    Object.defineProperty(self,'readyState',{{get:function(){{return 4}}}});
                                    setTimeout(function(){{self.onreadystatechange&&self.onreadystatechange();self.onload&&self.onload();}},0);
                                    return;
                                }}
                            }}
                            return origSend.apply(this,arguments);
                        }};
                    }}
                    window.__pw_routes.push({{pattern:{p},status:{st},body:{b},ct:{ct}}});
                    return window.__pw_routes.length;
                }})()"#, p=p, st=status, b=b, ct=ct
            )).await
        }
        Command::RemoveNetworkRoute { pattern } => {
            let p = json_str(&pattern);
            eval_js(app, pending, window_label, &format!(
                r#"(function(){{ if(!window.__pw_routes) return 0; window.__pw_routes=window.__pw_routes.filter(function(r){{return r.pattern!=={p}}}); return window.__pw_routes.length; }})()"#, p=p
            )).await
        }
        Command::ClearNetworkRoutes => {
            eval_js(app, pending, window_label, "(function(){ window.__pw_routes=[]; return null; })()").await
        }
        Command::GetNetworkRequests => {
            eval_js(app, pending, window_label, "window.__pw_net_requests||[]").await
        }
        Command::ClearNetworkRequests => {
            eval_js(app, pending, window_label, "(function(){ window.__pw_net_requests=[]; return null; })()").await
        }
        Command::DispatchEvent { selector, event_type, timeout_ms } => {
            let e = json_str(&event_type);
            eval_js(app, pending, window_label, &action_js(&selector, timeout_ms, &format!(
                "el.dispatchEvent(new Event({e},{{bubbles:true}})); return null", e=e
            ))).await
        }
        Command::GetComputedStyle { selector, property, timeout_ms } => {
            let p = json_str(&property);
            eval_js(app, pending, window_label, &query_js(&selector, timeout_ms, &format!(
                "getComputedStyle(el).getPropertyValue({p})", p=p
            ))).await
        }
        Command::IsFocused { selector } => {
            let s = json_str(&selector);
            eval_js(app, pending, window_label, &format!(
                r#"(function(){{ var el=document.querySelector({s}); return el!==null&&document.activeElement===el; }})()"#, s=s
            )).await
        }
        Command::Title => eval_js(app, pending, window_label, "document.title").await,
        Command::Url => eval_js(app, pending, window_label, "window.location.href").await,
        Command::Goto { url } => {
            let u = json_str(&url);
            eval_js(app, pending, window_label, &format!(r#"(function(){{ window.location.href={u}; return null; }})()"#, u=u)).await
        }
        Command::Reload => {
            eval_js(app, pending, window_label, "(function(){ window.location.reload(); return null; })()").await
        }
        Command::GoBack => {
            eval_js(app, pending, window_label, "(function(){ window.history.back(); return null; })()").await
        }
        Command::GoForward => {
            eval_js(app, pending, window_label, "(function(){ window.history.forward(); return null; })()").await
        }
        Command::WaitForUrl { pattern, timeout_ms } => {
            let p = json_str(&pattern);
            eval_js(app, pending, window_label, &format!(
                r#"(async function(){{ var dl=Date.now()+{t}; while(Date.now()<dl){{ if(window.location.href.includes({p})||new RegExp({p}).test(window.location.href)) return window.location.href; await new Promise(function(r){{setTimeout(r,100)}}); }} throw new Error('timeout waiting for URL matching '+{p}); }})()"#,
                p=p, t=timeout_ms
            )).await
        }
        Command::Screenshot { path } => {
            take_screenshot(app, pending, window_label, path).await
        }
        Command::NativeScreenshot { path } => {
            take_native_screenshot(path).await
        }
        Command::StartRecording { path, fps } => {
            let mut rec = recording.lock().await;
            if rec.is_some() {
                return Response::err("recording already in progress");
            }
            match RecordingSession::start(path, fps) {
                Ok(session) => {
                    let dir = session.output_dir.clone();
                    *rec = Some(session);
                    Response::ok(serde_json::json!({ "dir": dir, "fps": fps }))
                }
                Err(e) => Response::err(e),
            }
        }
        Command::StopRecording => {
            let mut rec = recording.lock().await;
            match rec.take() {
                Some(mut session) => {
                    let fps = session.fps;
                    let (dir, frame_count) = session.stop().await;

                    // Try to stitch into video with ffmpeg
                    let video_path = format!("{}/video.mp4", dir);
                    let video = session.stitch(&video_path).await.ok();

                    Response::ok(serde_json::json!({
                        "dir": dir,
                        "frame_count": frame_count,
                        "fps": fps,
                        "video": video,
                    }))
                }
                None => Response::err("no recording in progress"),
            }
        }
    }
}

/// Inject JS into the webview via `WebviewWindow::eval()` and wait for the result
/// to come back through the Tauri IPC `pw_result` command.
async fn eval_js<R: Runtime>(
    app: &Arc<AppHandle<R>>,
    pending: &PendingResults,
    window_label: &str,
    script: &str,
) -> Response {
    let id = format!("pw{}", COUNTER.fetch_add(1, Ordering::SeqCst));

    let (tx, rx) = oneshot::channel::<String>();
    pending.lock().await.insert(id.clone(), tx);

    // Wrap the user script in a try-catch that sends the result back via Tauri IPC.
    // The id is JSON-escaped to prevent injection if the format ever changes.
    let id_json = serde_json::to_string(&id).unwrap();
    let wrapped_script = format!(
        r#"(async () => {{
  const __pw_id = {id_json};
  try {{
    const __pw_result = await ({script});
    window.__TAURI_INTERNALS__.invoke('plugin:playwright|pw_result',
      {{ id: __pw_id, ok: true, data: JSON.stringify(__pw_result) }});
  }} catch(e) {{
    window.__TAURI_INTERNALS__.invoke('plugin:playwright|pw_result',
      {{ id: __pw_id, ok: false, error: String(e && e.message || e) }});
  }}
}})()"#,
        script = script,
        id_json = id_json,
    );

    // Get the webview window, retrying briefly if it's not ready yet (e.g. during app startup).
    let webview: WebviewWindow<R> = {
        let mut attempts = 0;
        loop {
            if let Some(w) = app.get_webview_window(window_label) {
                break w;
            }
            attempts += 1;
            if attempts >= 20 {
                pending.lock().await.remove(&id);
                return Response::err(format!("window '{}' not found after retries", window_label));
            }
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        }
    };

    if let Err(e) = webview.eval(&wrapped_script) {
        pending.lock().await.remove(&id);
        return Response::err(format!("eval failed: {}", e));
    }

    // Wait for the JS to execute and send the result back via IPC
    match tokio::time::timeout(std::time::Duration::from_secs(30), rx).await {
        Ok(Ok(result_json)) => {
            match serde_json::from_str::<serde_json::Value>(&result_json) {
                Ok(v) => {
                    if v.get("ok").and_then(|v| v.as_bool()) == Some(true) {
                        Response::ok(v.get("v").cloned())
                    } else {
                        let msg = v.get("e").and_then(|v| v.as_str()).unwrap_or("unknown JS error").to_string();
                        Response::err(msg)
                    }
                }
                Err(e) => Response::err(format!("parse error: {}", e)),
            }
        }
        Ok(Err(_)) => Response::err("channel dropped".to_string()),
        Err(_) => {
            pending.lock().await.remove(&id);
            Response::err("timeout (30s)".to_string())
        }
    }
}

/// Take a screenshot by loading html2canvas in the webview and rendering to PNG.
async fn take_screenshot<R: Runtime>(
    app: &Arc<AppHandle<R>>,
    pending: &PendingResults,
    window_label: &str,
    path: Option<String>,
) -> Response {
    let script = r#"
(async function() {
  var w = document.documentElement.scrollWidth;
  var h = document.documentElement.scrollHeight;
  var clone = document.documentElement.cloneNode(true);

  // Inline all computed styles
  var styles = '';
  try {
    for (var i = 0; i < document.styleSheets.length; i++) {
      try {
        var rules = document.styleSheets[i].cssRules || document.styleSheets[i].rules;
        if (rules) {
          for (var j = 0; j < rules.length; j++) {
            styles += rules[j].cssText + '\n';
          }
        }
      } catch(e) { /* cross-origin stylesheet, skip */ }
    }
  } catch(e) {}

  var styleEl = document.createElement('style');
  styleEl.textContent = styles;
  clone.querySelector('head').appendChild(styleEl);

  // Remove scripts from clone
  clone.querySelectorAll('script').forEach(function(s) { s.remove(); });

  var html = new XMLSerializer().serializeToString(clone);

  var svg = '<svg xmlns="http://www.w3.org/2000/svg" width="' + w + '" height="' + h + '">' +
    '<foreignObject width="100%" height="100%">' + html + '</foreignObject></svg>';

  var canvas = document.createElement('canvas');
  canvas.width = w;
  canvas.height = h;
  var ctx = canvas.getContext('2d');

  var blob = new Blob([svg], { type: 'image/svg+xml;charset=utf-8' });
  var url = URL.createObjectURL(blob);

  return new Promise(function(resolve, reject) {
    var img = new Image();
    img.onload = function() {
      ctx.drawImage(img, 0, 0);
      URL.revokeObjectURL(url);
      try {
        resolve(canvas.toDataURL('image/png'));
      } catch(e) {
        reject(new Error('canvas tainted: ' + e.message));
      }
    };
    img.onerror = function() {
      URL.revokeObjectURL(url);
      reject(new Error('svg render failed'));
    };
    img.src = url;
  });
})()
"#;

    let result = eval_js(app, pending, window_label, script).await;

    if !result.ok {
        return result;
    }

    // result.data is a data:image/png;base64,... string
    if let Some(serde_json::Value::String(data_url)) = &result.data {
        if let Some(base64_data) = data_url.strip_prefix("data:image/png;base64,") {
            // If a path was provided, save the file
            if let Some(ref file_path) = path {
                match base64_decode(base64_data) {
                    Ok(bytes) => {
                        if let Err(e) = tokio::fs::write(file_path, &bytes).await {
                            return Response::err(format!("write file: {}", e));
                        }
                        return Response::ok(serde_json::json!({
                            "path": file_path,
                            "size": bytes.len()
                        }));
                    }
                    Err(e) => return Response::err(format!("base64 decode: {}", e)),
                }
            }

            // Return base64 and size
            return Response::ok(serde_json::json!({
                "base64": base64_data,
                "size": base64_data.len()
            }));
        }
    }

    Response::err("unexpected screenshot result format".to_string())
}

/// Native screenshot via platform APIs (CoreGraphics on macOS).
async fn take_native_screenshot(path: Option<String>) -> Response {
    let result = tokio::task::spawn_blocking(|| {
        crate::native_capture::platform::screenshot()
    })
    .await;

    match result {
        Ok(Ok(png_bytes)) => {
            if let Some(ref file_path) = path {
                if let Err(e) = tokio::fs::write(file_path, &png_bytes).await {
                    return Response::err(format!("write file: {}", e));
                }
                Response::ok(serde_json::json!({
                    "path": file_path,
                    "size": png_bytes.len()
                }))
            } else {
                let base64 = crate::native_capture::base64_encode(&png_bytes);
                Response::ok(serde_json::json!({
                    "base64": base64,
                    "size": png_bytes.len()
                }))
            }
        }
        Ok(Err(e)) => Response::err(e),
        Err(e) => Response::err(format!("capture thread error: {}", e)),
    }
}

fn base64_decode(input: &str) -> Result<Vec<u8>, String> {
    const TABLE: [u8; 256] = {
        let mut t = [255u8; 256];
        let chars = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        let mut i = 0;
        while i < 64 {
            t[chars[i] as usize] = i as u8;
            i += 1;
        }
        t
    };

    let bytes: Vec<u8> = input.bytes().filter(|&b| b != b'=' && b != b'\n' && b != b'\r').collect();
    let mut result = Vec::with_capacity(bytes.len() * 3 / 4);

    for chunk in bytes.chunks(4) {
        if chunk.len() < 2 { break; }
        let a = TABLE[chunk[0] as usize] as u32;
        let b = TABLE[chunk[1] as usize] as u32;
        let c = if chunk.len() > 2 { TABLE[chunk[2] as usize] as u32 } else { 0 };
        let d = if chunk.len() > 3 { TABLE[chunk[3] as usize] as u32 } else { 0 };

        if a == 255 || b == 255 { return Err("invalid base64".to_string()); }

        let n = (a << 18) | (b << 12) | (c << 6) | d;
        result.push((n >> 16) as u8);
        if chunk.len() > 2 && c != 255 { result.push((n >> 8) as u8); }
        if chunk.len() > 3 && d != 255 { result.push(n as u8); }
    }

    Ok(result)
}