browser-use--browser-harness
64dafa2805
Replace naive `"return " in expression` check with a character-level parser that ignores strings, line comments, and block comments. Add `_decode_unserializable_js_value` to handle NaN, ±Infinity, -0, and BigInt. Extract `_runtime_value` and `_runtime_evaluate` to unify error handling across `js()` and `page_info()`. Wrap Runtime.evaluate TimeoutErrors in RuntimeError with expression context. Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
53 行
1.5 KiB
Python
53 行
1.5 KiB
Python
import os
|
|
import tempfile
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
from PIL import Image
|
|
|
|
from browser_harness import helpers
|
|
|
|
|
|
def _run(fake_png, width, height, **kwargs):
|
|
fake = lambda method, **_: {"data": fake_png(width, height)}
|
|
with patch("browser_harness.helpers.cdp", side_effect=fake), tempfile.TemporaryDirectory() as d:
|
|
path = os.path.join(d, "shot.png")
|
|
helpers.capture_screenshot(path, **kwargs)
|
|
return Image.open(path).size
|
|
|
|
|
|
def test_max_dim_downsizes_oversized_image(fake_png):
|
|
assert max(_run(fake_png, 4592, 2286, max_dim=1800)) == 1800
|
|
|
|
|
|
def test_max_dim_skips_when_image_already_small(fake_png):
|
|
assert _run(fake_png, 800, 400, max_dim=1800) == (800, 400)
|
|
|
|
|
|
def test_max_dim_default_is_no_resize(fake_png):
|
|
assert _run(fake_png, 4592, 2286) == (4592, 2286)
|
|
|
|
|
|
def test_page_info_raises_clear_error_on_js_exception():
|
|
def fake_send(req):
|
|
return {}
|
|
|
|
def fake_cdp(method, **kwargs):
|
|
return {
|
|
"result": {
|
|
"type": "object",
|
|
"subtype": "error",
|
|
"description": "ReferenceError: location is not defined",
|
|
},
|
|
"exceptionDetails": {
|
|
"text": "Uncaught",
|
|
"lineNumber": 0,
|
|
"columnNumber": 16,
|
|
},
|
|
}
|
|
|
|
with patch("browser_harness.helpers._send", side_effect=fake_send), \
|
|
patch("browser_harness.helpers.cdp", side_effect=fake_cdp):
|
|
with pytest.raises(RuntimeError, match="ReferenceError"):
|
|
helpers.page_info()
|