Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(fetch): discard interaction results and follow navigation before dump

lanyue-llkb52701f2fa0b
diff --git a/README.md b/README.md
index 8dfd19478..7d7e97a00 100644
--- a/README.md
+++ b/README.md
@@ -141,8 +141,9 @@ moli fetch --layout --dump pdf https://example.com > page.pdf
To export a page after an explicit interaction, combine `--eval` (or
`--eval-file`) with `--dump`. The expression runs after page readiness;
-promises are awaited, its return value is discarded, and the resulting page
-state is exported. An exception fails the command without emitting a dump.
+promises are awaited, and any pending navigation is followed before export.
+Its return value is discarded without serialization. An exception fails the
+command without emitting a dump.
```bash
moli fetch --eval 'document.querySelector("button.show-details").click()' \
diff --git a/moli/src/eval_output.rs b/moli/src/eval_output.rs
index 18385c176..6b4c11491 100644
--- a/moli/src/eval_output.rs
+++ b/moli/src/eval_output.rs
@@ -12,7 +12,7 @@ pub async fn evaluate(page: &mut Page, expression: &str) -> Result<Vec<u8>> {
pub async fn execute(page: &mut Page, expression: &str) -> Result<()> {
let result = page
- .evaluate_runtime_expression_by_value_async(expression)
+ .evaluate_runtime_expression_with_await_async(expression, true)
.await
.context("failed to evaluate JavaScript expression")?;
reject_exception(&result)
diff --git a/moli/tests/fetch_cli/eval.rs b/moli/tests/fetch_cli/eval.rs
index 53870ae1e..e0308e58c 100644
--- a/moli/tests/fetch_cli/eval.rs
+++ b/moli/tests/fetch_cli/eval.rs
@@ -111,13 +111,46 @@ fn eval_failure_does_not_emit_a_page_dump() -> Result<()> {
#[test]
fn eval_can_dump_html_without_a_serializable_return_value() -> Result<()> {
- let output = run_eval(
- "data:text/html,<p>Initial</p>",
- "document.querySelector('p').textContent='Edited'; document.body",
- &["--dump", "html"],
- )?;
- assert!(output.status.success(), "{}", clean_output(&output.stderr));
- assert!(clean_output(&output.stdout).contains("Edited"));
+ for result in [
+ "document.body",
+ "(()=>{const cycle={};cycle.self=cycle;return cycle})()",
+ "({get value(){document.body.textContent='Unexpected serialization side effect';return 1}})",
+ "({get value(){throw new Error('Unexpected serialization error')}})",
+ "Promise.resolve({get value(){throw new Error('Unexpected serialization error')}})",
+ ] {
+ let script = format!("document.body.textContent='Edited'; {result}");
+ let output = run_eval(
+ "data:text/html,<p>Initial</p>",
+ &script,
+ &["--dump", "html"],
+ )?;
+ assert!(output.status.success(), "{}", clean_output(&output.stderr));
+ let text = clean_output(&output.stdout);
+ assert!(text.contains("Edited"), "{text}");
+ assert!(!text.contains("Unexpected serialization"), "{text}");
+ }
+ Ok(())
+}
+
+#[test]
+fn eval_dumps_destination_after_interaction_navigation() -> Result<()> {
+ let runtime = tokio::runtime::Runtime::new()?;
+ let server = runtime.block_on(FixtureServer::spawn())?;
+ let url = server.url("/static");
+ for script in [
+ "const link=document.createElement('a');link.href='/location-nav/target?from=eval';document.body.append(link);link.click()",
+ "location.href='/location-nav/target?from=eval';undefined",
+ "(async()=>{await Promise.resolve();location.href='/location-nav/target?from=eval'})()",
+ ] {
+ for dump in ["html", "markdown"] {
+ let output = run_eval(&url, script, &["--dump", dump])?;
+ assert!(output.status.success(), "{}", clean_output(&output.stderr));
+ let text = clean_output(&output.stdout);
+ assert!(text.contains("location-target=eval"), "{text}");
+ assert!(!text.contains("fixture static"), "{text}");
+ }
+ }
+ runtime.block_on(server.shutdown());
Ok(())
}