Skip to content

Commit

Permalink
Merge pull request #10 from CloudCannon/feat/js-timeout-control
Browse files Browse the repository at this point in the history
Add `browser_timeout` setting for browser actions
  • Loading branch information
bglw authored Dec 12, 2024
2 parents 4af789a + dcbb496 commit 2e2dcba
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions toolproof/src/definitions/browser/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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) {
Expand All @@ -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);
Expand Down
16 changes: 13 additions & 3 deletions toolproof/src/definitions/browser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -45,6 +49,7 @@ pub enum BrowserTester {
Pagebrowse(Arc<Pagebrowser>),
Chrome {
browser: Arc<Browser>,
browser_timeout: u64,
event_thread: Arc<JoinHandle<Result<(), std::io::Error>>>,
},
}
Expand Down Expand Up @@ -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();
Expand All @@ -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");
Expand All @@ -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),
Expand All @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions toolproof/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ fn get_cli_matches() -> ArgMatches {
.required(false)
.value_parser(value_parser!(u64)),
)
.arg(
arg!(
--"browser-timeout" <NUM> "How long in seconds until actions in a browser time out"
)
.required(false)
.value_parser(value_parser!(u64)),
)
.arg(
arg!(
-n --name <NAME> "Exact name of a test to run")
Expand Down Expand Up @@ -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 = "%")]
Expand Down Expand Up @@ -266,6 +278,10 @@ impl ToolproofParams {
self.timeout = *timeout;
}

if let Some(browser_timeout) = cli_matches.get_one::<u64>("browser-timeout") {
self.browser_timeout = *browser_timeout;
}

if let Some(placeholder_delimiter) = cli_matches.get_one::<String>("placeholder-delimiter")
{
self.placeholder_delimiter = placeholder_delimiter.clone();
Expand Down

0 comments on commit 2e2dcba

Please sign in to comment.