use tauri::{
plugin::{Builder, TauriPlugin},
Manager, Runtime,
};
#[cfg(desktop)]
mod desktop;
#[cfg(mobile)]
mod mobile;
mod error;
mod platform;
mod server;
mod webdriver;
pub use error::{Error, Result};
pub const DEFAULT_PORT: u16 = 4445;
pub const PORT_ENV_VAR: &str = "TAURI_WEBDRIVER_PORT";
#[must_use]
pub fn init<R: Runtime>() -> TauriPlugin<R> {
let port = std::env::var(PORT_ENV_VAR)
.ok()
.and_then(|s| s.parse::<u16>().ok())
.unwrap_or(DEFAULT_PORT);
init_with_port(port)
}
#[must_use]
pub fn init_with_port<R: Runtime>(port: u16) -> TauriPlugin<R> {
Builder::new("wdio-webdriver")
.setup(move |app, api| {
#[cfg(mobile)]
let webdriver = mobile::init(app, api)?;
#[cfg(desktop)]
let webdriver = desktop::init(app, api);
app.manage(webdriver);
#[cfg(target_os = "windows")]
app.manage(platform::AsyncScriptState::default());
#[cfg(target_os = "windows")]
app.manage(platform::ScriptExecutionLocks::default());
app.manage(platform::AlertStateManager::default());
let app_handle = app.app_handle().clone();
server::start(app_handle, port);
tracing::info!("WDIO WebDriver plugin initialized on port {port}");
Ok(())
})
.on_webview_ready(|webview| {
platform::register_webview_handlers(&webview);
})
.build()
}