Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
feat(cli): make layout scrollbars opt-in
ldm0390679933b97
diff --git a/moli-cdp-smoke/moli_cdp_smoke/runner.py b/moli-cdp-smoke/moli_cdp_smoke/runner.pyindex 3b208daf4..553d4fa10 100644--- a/moli-cdp-smoke/moli_cdp_smoke/runner.py+++ b/moli-cdp-smoke/moli_cdp_smoke/runner.py@@ -711,7 +711,12 @@ async def async_main(argv: list[str] | None = None) -> int:port = int(port_env) if port_env else 0if port < 0 or port > 65535 or (port == 0 and port_env is not None):raise RuntimeError(f"invalid MOLI_CDP_PORT: {port_env}")- serve = await start_moli_serve(port)+ serve = await start_moli_serve(+ port,+ extra_args=("--scrollbars",)+ if selected_group.name == "classic-scrollbar"+ else (),+ )endpoint = await wait_for_moli_endpoint(serve)if endpoint is None:raise RuntimeError("CDP endpoint was not initialized")diff --git a/moli/src/cli.rs b/moli/src/cli.rsindex 12fc92fc9..88161fd51 100644--- a/moli/src/cli.rs+++ b/moli/src/cli.rs@@ -581,6 +581,7 @@ pub struct CommonArgs {pub disable_subframes: bool,/// Enable the real on-demand layout renderer and screenshot surfaces.+ /// Native scrollbars are hidden unless --scrollbars or MOLI_SCROLLBARS is enabled./// With `fetch --eval` or `--eval-file`, refresh layout once before running/// the script so geometry APIs can read it. Script mutations do not refresh it.///@@ -594,9 +595,14 @@ pub struct CommonArgs {)]pub layout: bool,- /// Hide native scrollbars without disabling scrolling. Requires --layout.- #[arg(long, requires = "layout")]- pub hide_scrollbars: bool,+ /// Show native scrollbars. Requires --layout.+ #[arg(+ long,+ env = "MOLI_SCROLLBARS",+ value_parser = clap::builder::BoolishValueParser::new(),+ requires_if("true", "layout")+ )]+ pub scrollbars: bool,#[arg(short, long = "cookie-file")]pub cookie_file: Vec<String>,diff --git a/moli/src/config.rs b/moli/src/config.rsindex 9b1ea3e50..ae0067665 100644--- a/moli/src/config.rs+++ b/moli/src/config.rs@@ -90,8 +90,8 @@ impl AppConfig {}fn apply_common_args(config: &mut AppConfig, common: &CommonArgs) -> Result<()> {- if common.hide_scrollbars && !common.layout {- bail!("--hide-scrollbars requires --layout or MOLI_LAYOUT=true");+ if common.scrollbars && !common.layout {+ bail!("--scrollbars requires --layout or MOLI_LAYOUT=true");}if let Some(log_level) = common.log_level {@@ -192,7 +192,9 @@ fn apply_common_args(config: &mut AppConfig, common: &CommonArgs) -> Result<()>} else {LayoutPolicy::Mock});- config.browser.set_scrollbars_hidden(common.hide_scrollbars);+ config+ .browser+ .set_scrollbars_hidden(common.layout && !common.scrollbars);config.fetch.cookie_files = common.cookie_file.clone();config.browser.fetch_mut().set_network_blocking(common.block_private_networks,diff --git a/moli/tests/cli.rs b/moli/tests/cli.rsindex e6c32f6e7..419d2bf5a 100644--- a/moli/tests/cli.rs+++ b/moli/tests/cli.rs@@ -140,7 +140,7 @@ fn parses_explicit_fetch_command_with_compatibility_flags() {resource: false,disable_subframes: false,layout: false,- hide_scrollbars: false,+ scrollbars: false,cookie_file: Vec::new(),document_start_script: Vec::new(),document_start_script_file: Vec::new(),@@ -1625,6 +1625,7 @@ fn app_config_defaults_layout_to_mock_for_fetch_and_serve() {config.browser.layout_policy(),moli_core::LayoutPolicy::Mock);+ assert!(!config.browser.scrollbars_hidden());}}@@ -1644,19 +1645,19 @@ fn layout_selects_on_demand_policy_for_fetch_and_serve() {}#[test]-fn hide_scrollbars_is_opt_in_for_fetch_and_serve() {+fn scrollbars_are_opt_in_for_layout_fetch_and_serve() {for command in ["fetch", "serve"] {- for hidden in [false, true] {+ for visible in [false, true] {let mut args = vec!["moli", command, "--layout"];- if hidden {- args.push("--hide-scrollbars");+ if visible {+ args.push("--scrollbars");}if command == "fetch" {args.push("about:blank");}let cli = Cli::try_parse_from(args).unwrap();let config = AppConfig::from_cli(&cli).unwrap();- assert_eq!(config.browser.scrollbars_hidden(), hidden);+ assert_eq!(config.browser.scrollbars_hidden(), !visible);assert_eq!(config.browser.layout_policy(),moli_core::LayoutPolicy::OnDemand@@ -1666,33 +1667,57 @@ fn hide_scrollbars_is_opt_in_for_fetch_and_serve() {}#[test]-fn hide_scrollbars_requires_enabled_layout_from_cli_or_environment() {+fn scrollbars_require_enabled_layout_from_cli_or_environment() {for command in ["fetch", "serve"] {- for (case, env_value, explicit_layout, expected) in [- ("missing", None, false, false),- ("true", Some("true"), false, true),- ("one", Some("1"), false, true),- ("false", Some("false"), false, false),- ("zero", Some("0"), false, false),- ("cli-overrides", Some("false"), true, true),+ for (case, layout_env, explicit_layout, scrollbars_env, expected) in [+ ("missing", None, false, None, false),+ ("true", Some("true"), false, None, true),+ ("one", Some("1"), false, None, true),+ ("false", Some("false"), false, None, false),+ ("zero", Some("0"), false, None, false),+ ("cli-overrides", Some("false"), true, None, true),+ ("env-true-without-layout", None, false, Some("true"), false),+ ("env-one-without-layout", None, false, Some("1"), false),+ (+ "env-true-layout-disabled",+ Some("false"),+ false,+ Some("true"),+ false,+ ),+ (+ "env-one-layout-disabled",+ Some("0"),+ false,+ Some("1"),+ false,+ ),] {let mut child = Command::new(std::env::current_exe().unwrap());child.args(["--exact",- "parse_hide_scrollbars_in_child_process",+ "parse_scrollbars_in_child_process","--nocapture",]).env_remove("MOLI_LAYOUT")- .env("MOLI_TEST_HIDE_SCROLLBARS_COMMAND", command)+ .env_remove("MOLI_SCROLLBARS")+ .env("MOLI_TEST_SCROLLBARS_COMMAND", command).env(- "MOLI_TEST_HIDE_SCROLLBARS_CLI_LAYOUT",+ "MOLI_TEST_SCROLLBARS_CLI_FLAG",+ scrollbars_env.is_none().to_string(),+ )+ .env(+ "MOLI_TEST_SCROLLBARS_CLI_LAYOUT",explicit_layout.to_string(),)- .env("MOLI_TEST_HIDE_SCROLLBARS_EXPECTED", expected.to_string());- if let Some(value) = env_value {+ .env("MOLI_TEST_SCROLLBARS_EXPECTED", expected.to_string());+ if let Some(value) = layout_env {child.env("MOLI_LAYOUT", value);}+ if let Some(value) = scrollbars_env {+ child.env("MOLI_SCROLLBARS", value);+ }let output = child.output().unwrap();assert!(output.status.success(),@@ -1705,13 +1730,16 @@ fn hide_scrollbars_requires_enabled_layout_from_cli_or_environment() {}#[test]-fn parse_hide_scrollbars_in_child_process() {- let Ok(command) = std::env::var("MOLI_TEST_HIDE_SCROLLBARS_COMMAND") else {+fn parse_scrollbars_in_child_process() {+ let Ok(command) = std::env::var("MOLI_TEST_SCROLLBARS_COMMAND") else {return;};- let explicit_layout = std::env::var("MOLI_TEST_HIDE_SCROLLBARS_CLI_LAYOUT").unwrap() == "true";- let expected = std::env::var("MOLI_TEST_HIDE_SCROLLBARS_EXPECTED").unwrap() == "true";- let mut args = vec!["moli", command.as_str(), "--hide-scrollbars"];+ let explicit_layout = std::env::var("MOLI_TEST_SCROLLBARS_CLI_LAYOUT").unwrap() == "true";+ let expected = std::env::var("MOLI_TEST_SCROLLBARS_EXPECTED").unwrap() == "true";+ let mut args = vec!["moli", command.as_str()];+ if std::env::var("MOLI_TEST_SCROLLBARS_CLI_FLAG").unwrap() == "true" {+ args.push("--scrollbars");+ }if explicit_layout {args.push("--layout");}@@ -1723,7 +1751,7 @@ fn parse_hide_scrollbars_in_child_process() {.and_then(|cli| AppConfig::from_cli(&cli));if expected {let config = result.unwrap();- assert!(config.browser.scrollbars_hidden());+ assert!(!config.browser.scrollbars_hidden());assert_eq!(config.browser.layout_policy(),moli_core::LayoutPolicy::OnDemand@@ -1780,22 +1808,33 @@ fn screencast_interval_defaults_to_one_second() {#[test]fn env_flags_are_fallbacks_and_cli_flags_take_priority() {- for (case, env_value, expected) in [- ("env-enables", "true", true),- ("env-disables", "false", false),- ("env-one-enables", "1", true),- ("env-zero-disables", "0", false),- ("cli-overrides-env", "0", true),+ for (case, env_value, expected, scrollbars_env, expected_scrollbars) in [+ ("env-enables", "true", true, Some("true"), true),+ ("env-disables", "false", false, Some("false"), false),+ ("env-one-enables", "1", true, Some("1"), true),+ ("env-zero-disables", "0", false, Some("0"), false),+ ("cli-overrides-env", "0", true, Some("0"), true),+ ("scrollbars-hidden", "true", true, Some("false"), false),+ ("scrollbars-hidden-zero", "true", true, Some("0"), false),+ ("scrollbars-default", "true", true, None, false),] {- let output = Command::new(std::env::current_exe().unwrap())+ let mut child = Command::new(std::env::current_exe().unwrap());+ child.args(["--exact", "parse_env_flags_in_child_process", "--nocapture"]).env("MOLI_TEST_ENV_FLAG_CASE", case).env("MOLI_TEST_ENV_FLAG_EXPECTED", expected.to_string())+ .env(+ "MOLI_TEST_ENV_FLAG_SCROLLBARS_EXPECTED",+ expected_scrollbars.to_string(),+ ).env("MOLI_LAYOUT", env_value).env("MOLI_RESOURCE", env_value).env("MOLI_BLOCK_PRIVATE_NETWORKS", env_value)- .output()- .unwrap();+ .env_remove("MOLI_SCROLLBARS");+ if let Some(value) = scrollbars_env {+ child.env("MOLI_SCROLLBARS", value);+ }+ let output = child.output().unwrap();assert!(output.status.success(),@@ -1815,30 +1854,46 @@ fn parse_env_flags_in_child_process() {.unwrap().parse::<bool>().unwrap();+ let expected_scrollbars = std::env::var("MOLI_TEST_ENV_FLAG_SCROLLBARS_EXPECTED")+ .unwrap()+ .parse::<bool>()+ .unwrap();- let args = if case == "cli-overrides-env" {- vec![- "moli",- "serve",- "--layout",- "--resource",- "--block-private-networks",- ]- } else {- vec!["moli", "serve"]- };- let cli = Cli::try_parse_from(normalize_args_for_compat(args)).unwrap();- let Commands::Serve(args) = cli.command else {- panic!("expected serve command");- };- assert_eq!(args.common.layout, expected);- assert_eq!(args.common.resource, expected);- assert_eq!(args.common.block_private_networks, expected);+ for command in ["fetch", "serve"] {+ let mut args = vec!["moli", command];+ if case == "cli-overrides-env" {+ args.extend([+ "--layout",+ "--scrollbars",+ "--resource",+ "--block-private-networks",+ ]);+ }+ if command == "fetch" {+ args.push("about:blank");+ }+ let cli = Cli::try_parse_from(normalize_args_for_compat(args)).unwrap();+ let config = AppConfig::from_cli(&cli).unwrap();+ assert_eq!(+ config.browser.scrollbars_hidden(),+ expected && !expected_scrollbars+ );+ let common = match cli.command {+ Commands::Fetch(args) => args.common,+ Commands::Serve(args) => args.common,+ Commands::Import(_) => panic!("expected fetch or serve command"),+ };+ assert_eq!(common.layout, expected);+ assert_eq!(common.scrollbars, expected_scrollbars);+ assert_eq!(common.resource, expected);+ assert_eq!(common.block_private_networks, expected);+ }}#[test]fn removed_long_form_flags_are_rejected() {for flag in [+ "--hide-scrollbars","--cdp-max-connections","--cdp-max-pending-connections","--no-layout",diff --git a/moli/tests/fetch_cli/eval.rs b/moli/tests/fetch_cli/eval.rsindex e12dfdc09..5051e9775 100644--- a/moli/tests/fetch_cli/eval.rs+++ b/moli/tests/fetch_cli/eval.rs@@ -174,7 +174,7 @@ fn eval_with_layout_publishes_static_geometry() -> Result<()> {}#[test]-fn eval_hide_scrollbars_removes_gutters_and_preserves_scrolling() -> Result<()> {+fn eval_scrollbars_are_hidden_by_default_and_can_be_enabled() -> Result<()> {let url = "data:text/html,<!doctype html><body style='margin:0'><div style='width:100vw;height:2000px'><div id=scroller style='width:200px;height:100px;overflow:scroll;scrollbar-width:auto!important;scrollbar-gutter:stable both-edges'><div style='width:400px;height:300px'></div></div></div>";let script = r#"(() => {const scroller = document.getElementById('scroller');@@ -191,23 +191,23 @@ fn eval_hide_scrollbars_removes_gutters_and_preserves_scrolling() -> Result<()>css: [getComputedStyle(scroller).scrollbarWidth, getComputedStyle(scroller).scrollbarGutter]};})()"#;- for hidden in [false, true] {+ for visible in [false, true] {let mut args = vec!["--layout"];- if hidden {- args.push("--hide-scrollbars");+ if visible {+ args.push("--scrollbars");}let output = run_eval(url, script, &args)?;assert!(output.status.success(), "{}", clean_output(&output.stderr));let value: serde_json::Value = serde_json::from_slice(&output.stdout)?;let inner = value["inner"].as_u64().expect("viewport width");- assert_eq!(value["client"], inner - if hidden { 0 } else { 15 });+ assert_eq!(value["client"], inner - if visible { 15 } else { 0 });assert_eq!(value["scrollWidth"], inner);assert_eq!(value["elementClient"],- serde_json::json!(if hidden {- [170, 100, 15]- } else {+ serde_json::json!(if visible {[170, 85, 15]+ } else {+ [170, 100, 15]}));assert_eq!(value["elementScroll"], serde_json::json!([400, 300]));