Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,32 @@ def wait_for_load(timeout=15.0):
time.sleep(0.3)
return False

def _exception_text(r):
d = r.get("exceptionDetails") or {}
e = d.get("exception") or {}
return "\n".join(str(x) for x in (d.get("text"), e.get("description"), e.get("value")) if x)

def _is_illegal_return_syntax_error(r):
d = r.get("exceptionDetails") or {}
e = d.get("exception") or {}
text = str(d.get("text") or "")
description = str(e.get("description") or "")
class_name = e.get("className")
return (
"Illegal return statement" in (text + "\n" + description)
and (class_name == "SyntaxError" or "SyntaxError:" in text or "SyntaxError:" in description)
)

def js(expression, target_id=None):
"""Run JS in the attached tab (default) or inside an iframe target (via iframe_target()).

Expressions with top-level `return` are automatically wrapped in an IIFE, so both
`document.title` and `const x = 1; return x` are valid inputs.
"""
sid = cdp("Target.attachToTarget", targetId=target_id, flatten=True)["sessionId"] if target_id else None
if "return " in expression and not expression.strip().startswith("("):
expression = f"(function(){{{expression}}})()"
r = cdp("Runtime.evaluate", session_id=sid, expression=expression, returnByValue=True, awaitPromise=True)
if _is_illegal_return_syntax_error(r):
r = cdp("Runtime.evaluate", session_id=sid, expression=f"(function(){{{expression}}})()", returnByValue=True, awaitPromise=True)
return r.get("result", {}).get("value")


Expand Down
31 changes: 25 additions & 6 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
Read SKILL.md for the default workflow and examples.

Typical usage:
uv run bh <<'PY'
browser-harness <<'PY'
ensure_real_tab()
print(page_info())
PY

browser-harness -c "print(page_info())"

Helpers are pre-imported. The daemon auto-starts and connects to the running browser.

Commands:
Expand All @@ -37,6 +39,15 @@
"""


USAGE = 'Usage: browser-harness [-c "print(page_info())"]'


def _exec_code(code):
print_update_banner()
ensure_daemon()
exec(code, globals())


def main():
args = sys.argv[1:]
if args and args[0] in {"-h", "--help"}:
Expand All @@ -59,11 +70,19 @@ def main():
if args and args[0] == "--debug-clicks":
os.environ["BH_DEBUG_CLICKS"] = "1"
args = args[1:]
if not args or args[0] != "-c":
sys.exit("Usage: browser-harness -c \"print(page_info())\"")
print_update_banner()
ensure_daemon()
exec(args[1], globals())
if args and args[0] == "-c":
if len(args) < 2:
sys.exit(USAGE)
_exec_code(args[1])
return
if args:
sys.exit(USAGE)
if sys.stdin.isatty():
sys.exit(USAGE)
code = sys.stdin.read()
if not code.strip():
sys.exit(USAGE)
_exec_code(code)


if __name__ == "__main__":
Expand Down
59 changes: 54 additions & 5 deletions test_js.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,83 @@
import helpers


def _capture_cdp():
def _capture_cdp(runtime_results=None):
captured = []
runtime_results = list(runtime_results or [{"result": {"value": None}}])

def fake_cdp(method, **kwargs):
captured.append((method, kwargs))
if method == "Runtime.evaluate" and runtime_results:
return runtime_results.pop(0)
return {"result": {"value": None}}

return fake_cdp, captured


def _evaluated_expression(captured):
return next(kw["expression"] for m, kw in captured if m == "Runtime.evaluate")


def _evaluated_expressions(captured):
return [kw["expression"] for m, kw in captured if m == "Runtime.evaluate"]


def test_simple_expression_passes_through():
fake_cdp, captured = _capture_cdp()
with patch("helpers.cdp", side_effect=fake_cdp):
helpers.js("document.title")
assert _evaluated_expression(captured) == "document.title"


def test_return_statement_gets_wrapped():
fake_cdp, captured = _capture_cdp()
def test_top_level_return_retries_wrapped():
fake_cdp, captured = _capture_cdp([
{
"exceptionDetails": {
"text": "Uncaught SyntaxError: Illegal return statement",
"exception": {
"className": "SyntaxError",
"description": "SyntaxError: Illegal return statement",
},
}
},
{"result": {"value": 1}},
])
with patch("helpers.cdp", side_effect=fake_cdp):
helpers.js("const x = 1; return x")
assert _evaluated_expression(captured) == "(function(){const x = 1; return x})()"
assert helpers.js("const x = 1; return x") == 1
assert _evaluated_expressions(captured) == [
"const x = 1; return x",
"(function(){const x = 1; return x})()",
]


def test_iife_with_internal_return_is_not_double_wrapped():
fake_cdp, captured = _capture_cdp()
with patch("helpers.cdp", side_effect=fake_cdp):
helpers.js("(function(){ return document.title; })()")
assert _evaluated_expression(captured) == "(function(){ return document.title; })()"


def test_runtime_error_message_does_not_retry_wrapped():
fake_cdp, captured = _capture_cdp([
{
"exceptionDetails": {
"text": "Uncaught Error: Illegal return statement",
"exception": {
"className": "Error",
"description": "Error: Illegal return statement",
},
}
}
])
expression = "throw new Error('Illegal return statement')"
with patch("helpers.cdp", side_effect=fake_cdp):
assert helpers.js(expression) is None
assert _evaluated_expressions(captured) == [expression]


def test_iife_return_is_not_wrapped():
fake_cdp, captured = _capture_cdp([{"result": {"value": 1}}])
expression = "(() => { return 1 })()"
with patch("helpers.cdp", side_effect=fake_cdp):
assert helpers.js(expression) == 1
assert _evaluated_expressions(captured) == [expression]
12 changes: 12 additions & 0 deletions test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,15 @@ def test_c_flag_does_not_read_stdin():
run.main()

assert not stdin_read, "stdin should not be read when -c is passed"


def test_no_args_executes_stdin():
stdout = StringIO()
with patch.object(sys, "argv", ["browser-harness"]), \
patch("run.ensure_daemon"), \
patch("run.print_update_banner"), \
patch("sys.stdin", StringIO("print('hello from stdin')")), \
patch("sys.stdout", stdout):
run.main()

assert stdout.getvalue().strip() == "hello from stdin"