Skip to main content

tauri_plugin_wdio/
lib.rs

1use std::sync::Mutex;
2use tauri::{
3    plugin::{self, TauriPlugin},
4    Manager, Runtime,
5};
6
7pub use models::*;
8
9mod desktop;
10mod commands;
11mod error;
12mod models;
13
14pub use error::{Error, Result};
15
16use desktop::Wdio;
17
18struct WdioUnifiedLogger;
19
20static LOGGER_INIT: Mutex<bool> = Mutex::new(false);
21
22impl log::Log for WdioUnifiedLogger {
23    fn enabled(&self, _metadata: &log::Metadata) -> bool {
24        true
25    }
26
27    fn log(&self, record: &log::Record) {
28        eprintln!("[Tauri:Backend] {}: {}", record.level(), record.args());
29    }
30
31    fn flush(&self) {}
32}
33
34/// Creates the Wdio plugin with default options.
35pub fn init<R: Runtime>() -> TauriPlugin<R> {
36    plugin::Builder::new("wdio")
37        .invoke_handler(tauri::generate_handler![
38            commands::execute,
39            commands::log_frontend,
40            commands::debug_plugin,
41            commands::get_active_window_label,
42            commands::list_windows,
43            commands::get_window_states
44        ])
45        .setup(|app_handle, _api| {
46            // Only set up our global logger if no logger is already configured
47            // This prevents conflicts with tauri_plugin_log or other loggers
48            let mut initialized = LOGGER_INIT.lock().unwrap();
49            if !*initialized {
50                let logger = Box::new(WdioUnifiedLogger);
51                if let Err(e) = log::set_boxed_logger(logger) {
52                    eprintln!("[WDIO] Failed to set global logger (may be already set by another plugin): {}", e);
53                } else {
54                    *initialized = true;
55                }
56            }
57            drop(initialized);
58
59            #[cfg(desktop)]
60            let wdio = desktop::init(app_handle, _api)?;
61
62            app_handle.manage(wdio);
63
64            Ok(())
65        })
66        .build()
67}
68
69/// Extension trait for accessing wdio APIs
70pub trait WdioExt<R: Runtime> {
71    fn wdio(&self) -> &Wdio<R>;
72}
73
74impl<R: Runtime, T: Manager<R>> WdioExt<R> for T {
75    fn wdio(&self) -> &Wdio<R> {
76        self.state::<Wdio<R>>().inner()
77    }
78}