browser-use--browser-harness
5748035bb1
Previously js() silently returned None when Chrome reported a syntax or runtime error. Now it raises RuntimeError with the error description, line/column location, and a snippet of the failing expression. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
94 行
3.0 KiB
Python
94 行
3.0 KiB
Python
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
from browser_harness import helpers
|
|
|
|
|
|
def _capture_cdp():
|
|
captured = []
|
|
def fake_cdp(method, **kwargs):
|
|
captured.append((method, kwargs))
|
|
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 test_simple_expression_passes_through():
|
|
fake_cdp, captured = _capture_cdp()
|
|
with patch("browser_harness.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()
|
|
with patch("browser_harness.helpers.cdp", side_effect=fake_cdp):
|
|
helpers.js("const x = 1; return x")
|
|
assert _evaluated_expression(captured) == "(function(){const x = 1; return x})()"
|
|
|
|
|
|
def test_iife_with_internal_return_is_not_double_wrapped():
|
|
fake_cdp, captured = _capture_cdp()
|
|
with patch("browser_harness.helpers.cdp", side_effect=fake_cdp):
|
|
helpers.js("(function(){ return document.title; })()")
|
|
assert _evaluated_expression(captured) == "(function(){ return document.title; })()"
|
|
|
|
|
|
def test_js_raises_on_syntax_error_exception_details():
|
|
def fake_cdp(method, **kwargs):
|
|
return {
|
|
"result": {
|
|
"type": "object",
|
|
"subtype": "error",
|
|
"description": "SyntaxError: Invalid or unexpected token",
|
|
},
|
|
"exceptionDetails": {
|
|
"text": "Uncaught",
|
|
"lineNumber": 1,
|
|
"columnNumber": 12,
|
|
},
|
|
}
|
|
|
|
with patch("browser_harness.helpers.cdp", side_effect=fake_cdp):
|
|
with pytest.raises(RuntimeError, match="SyntaxError"):
|
|
helpers.js('return "a\n\nb";')
|
|
|
|
|
|
def test_js_raises_on_runtime_error_exception_details():
|
|
def fake_cdp(method, **kwargs):
|
|
return {
|
|
"result": {
|
|
"type": "object",
|
|
"subtype": "error",
|
|
"description": "ReferenceError: missing is not defined",
|
|
},
|
|
"exceptionDetails": {
|
|
"text": "Uncaught",
|
|
"lineNumber": 0,
|
|
"columnNumber": 17,
|
|
},
|
|
}
|
|
|
|
with patch("browser_harness.helpers.cdp", side_effect=fake_cdp):
|
|
with pytest.raises(RuntimeError, match="ReferenceError"):
|
|
helpers.js("return missing.value")
|
|
|
|
|
|
def test_js_raises_on_error_result_without_exception_details():
|
|
def fake_cdp(method, **kwargs):
|
|
return {
|
|
"result": {
|
|
"type": "object",
|
|
"subtype": "error",
|
|
"description": "Error: evaluation failed",
|
|
}
|
|
}
|
|
|
|
with patch("browser_harness.helpers.cdp", side_effect=fake_cdp):
|
|
with pytest.raises(RuntimeError, match="evaluation failed"):
|
|
helpers.js("throw new Error('evaluation failed')")
|