diff --git a/CHANGELOG.md b/CHANGELOG.md index dc9d53e..0b229d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ ## Unreleased +* Add `browser-timeout` / `browser_timeout` setting that changes the default timeout for browser actions such as `toolproof.querySelector()` + ## v0.9.0 (December 3, 2024) * Add automatic wait-and-timeout to Toolproof actions that get elements diff --git a/toolproof/src/definitions/browser/init.js b/toolproof/src/definitions/browser/init.js index d8c5bd4..93c463c 100644 --- a/toolproof/src/definitions/browser/init.js +++ b/toolproof/src/definitions/browser/init.js @@ -75,7 +75,7 @@ class ToolproofHarness { } } - async waitFor(q, timeout = 4000) { + async waitFor(q, timeout = DEFAULT_TIMEOUT) { let start = Date.now(); const throttle = 50; // TODO: configure @@ -95,7 +95,7 @@ class ToolproofHarness { return r; } - async querySelector(s, timeout = 4000) { + async querySelector(s, timeout = DEFAULT_TIMEOUT) { try { return await this.waitFor(() => document.querySelector(s), timeout); } catch (e) { @@ -109,7 +109,7 @@ class ToolproofHarness { } } - async querySelectorAll(s, timeout = 4000) { + async querySelectorAll(s, timeout = DEFAULT_TIMEOUT) { try { return await this.waitFor(() => { let els = document.querySelectorAll(s); diff --git a/toolproof/src/definitions/browser/mod.rs b/toolproof/src/definitions/browser/mod.rs index 273f803..ada00fe 100644 --- a/toolproof/src/definitions/browser/mod.rs +++ b/toolproof/src/definitions/browser/mod.rs @@ -35,6 +35,10 @@ fn harnessed(js: String) -> String { HARNESS.replace("// insert_toolproof_inner_js", &js) } +fn init_script(timeout_secs: u64) -> String { + INIT_SCRIPT.replace("DEFAULT_TIMEOUT", &(timeout_secs * 1000).to_string()) +} + /// We want selector steps to timeout before the step itself does, /// since it provides a better error. This makes that more likely. fn auto_selector_timeout(civ: &Civilization) -> u64 { @@ -45,6 +49,7 @@ pub enum BrowserTester { Pagebrowse(Arc), Chrome { browser: Arc, + browser_timeout: u64, event_thread: Arc>>, }, } @@ -91,6 +96,7 @@ impl BrowserTester { BrowserTester::Chrome { browser: Arc::new(browser), + browser_timeout: params.browser_timeout, event_thread: Arc::new(tokio::task::spawn(async move { loop { let _ = handler.next().await.unwrap(); @@ -105,7 +111,7 @@ impl BrowserTester { "{}/../bin/pagebrowse_manager", env!("CARGO_MANIFEST_DIR") )) - .init_script(INIT_SCRIPT.to_string()) + .init_script(init_script(params.browser_timeout)) .build() .await .expect("Can't build the pagebrowser"); @@ -120,7 +126,11 @@ impl BrowserTester { BrowserTester::Pagebrowse(pb) => { BrowserWindow::Pagebrowse(pb.get_window().await.unwrap()) } - BrowserTester::Chrome { browser, .. } => { + BrowserTester::Chrome { + browser, + browser_timeout, + .. + } => { let context = browser .create_browser_context(CreateBrowserContextParams { dispose_on_detach: Some(true), @@ -143,7 +153,7 @@ impl BrowserTester { }) .await .unwrap(); - page.evaluate_on_new_document(INIT_SCRIPT.to_string()) + page.evaluate_on_new_document(init_script(*browser_timeout)) .await .expect("Could not set initialization js"); BrowserWindow::Chrome(page) diff --git a/toolproof/src/options.rs b/toolproof/src/options.rs index d457e1e..3cc8aca 100644 --- a/toolproof/src/options.rs +++ b/toolproof/src/options.rs @@ -119,6 +119,13 @@ fn get_cli_matches() -> ArgMatches { .required(false) .value_parser(value_parser!(u64)), ) + .arg( + arg!( + --"browser-timeout" "How long in seconds until actions in a browser time out" + ) + .required(false) + .value_parser(value_parser!(u64)), + ) .arg( arg!( -n --name "Exact name of a test to run") @@ -188,6 +195,11 @@ pub struct ToolproofParams { #[setting(default = 10)] pub timeout: u64, + /// How long in seconds until actions in a browser time out + #[setting(env = "TOOLPROOF_BROWSER_TIMEOUT")] + #[setting(default = 8)] + pub browser_timeout: u64, + /// What delimiter should be used when replacing placeholders #[setting(env = "TOOLPROOF_PLACEHOLDER_DELIM")] #[setting(default = "%")] @@ -266,6 +278,10 @@ impl ToolproofParams { self.timeout = *timeout; } + if let Some(browser_timeout) = cli_matches.get_one::("browser-timeout") { + self.browser_timeout = *browser_timeout; + } + if let Some(placeholder_delimiter) = cli_matches.get_one::("placeholder-delimiter") { self.placeholder_delimiter = placeholder_delimiter.clone();