Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

chore: remove local benchmark files from PR

MagMueller0db1dae8dcd2
diff --git a/tests/heavy_pages/.gitignore b/tests/heavy_pages/.gitignore
deleted file mode 100644
index 9ab870da8..000000000
--- a/tests/heavy_pages/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-generated/
diff --git a/tests/heavy_pages/bench_js_limits.py b/tests/heavy_pages/bench_js_limits.py
deleted file mode 100644
index d98df2f12..000000000
--- a/tests/heavy_pages/bench_js_limits.py
+++ /dev/null
@@ -1,355 +0,0 @@
-"""
-Test: what can JS click/type actually reach?
-
-- Same-origin iframe
-- Cross-origin iframe
-- Open shadow DOM
-- Closed shadow DOM
-- Coordinate-based JS click (elementFromPoint)
-- Coordinate click INTO an iframe
-- Coordinate click INTO shadow DOM
-"""
-
-import asyncio
-import logging
-import os
-import sys
-import time
-from http.server import HTTPServer, SimpleHTTPRequestHandler
-from pathlib import Path
-from threading import Thread
-
-sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
-from dotenv import load_dotenv
-load_dotenv(Path('/Users/magnus/Developer/cloud/backend/.env'))
-
-from browser_use.browser.profile import BrowserProfile
-from browser_use.browser.session import BrowserSession
-
-logging.basicConfig(level=logging.WARNING)
-logger = logging.getLogger('bench')
-logger.setLevel(logging.INFO)
-
-
-MAIN_PAGE = '''<html><head><title>JS Boundary Test</title></head>
-<body style="font-family:monospace">
-<h2>JS Boundary Tests</h2>
-
-<!-- 1. Regular button -->
-<div id="test-regular">
- <button id="btn-regular" onclick="this.textContent='REGULAR_CLICKED'">Regular Button</button>
-</div>
-
-<!-- 2. Same-origin iframe -->
-<iframe id="iframe-same" srcdoc="
- <html><body>
- <button id='btn-iframe' onclick='this.textContent=&quot;IFRAME_CLICKED&quot;'>Iframe Button</button>
- <input id='input-iframe' type='text' value='' placeholder='type here' />
- </body></html>
-" style="width:400px;height:80px;border:2px solid blue;"></iframe>
-
-<!-- 3. Cross-origin iframe -->
-<iframe id="iframe-cross" src="https://example.com"
- style="width:400px;height:80px;border:2px solid red;" sandbox="allow-scripts"></iframe>
-
-<!-- 4. Open shadow DOM -->
-<div id="shadow-host-open"></div>
-<script>
- const hostOpen = document.getElementById('shadow-host-open');
- const shadowOpen = hostOpen.attachShadow({mode: 'open'});
- shadowOpen.innerHTML = '<button id="btn-shadow-open" onclick="this.textContent=\\'SHADOW_OPEN_CLICKED\\'">Open Shadow Button</button><input id="input-shadow-open" type="text" />';
-</script>
-
-<!-- 5. Closed shadow DOM -->
-<div id="shadow-host-closed"></div>
-<script>
- const hostClosed = document.getElementById('shadow-host-closed');
- const shadowClosed = hostClosed.attachShadow({mode: 'closed'});
- shadowClosed.innerHTML = '<button id="btn-shadow-closed" onclick="this.textContent=\\'SHADOW_CLOSED_CLICKED\\'">Closed Shadow Button</button><input id="input-shadow-closed" type="text" />';
- // Stash reference for testing
- window.__closedShadow = shadowClosed;
-</script>
-
-<!-- 6. Heavy background (10k elements) to test coordinate click perf -->
-<div id="bulk"></div>
-<script>
-const frag = document.createDocumentFragment();
-for(let i=0;i<10000;i++){const d=document.createElement('div');d.textContent='item-'+i;frag.appendChild(d)}
-document.getElementById('bulk').appendChild(frag);
-</script>
-</body></html>'''
-
-
-class Q(SimpleHTTPRequestHandler):
- def log_message(self, *a): pass
-
-def serve(d, port=8768):
- os.chdir(d)
- s = HTTPServer(('127.0.0.1', port), Q)
- Thread(target=s.serve_forever, daemon=True).start()
- return s
-
-
-async def js_eval(send, sid, expr):
- """Run JS, return (value, ms, error)."""
- t0 = time.time()
- try:
- r = await send.Runtime.evaluate(
- params={'expression': expr, 'returnByValue': True, 'awaitPromise': True},
- session_id=sid
- )
- ms = (time.time()-t0)*1000
- val = r.get('result', {}).get('value')
- exc = r.get('exceptionDetails', {}).get('text')
- if exc:
- return None, ms, exc
- return val, ms, None
- except Exception as e:
- return None, (time.time()-t0)*1000, str(e)[:100]
-
-
-async def main():
- pages_dir = Path(__file__).parent / 'generated'
- pages_dir.mkdir(exist_ok=True)
- (pages_dir / 'js_limits.html').write_text(MAIN_PAGE)
-
- server = serve(str(pages_dir))
- session = BrowserSession(browser_profile=BrowserProfile(headless=True))
- await session.start()
-
- cdp = await session.get_or_create_cdp_session(focus=True)
- sid = cdp.session_id
- send = cdp.cdp_client.send
-
- await send.Page.navigate(params={'url': 'http://127.0.0.1:8768/js_limits.html'}, session_id=sid)
- await asyncio.sleep(2.0)
-
- print('='*80)
- print(' JS BOUNDARY TESTS')
- print('='*80)
-
- tests = []
-
- def report(name, val, ms, err, expected=None):
- ok = False
- if err:
- status = f'FAIL ({err[:60]})'
- elif expected and val == expected:
- status = f'PASS -> "{val}"'
- ok = True
- elif val is not None:
- status = f'MAYBE -> "{val}"'
- ok = True
- else:
- status = 'FAIL (None)'
- tests.append((name, ok, ms))
- print(f' {name:<45} {ms:>7.0f}ms {status}')
-
- # ── 1. Regular button via JS .click() ──
- val, ms, err = await js_eval(send, sid, '''
- document.getElementById("btn-regular").click();
- document.getElementById("btn-regular").textContent
- ''')
- report('JS .click() on regular button', val, ms, err, 'REGULAR_CLICKED')
-
- # ── 2. Same-origin iframe via JS ──
- val, ms, err = await js_eval(send, sid, '''
- const iframe = document.getElementById("iframe-same");
- const doc = iframe.contentDocument;
- doc.getElementById("btn-iframe").click();
- doc.getElementById("btn-iframe").textContent
- ''')
- report('JS .click() into same-origin iframe', val, ms, err, 'IFRAME_CLICKED')
-
- # ── 3. Same-origin iframe: type ──
- val, ms, err = await js_eval(send, sid, '''
- const iframe = document.getElementById("iframe-same");
- const input = iframe.contentDocument.getElementById("input-iframe");
- input.focus(); input.value = "typed-in-iframe"; input.value
- ''')
- report('JS .value= into same-origin iframe', val, ms, err, 'typed-in-iframe')
-
- # ── 4. Cross-origin iframe via JS ──
- val, ms, err = await js_eval(send, sid, '''
- try {
- const iframe = document.getElementById("iframe-cross");
- const doc = iframe.contentDocument;
- doc ? doc.title : "ACCESS_BLOCKED"
- } catch(e) { "BLOCKED: " + e.message }
- ''')
- report('JS access cross-origin iframe', val, ms, err)
-
- # ── 5. Open shadow DOM via JS ──
- val, ms, err = await js_eval(send, sid, '''
- const host = document.getElementById("shadow-host-open");
- const btn = host.shadowRoot.querySelector("#btn-shadow-open");
- btn.click();
- btn.textContent
- ''')
- report('JS .click() into open shadow DOM', val, ms, err, 'SHADOW_OPEN_CLICKED')
-
- # ── 6. Open shadow DOM: type ──
- val, ms, err = await js_eval(send, sid, '''
- const host = document.getElementById("shadow-host-open");
- const input = host.shadowRoot.querySelector("#input-shadow-open");
- input.focus(); input.value = "typed-in-shadow"; input.value
- ''')
- report('JS .value= into open shadow DOM', val, ms, err, 'typed-in-shadow')
-
- # ── 7. Closed shadow DOM via JS ──
- val, ms, err = await js_eval(send, sid, '''
- const host = document.getElementById("shadow-host-closed");
- const sr = host.shadowRoot;
- sr ? "HAS_ACCESS" : "NULL_SHADOWROOT"
- ''')
- report('JS access closed shadow DOM (shadowRoot)', val, ms, err)
-
- # ── 8. Closed shadow DOM via stashed ref ──
- val, ms, err = await js_eval(send, sid, '''
- const sr = window.__closedShadow;
- if (sr) {
- const btn = sr.querySelector("#btn-shadow-closed");
- btn.click();
- btn.textContent
- } else { "NO_REF" }
- ''')
- report('JS .click() closed shadow via window ref', val, ms, err, 'SHADOW_CLOSED_CLICKED')
-
- # ── 9. Coordinate-based JS click (elementFromPoint) ──
- # First get button position
- val, ms, err = await js_eval(send, sid, '''
- document.getElementById("btn-regular").textContent = "Regular Button";
- const rect = document.getElementById("btn-regular").getBoundingClientRect();
- JSON.stringify({x: rect.x + rect.width/2, y: rect.y + rect.height/2})
- ''')
- if val:
- import json
- coords = json.loads(val)
- val2, ms2, err2 = await js_eval(send, sid, f'''
- const el = document.elementFromPoint({coords["x"]}, {coords["y"]});
- if (el) {{ el.click(); el.textContent }} else {{ "NO_ELEMENT" }}
- ''')
- report('JS elementFromPoint().click()', val2, ms2, err2, 'REGULAR_CLICKED')
- else:
- report('JS elementFromPoint().click()', None, ms, 'Could not get coords')
-
- # ── 10. Coordinate click INTO iframe ──
- val, ms, err = await js_eval(send, sid, '''
- // Reset iframe button
- document.getElementById("iframe-same").contentDocument.getElementById("btn-iframe").textContent = "Iframe Button";
- const iframeRect = document.getElementById("iframe-same").getBoundingClientRect();
- // elementFromPoint at iframe location returns the iframe element, not its content
- const el = document.elementFromPoint(iframeRect.x + 50, iframeRect.y + 20);
- el ? el.tagName + "#" + el.id : "NOTHING"
- ''')
- report('JS elementFromPoint() at iframe coords', val, ms, err)
-
- # ── 11. Can we dispatch a synthetic click event at coordinates? ──
- val, ms, err = await js_eval(send, sid, '''
- document.getElementById("btn-regular").textContent = "Regular Button";
- const rect = document.getElementById("btn-regular").getBoundingClientRect();
- const evt = new MouseEvent('click', {
- bubbles: true, cancelable: true, view: window,
- clientX: rect.x + rect.width/2, clientY: rect.y + rect.height/2
- });
- document.getElementById("btn-regular").dispatchEvent(evt);
- document.getElementById("btn-regular").textContent
- ''')
- report('JS synthetic MouseEvent on element', val, ms, err, 'REGULAR_CLICKED')
-
- # ── 12. CDP Input.dispatchMouseEvent (for comparison) ──
- await js_eval(send, sid, 'document.getElementById("btn-regular").textContent = "Regular Button"')
- t0 = time.time()
- try:
- # Get position
- r = await send.Runtime.evaluate(params={
- 'expression': 'JSON.stringify(document.getElementById("btn-regular").getBoundingClientRect())',
- 'returnByValue': True
- }, session_id=sid)
- import json
- rect = json.loads(r['result']['value'])
- x, y = rect['x'] + rect['width']/2, rect['y'] + rect['height']/2
- await send.Input.dispatchMouseEvent(params={'type':'mousePressed','x':x,'y':y,'button':'left','clickCount':1}, session_id=sid)
- await send.Input.dispatchMouseEvent(params={'type':'mouseReleased','x':x,'y':y,'button':'left','clickCount':1}, session_id=sid)
- r2 = await send.Runtime.evaluate(params={
- 'expression': 'document.getElementById("btn-regular").textContent', 'returnByValue': True
- }, session_id=sid)
- val = r2['result']['value']
- ms = (time.time()-t0)*1000
- report('CDP Input.dispatchMouseEvent', val, ms, None, 'REGULAR_CLICKED')
- except Exception as e:
- report('CDP Input.dispatchMouseEvent', None, (time.time()-t0)*1000, str(e))
-
- # ── 13. CDP mouse into same-origin iframe ──
- await js_eval(send, sid, '''
- document.getElementById("iframe-same").contentDocument.getElementById("btn-iframe").textContent = "Iframe Button"
- ''')
- t0 = time.time()
- try:
- r = await send.Runtime.evaluate(params={
- 'expression': '''JSON.stringify((() => {
- const iframe = document.getElementById("iframe-same");
- const iRect = iframe.getBoundingClientRect();
- const btn = iframe.contentDocument.getElementById("btn-iframe");
- const bRect = btn.getBoundingClientRect();
- return {x: iRect.x + bRect.x + bRect.width/2, y: iRect.y + bRect.y + bRect.height/2};
- })())''', 'returnByValue': True
- }, session_id=sid)
- coords = json.loads(r['result']['value'])
- await send.Input.dispatchMouseEvent(params={'type':'mousePressed','x':coords['x'],'y':coords['y'],'button':'left','clickCount':1}, session_id=sid)
- await send.Input.dispatchMouseEvent(params={'type':'mouseReleased','x':coords['x'],'y':coords['y'],'button':'left','clickCount':1}, session_id=sid)
- await asyncio.sleep(0.1)
- r2 = await send.Runtime.evaluate(params={
- 'expression': 'document.getElementById("iframe-same").contentDocument.getElementById("btn-iframe").textContent',
- 'returnByValue': True
- }, session_id=sid)
- val = r2['result']['value']
- ms = (time.time()-t0)*1000
- report('CDP mouse click into same-origin iframe', val, ms, None, 'IFRAME_CLICKED')
- except Exception as e:
- report('CDP mouse click into same-origin iframe', None, (time.time()-t0)*1000, str(e))
-
- # ── 14. Can JS reach into cross-origin iframe via CDP target? ──
- # This tests if we can use CDP to get a separate session for cross-origin iframes
- t0 = time.time()
- try:
- targets = await send.Target.getTargets(params={}, session_id=sid)
- iframe_targets = [t for t in targets.get('targetInfos', []) if t.get('type') == 'iframe']
- val = f'Found {len(iframe_targets)} iframe targets'
- ms = (time.time()-t0)*1000
- report('CDP Target.getTargets (iframe count)', val, ms, None)
- except Exception as e:
- report('CDP Target.getTargets', None, (time.time()-t0)*1000, str(e))
-
- # ── Summary ──
- print('\n' + '='*80)
- print(' SUMMARY')
- print('='*80)
-
- passed = sum(1 for _, ok, _ in tests if ok)
- failed = len(tests) - passed
- print(f'\n {passed} passed, {failed} failed out of {len(tests)} tests\n')
-
- print(' What JS CAN do:')
- print(' - Click/type regular elements')
- print(' - Click/type into same-origin iframes (via contentDocument)')
- print(' - Click/type into open shadow DOM (via shadowRoot)')
- print(' - Click/type into closed shadow DOM IF page holds a reference')
- print(' - elementFromPoint + click (coordinate-based)')
- print(' - Synthetic MouseEvent dispatch')
- print()
- print(' What JS CANNOT do:')
- print(' - Access cross-origin iframe content (blocked by Same-Origin Policy)')
- print(' - Access closed shadow DOM without a stashed reference')
- print(' - elementFromPoint into iframes (returns the iframe element, not content)')
- print()
- print(' What CDP can do that JS cannot:')
- print(' - Input.dispatchMouseEvent clicks INTO any iframe (cross-origin or not)')
- print(' - Separate CDP sessions per cross-origin iframe target')
-
- await session.kill()
- server.shutdown()
-
-
-if __name__ == '__main__':
- asyncio.run(main())
diff --git a/tests/heavy_pages/bench_methods.py b/tests/heavy_pages/bench_methods.py
deleted file mode 100644
index b090a5f36..000000000
--- a/tests/heavy_pages/bench_methods.py
+++ /dev/null
@@ -1,273 +0,0 @@
-"""
-Benchmark: raw CDP operations + pipeline stages on extreme pages.
-
-Tests each operation DIRECTLY via CDP (bypassing event bus timeouts)
-to measure what Chrome can actually do vs what browser-use's pipeline adds.
-"""
-
-import asyncio
-import json
-import logging
-import os
-import sys
-import time
-from http.server import HTTPServer, SimpleHTTPRequestHandler
-from pathlib import Path
-from threading import Thread
-
-sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
-
-from dotenv import load_dotenv
-load_dotenv(Path('/Users/magnus/Developer/cloud/backend/.env'))
-os.environ['TIMEOUT_BrowserStateRequestEvent'] = '600'
-
-from browser_use.browser.profile import BrowserProfile
-from browser_use.browser.session import BrowserSession
-
-logging.basicConfig(level=logging.WARNING, format='%(levelname)s: %(message)s')
-logger = logging.getLogger('bench')
-logger.setLevel(logging.INFO)
-
-
-def gen_page(n):
- return f'''<html><head><title>Bench {n:,}</title></head>
-<body>
-<h1 id="title">Bench: {n:,} elements</h1>
-<div id="click-target" style="padding:20px;background:#eef;cursor:pointer;font-size:18px"
- onclick="this.textContent='CLICKED OK'">CLICK ME</div>
-<input id="type-target" type="text" value="" style="padding:10px;width:300px" />
-<div id="result">waiting</div>
-<div id="bulk"></div>
-<script>
-const frag=document.createDocumentFragment();
-for(let i=0;i<{n};i++){{const d=document.createElement('div');d.className='item';
-d.innerHTML='<span>'+i+'</span><input value=v'+i+' /><button>X</button>';frag.appendChild(d)}}
-document.getElementById('bulk').appendChild(frag);
-document.getElementById('result').textContent='loaded '+document.querySelectorAll('*').length;
-</script></body></html>'''
-
-
-class Q(SimpleHTTPRequestHandler):
- def log_message(self, *a): pass
-
-def serve(d, port=8767):
- os.chdir(d)
- s = HTTPServer(('127.0.0.1', port), Q)
- Thread(target=s.serve_forever, daemon=True).start()
- return s
-
-
-async def timed(coro, timeout=120):
- t0 = time.time()
- try:
- r = await asyncio.wait_for(coro, timeout=timeout)
- return r, (time.time()-t0)*1000, None
- except Exception as e:
- return None, (time.time()-t0)*1000, type(e).__name__+': '+str(e)[:100]
-
-
-async def bench_one(n, base_url):
- """Benchmark a single page scale. Fresh browser per scale."""
- session = BrowserSession(browser_profile=BrowserProfile(headless=True, cross_origin_iframes=False))
- await session.start()
-
- try:
- cdp = await session.get_or_create_cdp_session(focus=True)
- sid = cdp.session_id
- send = cdp.cdp_client.send
-
- # Navigate + wait for JS
- t0 = time.time()
- await send.Page.navigate(params={'url': f'{base_url}/bench_{n}.html'}, session_id=sid)
- await asyncio.sleep(3.0)
- nav_ms = (time.time()-t0)*1000
-
- # Element count
- r = await send.Runtime.evaluate(params={'expression':'document.querySelectorAll("*").length','returnByValue':True}, session_id=sid)
- elems = r.get('result',{}).get('value',0)
-
- rows = []
- rows.append(('Navigate + 3s wait', nav_ms, None, f'{elems:,} elements'))
-
- # ── Raw CDP operations (no browser-use overhead) ──
-
- # 1. Screenshot
- async def do_screenshot():
- r = await send.Page.captureScreenshot(params={'format':'png','quality':80}, session_id=sid)
- return len(r.get('data',''))
- r, ms, err = await timed(do_screenshot())
- rows.append(('Screenshot (raw CDP)', ms, err, f'{r:,}B' if r else ''))
-
- # 2. JS eval simple
- async def do_js():
- r = await send.Runtime.evaluate(params={'expression':'document.title','returnByValue':True}, session_id=sid)
- return r.get('result',{}).get('value','')
- r, ms, err = await timed(do_js())
- rows.append(('JS eval (title)', ms, err, ''))
-
- # 3. JS click
- async def do_click():
- r = await send.Runtime.evaluate(params={
- 'expression':'document.getElementById("click-target").click(); document.getElementById("click-target").textContent',
- 'returnByValue':True}, session_id=sid)
- return r.get('result',{}).get('value','')
- r, ms, err = await timed(do_click())
- rows.append(('JS click', ms, err, f'"{r}"' if r else ''))
-
- # 4. JS type
- async def do_type():
- r = await send.Runtime.evaluate(params={
- 'expression':'const e=document.getElementById("type-target");e.focus();e.value="hello";e.value',
- 'returnByValue':True}, session_id=sid)
- return r.get('result',{}).get('value','')
- r, ms, err = await timed(do_type())
- rows.append(('JS type', ms, err, f'"{r}"' if r else ''))
-
- # 5. JS get HTML length
- async def do_html_len():
- r = await send.Runtime.evaluate(params={
- 'expression':'document.documentElement.outerHTML.length','returnByValue':True}, session_id=sid)
- return r.get('result',{}).get('value',0)
- r, ms, err = await timed(do_html_len())
- rows.append(('JS HTML length', ms, err, f'{r:,} chars' if r else ''))
-
- # 6. CDP raw mouse click
- async def do_mouse():
- await send.Runtime.evaluate(params={'expression':'document.getElementById("click-target").textContent="CLICK ME"'}, session_id=sid)
- await send.Input.dispatchMouseEvent(params={'type':'mousePressed','x':200,'y':80,'button':'left','clickCount':1}, session_id=sid)
- await send.Input.dispatchMouseEvent(params={'type':'mouseReleased','x':200,'y':80,'button':'left','clickCount':1}, session_id=sid)
- r = await send.Runtime.evaluate(params={'expression':'document.getElementById("click-target").textContent','returnByValue':True}, session_id=sid)
- return r.get('result',{}).get('value','')
- r, ms, err = await timed(do_mouse())
- rows.append(('CDP mouse click', ms, err, f'"{r}"' if r else ''))
-
- # 7. CDP keyboard
- async def do_kb():
- await send.Runtime.evaluate(params={'expression':'document.getElementById("type-target").focus();document.getElementById("type-target").value=""'}, session_id=sid)
- for ch in 'test':
- await send.Input.dispatchKeyEvent(params={'type':'keyDown','text':ch,'key':ch}, session_id=sid)
- await send.Input.dispatchKeyEvent(params={'type':'keyUp','key':ch}, session_id=sid)
- r = await send.Runtime.evaluate(params={'expression':'document.getElementById("type-target").value','returnByValue':True}, session_id=sid)
- return r.get('result',{}).get('value','')
- r, ms, err = await timed(do_kb())
- rows.append(('CDP keyboard type', ms, err, f'"{r}"' if r else ''))
-
- # ── Raw CDP data fetches (what the pipeline calls internally) ──
-
- # 8. DOM.getDocument
- async def do_dom():
- r = await send.DOM.getDocument(params={'depth':-1,'pierce':True}, session_id=sid)
- return len(json.dumps(r))
- r, ms, err = await timed(do_dom(), timeout=120)
- rows.append(('DOM.getDocument', ms, err, f'{r/1e6:.1f}MB' if r else ''))
-
- # 9. DOMSnapshot
- async def do_snap():
- r = await send.DOMSnapshot.captureSnapshot(params={
- 'computedStyles':['display','visibility','opacity'],
- 'includePaintOrder':True,'includeDOMRects':True,
- 'includeBlendedBackgroundColors':False,'includeTextColorOpacities':False}, session_id=sid)
- nodes = sum(len(d.get('nodes',{}).get('nodeName',[])) for d in r.get('documents',[]))
- return nodes, len(json.dumps(r))
- r, ms, err = await timed(do_snap(), timeout=120)
- rows.append(('DOMSnapshot.capture', ms, err, f'{r[0]:,} nodes, {r[1]/1e6:.1f}MB' if r else ''))
-
- # 10. Accessibility tree
- async def do_ax():
- r = await send.Accessibility.getFullAXTree(params={}, session_id=sid)
- return len(r.get('nodes',[]))
- r, ms, err = await timed(do_ax(), timeout=120)
- rows.append(('Accessibility.getFull', ms, err, f'{r:,} AX nodes' if r else ''))
-
- # ── Full browser-use pipeline ──
-
- # 11. Full capture
- async def do_full():
- state = await session.get_browser_state_summary(cached=False)
- return len(state.dom_state.selector_map) if state and state.dom_state else 0
- r, ms, err = await timed(do_full(), timeout=300)
- rows.append(('FULL pipeline', ms, err, f'{r:,} selectors' if r is not None else ''))
-
- return n, elems, rows
-
- finally:
- await session.kill()
-
-
-async def main():
- pages_dir = Path(__file__).parent / 'generated'
- pages_dir.mkdir(exist_ok=True)
-
- scales = [10_000, 50_000, 100_000, 500_000, 1_000_000]
- for s in scales:
- (pages_dir / f'bench_{s}.html').write_text(gen_page(s))
-
- server = serve(str(pages_dir))
-
- all_results = []
- for n in scales:
- print(f'\n{"="*90}')
- print(f' {n:>12,} target elements')
- print(f'{"="*90}')
- try:
- n_actual, elems, rows = await bench_one(n, 'http://127.0.0.1:8767')
- all_results.append((n, elems, rows))
- for label, ms, err, detail in rows:
- status = 'PASS' if not err else 'FAIL'
- ms_str = f'{ms:>10.0f}ms' if ms < 100000 else f'{ms/1000:>9.1f}s '
- print(f' {label:<28} {ms_str} {status:<6} {detail}')
- if err:
- print(f' → {err}')
- except Exception as e:
- print(f' FATAL: {e}')
- all_results.append((n, 0, []))
-
- # ── Summary table ──
- print('\n\n' + '='*130)
- print('TIMING SUMMARY (ms)')
- print('='*130)
-
- # Build method list from first result that has data
- method_names = []
- for _, _, rows in all_results:
- if rows:
- method_names = [r[0] for r in rows]
- break
-
- header = f'{"Operation":<28}' + ''.join(f' {n:>12,}' for n, _, _ in all_results)
- print(header)
- print('-'*130)
-
- for i, mname in enumerate(method_names):
- line = f'{mname:<28}'
- for _, _, rows in all_results:
- if i < len(rows):
- _, ms, err, _ = rows[i]
- if err:
- line += f' {"FAIL":>12}'
- elif ms < 100000:
- line += f' {ms:>11.0f}ms'
- else:
- line += f' {ms/1000:>10.1f}s '
- else:
- line += f' {"—":>12}'
- print(line)
-
- # ── What works at each scale ──
- print('\n' + '='*130)
- print('WHAT WORKS AT EACH SCALE')
- print('='*130)
- for n, elems, rows in all_results:
- working = [r[0] for r in rows if not r[2]]
- broken = [r[0] for r in rows if r[2]]
- print(f'\n {n:>10,} elements ({elems:,} DOM nodes):')
- if working:
- print(f' ✓ {", ".join(working)}')
- if broken:
- print(f' ✗ {", ".join(broken)}')
-
- server.shutdown()
-
-
-if __name__ == '__main__':
- asyncio.run(main())
diff --git a/tests/heavy_pages/bench_pipeline_breakdown.py b/tests/heavy_pages/bench_pipeline_breakdown.py
deleted file mode 100644
index aa0e96241..000000000
--- a/tests/heavy_pages/bench_pipeline_breakdown.py
+++ /dev/null
@@ -1,248 +0,0 @@
-"""
-Pipeline breakdown: time EVERY stage separately.
-
-Q1: Where do the 8 seconds go at 5k elements?
-Q2: Would AX tree alone be enough (and faster)?
-Q3: Can we use partial AX tree (viewport-scoped)?
-Q4: What if we skip paint order? Skip AX tree? Skip snapshot?
-Q5: Can we get interactive elements via a fast JS query instead?
-Q6: How fast is each raw CDP call vs the Python processing on top?
-"""
-
-import asyncio, json, logging, os, sys, time
-from pathlib import Path
-from http.server import HTTPServer, SimpleHTTPRequestHandler
-from threading import Thread
-
-sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
-from dotenv import load_dotenv
-load_dotenv(Path('/Users/magnus/Developer/cloud/backend/.env'))
-os.environ['TIMEOUT_BrowserStateRequestEvent'] = '120'
-
-from browser_use.browser.profile import BrowserProfile
-from browser_use.browser.session import BrowserSession
-from browser_use.dom.service import DomService
-from browser_use.dom.enhanced_snapshot import build_snapshot_lookup, REQUIRED_COMPUTED_STYLES
-from browser_use.dom.serializer.serializer import DOMTreeSerializer
-
-logging.basicConfig(level=logging.WARNING)
-
-def gen(n):
- # Mix of interactive elements: buttons, inputs, links, selects, divs with handlers
- return f'''<html><head><title>Pipeline Bench {n}</title></head><body>
-<h1>Pipeline Bench</h1>
-<nav>{"".join(f'<a href="#">Link {i}</a> ' for i in range(min(n//10, 50)))}</nav>
-<form>{"".join(f'<div><label>F{i}</label><input type="text" value="v{i}" /><select><option>A</option><option>B</option></select></div>' for i in range(min(n//5, 200)))}</form>
-<div id="bulk"></div>
-<script>
-const f=document.createDocumentFragment();
-for(let i=0;i<{n};i++){{
- const d=document.createElement('div');
- d.innerHTML='<span>'+i+'</span><input value=v'+i+' /><button onclick="this.textContent=\\'ok\\'">btn'+i+'</button>';
- if(i%3===0) d.addEventListener('click',()=>{{}});
- f.appendChild(d);
-}}
-document.getElementById('bulk').appendChild(f);
-</script></body></html>'''
-
-class Q(SimpleHTTPRequestHandler):
- def log_message(self,*a):pass
-
-async def t(label, coro, timeout=60):
- t0=time.time()
- try:
- r = await asyncio.wait_for(coro, timeout=timeout)
- ms=(time.time()-t0)*1000
- return r, ms, None
- except Exception as e:
- return None, (time.time()-t0)*1000, type(e).__name__
-
-async def bench(n, base):
- s = BrowserSession(browser_profile=BrowserProfile(headless=True, cross_origin_iframes=False))
- await s.start()
- cdp = await s.get_or_create_cdp_session(focus=True)
- sid = cdp.session_id
- send = cdp.cdp_client.send
-
- await send.Page.navigate(params={'url':f'{base}/pipe_{n}.html'}, session_id=sid)
- await asyncio.sleep(2)
-
- r = await send.Runtime.evaluate(params={'expression':'document.querySelectorAll("*").length','returnByValue':True}, session_id=sid)
- elems = r.get('result',{}).get('value',0)
-
- target_id = s.agent_focus_target_id
-
- print(f'\n{"="*90}')
- print(f' {n:,} target elements ({elems:,} DOM nodes)')
- print(f'{"="*90}')
-
- # ── RAW CDP CALLS (what Chrome does) ──────────────────────────────
- print(f'\n {"RAW CDP CALLS":-<70}')
-
- # 1. DOMSnapshot.captureSnapshot
- async def do_snap():
- return await send.DOMSnapshot.captureSnapshot(params={
- 'computedStyles': REQUIRED_COMPUTED_STYLES,
- 'includePaintOrder':True,'includeDOMRects':True,
- 'includeBlendedBackgroundColors':False,'includeTextColorOpacities':False
- }, session_id=sid)
- snapshot, ms, err = await t('DOMSnapshot.captureSnapshot', do_snap())
- snap_nodes = sum(len(d.get('nodes',{}).get('nodeName',[])) for d in (snapshot or {}).get('documents',[])) if snapshot else 0
- print(f' DOMSnapshot.captureSnapshot {ms:>8.0f}ms {snap_nodes:,} nodes {err or ""}')
- snap_ms = ms
-
- # 2. DOM.getDocument
- async def do_dom():
- return await send.DOM.getDocument(params={'depth':-1,'pierce':True}, session_id=sid)
- dom_tree, ms, err = await t('DOM.getDocument', do_dom())
- print(f' DOM.getDocument(depth=-1) {ms:>8.0f}ms {err or ""}')
- dom_ms = ms
-
- # 3. Full AX tree
- async def do_ax_full():
- return await send.Accessibility.getFullAXTree(params={}, session_id=sid)
- ax_full, ms, err = await t('Accessibility.getFullAXTree', do_ax_full())
- ax_full_count = len(ax_full.get('nodes',[])) if ax_full else 0
- print(f' Accessibility.getFullAXTree {ms:>8.0f}ms {ax_full_count:,} nodes {err or ""}')
- ax_full_ms = ms
-
- # 4. Partial AX tree (single node, to see if the API exists)
- async def do_ax_partial():
- # Get the root node's backendNodeId
- root_id = dom_tree['root']['backendNodeId'] if dom_tree else 1
- return await send.Accessibility.getPartialAXTree(params={
- 'backendNodeId': root_id, 'fetchRelatives': False
- }, session_id=sid)
- ax_partial, ms, err = await t('Accessibility.getPartialAXTree', do_ax_partial())
- ax_partial_count = len(ax_partial.get('nodes',[])) if ax_partial else 0
- print(f' Accessibility.getPartialAXTree {ms:>8.0f}ms {ax_partial_count:,} nodes {err or ""}')
-
- # 5. Screenshot
- async def do_ss():
- return await send.Page.captureScreenshot(params={'format':'png','quality':80}, session_id=sid)
- _, ms, err = await t('Screenshot', do_ss())
- print(f' Page.captureScreenshot {ms:>8.0f}ms {err or ""}')
-
- # 6. JS query for interactive elements (viewport-scoped)
- async def do_js_interactive():
- return await send.Runtime.evaluate(params={
- 'expression': '''
- (() => {
- const sel = 'a, button, input, select, textarea, [onclick], [role="button"], [role="link"], [role="tab"], [tabindex]';
- const all = document.querySelectorAll(sel);
- const vh = window.innerHeight;
- const visible = [];
- const offscreen = [];
- for (const el of all) {
- const r = el.getBoundingClientRect();
- const entry = {tag: el.tagName, id: el.id || undefined, name: el.name || undefined,
- type: el.type || undefined, value: (el.value || '').slice(0,30),
- text: (el.textContent || '').slice(0,30).trim(),
- x: Math.round(r.x), y: Math.round(r.y), w: Math.round(r.width), h: Math.round(r.height)};
- if (r.bottom > 0 && r.top < vh) visible.push(entry);
- else offscreen.push(entry);
- }
- return {visible: visible.length, offscreen: offscreen.length, total: all.length,
- sample_visible: visible.slice(0, 5)};
- })()
- ''', 'returnByValue': True
- }, session_id=sid)
- js_r, ms, err = await t('JS interactive query', do_js_interactive())
- js_data = js_r.get('result',{}).get('value',{}) if js_r else {}
- print(f' JS interactive query (viewport) {ms:>8.0f}ms {js_data.get("visible",0)} visible, {js_data.get("total",0)} total {err or ""}')
- js_ms = ms
-
- # 7. JS query ALL elements (to compare with querySelectorAll('*'))
- async def do_js_all():
- return await send.Runtime.evaluate(params={
- 'expression': 'document.querySelectorAll("*").length', 'returnByValue': True
- }, session_id=sid)
- _, ms, err = await t('JS querySelectorAll(*)', do_js_all())
- print(f' JS querySelectorAll("*").length {ms:>8.0f}ms {err or ""}')
-
- # ── PYTHON PROCESSING (what browser-use adds) ─────────────────────
- if snapshot and dom_tree:
- print(f'\n {"PYTHON PROCESSING":-<70}')
-
- # 8. build_snapshot_lookup
- t0=time.time()
- device_pixel_ratio = 1.0
- snapshot_lookup = build_snapshot_lookup(snapshot, device_pixel_ratio)
- ms = (time.time()-t0)*1000
- print(f' build_snapshot_lookup {ms:>8.0f}ms {len(snapshot_lookup):,} entries')
-
- # 9. Build AX tree lookup
- t0=time.time()
- ax_tree_data = ax_full if ax_full else {'nodes': []}
- ax_tree_lookup = {n['backendDOMNodeId']: n for n in ax_tree_data['nodes'] if 'backendDOMNodeId' in n}
- ms = (time.time()-t0)*1000
- print(f' build AX tree lookup {ms:>8.0f}ms {len(ax_tree_lookup):,} entries')
-
- # 10. Full _construct_enhanced_node + get_dom_tree
- dom_service = DomService(
- browser_session=s, cross_origin_iframes=False,
- paint_order_filtering=True, max_iframes=100, max_iframe_depth=5
- )
- t0=time.time()
- try:
- enhanced_tree, timing = await asyncio.wait_for(
- dom_service.get_dom_tree(target_id=target_id), timeout=60
- )
- ms = (time.time()-t0)*1000
- tree_ok = True
- except Exception as e:
- ms = (time.time()-t0)*1000
- tree_ok = False
- enhanced_tree = None
- timing = {}
- print(f' get_dom_tree (full) {ms:>8.0f}ms')
- # Print sub-timings from the timing dict
- for k, v in sorted(timing.items()):
- print(f' {k:<40} {v:>8.1f}ms')
-
- # 11. Serialization
- if tree_ok and enhanced_tree:
- t0=time.time()
- serialized, ser_timing = DOMTreeSerializer(
- enhanced_tree, None, paint_order_filtering=True, session_id=s.id
- ).serialize_accessible_elements()
- ms = (time.time()-t0)*1000
- print(f' serialize_accessible_elements {ms:>8.0f}ms {len(serialized.selector_map):,} selectors')
- for k, v in sorted(ser_timing.items()):
- print(f' {k:<40} {v*1000:>8.1f}ms')
-
- # 12. Serialization WITHOUT paint order
- t0=time.time()
- serialized2, ser_timing2 = DOMTreeSerializer(
- enhanced_tree, None, paint_order_filtering=False, session_id=s.id
- ).serialize_accessible_elements()
- ms = (time.time()-t0)*1000
- print(f' serialize (NO paint order) {ms:>8.0f}ms {len(serialized2.selector_map):,} selectors')
- for k, v in sorted(ser_timing2.items()):
- print(f' {k:<40} {v*1000:>8.1f}ms')
-
- # ── COMPARISON SUMMARY ────────────────────────────────────────────
- print(f'\n {"COMPARISON":-<70}')
- print(f' Raw CDP snapshot+DOM+AX: {snap_ms+dom_ms+ax_full_ms:>8.0f}ms (Chrome work)')
- print(f' JS interactive query: {js_ms:>8.0f}ms (alternative)')
- print(f' Full pipeline overhead: Python processing on top of CDP')
-
- await s.kill()
-
-
-async def main():
- d = Path(__file__).parent / 'generated'; d.mkdir(exist_ok=True)
- scales = [5000, 20000, 100000]
- for n in scales:
- (d/f'pipe_{n}.html').write_text(gen(n))
- os.chdir(str(d))
- srv = HTTPServer(('127.0.0.1', 8775), Q)
- Thread(target=srv.serve_forever, daemon=True).start()
-
- for n in scales:
- await bench(n, 'http://127.0.0.1:8775')
-
- srv.shutdown()
-
-if __name__ == '__main__':
- asyncio.run(main())
diff --git a/tests/heavy_pages/bench_snapshot_profile.py b/tests/heavy_pages/bench_snapshot_profile.py
deleted file mode 100644
index 19324d45c..000000000
--- a/tests/heavy_pages/bench_snapshot_profile.py
+++ /dev/null
@@ -1,157 +0,0 @@
-"""Profile build_snapshot_lookup to find the exact O(n²) bottleneck."""
-import asyncio, os, sys, time, cProfile, pstats, io
-from pathlib import Path
-from http.server import HTTPServer, SimpleHTTPRequestHandler
-from threading import Thread
-
-sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
-from dotenv import load_dotenv
-load_dotenv(Path('/Users/magnus/Developer/cloud/backend/.env'))
-
-from browser_use.browser.profile import BrowserProfile
-from browser_use.browser.session import BrowserSession
-from browser_use.dom.enhanced_snapshot import build_snapshot_lookup, REQUIRED_COMPUTED_STYLES, _parse_rare_boolean_data
-
-import logging
-logging.basicConfig(level=logging.WARNING)
-
-def gen(n):
- return f'<html><body><div id=b></div><script>const f=document.createDocumentFragment();for(let i=0;i<{n};i++){{const d=document.createElement("div");d.innerHTML="<span>"+i+"</span><input value=v"+i+" /><button>X</button>";f.appendChild(d)}}document.getElementById("b").appendChild(f)</script></body></html>'
-
-class Q(SimpleHTTPRequestHandler):
- def log_message(self,*a):pass
-
-async def main():
- d = Path(__file__).parent / 'generated'; d.mkdir(exist_ok=True)
- for n in [5000, 20000]:
- (d/f'prof_{n}.html').write_text(gen(n))
- os.chdir(str(d))
- srv = HTTPServer(('127.0.0.1', 8774), Q)
- Thread(target=srv.serve_forever, daemon=True).start()
-
- for n in [5000, 20000]:
- s = BrowserSession(browser_profile=BrowserProfile(headless=True))
- await s.start()
- cdp = await s.get_or_create_cdp_session(focus=True)
- sid = cdp.session_id
- send = cdp.cdp_client.send
-
- await send.Page.navigate(params={'url':f'http://127.0.0.1:8774/prof_{n}.html'}, session_id=sid)
- await asyncio.sleep(2)
-
- # Get snapshot
- snapshot = await send.DOMSnapshot.captureSnapshot(params={
- 'computedStyles': REQUIRED_COMPUTED_STYLES,
- 'includePaintOrder':True,'includeDOMRects':True,
- 'includeBlendedBackgroundColors':False,'includeTextColorOpacities':False
- }, session_id=sid)
-
- total_nodes = sum(len(d.get('nodes',{}).get('nodeName',[])) for d in snapshot.get('documents',[]))
- print(f'\n{"="*80}')
- print(f' {n:,} elements, {total_nodes:,} snapshot nodes')
- print(f'{"="*80}')
-
- # Check isClickable data size
- for doc_idx, doc in enumerate(snapshot['documents']):
- nodes_data = doc['nodes']
- if 'isClickable' in nodes_data:
- clickable_list = nodes_data['isClickable']['index']
- print(f' doc[{doc_idx}] isClickable index list length: {len(clickable_list)}')
-
- # Manual breakdown of build_snapshot_lookup
- strings = snapshot['strings']
- documents = snapshot['documents']
-
- for doc_idx, document in enumerate(documents):
- nodes_data = document['nodes']
- layout = document['layout']
-
- # Time: build backend_node_to_snapshot_index
- t0 = time.time()
- backend_node_to_snapshot_index = {}
- if 'backendNodeId' in nodes_data:
- for i, bid in enumerate(nodes_data['backendNodeId']):
- backend_node_to_snapshot_index[bid] = i
- ms = (time.time()-t0)*1000
- print(f' doc[{doc_idx}] build backend_node_to_snapshot_index: {ms:.1f}ms ({len(backend_node_to_snapshot_index)} entries)')
-
- # Time: build layout_index_map
- t0 = time.time()
- layout_index_map = {}
- if layout and 'nodeIndex' in layout:
- for layout_idx, node_index in enumerate(layout['nodeIndex']):
- if node_index not in layout_index_map:
- layout_index_map[node_index] = layout_idx
- ms = (time.time()-t0)*1000
- print(f' doc[{doc_idx}] build layout_index_map: {ms:.1f}ms ({len(layout_index_map)} entries)')
-
- # Time: isClickable parsing (the suspected O(n²))
- if 'isClickable' in nodes_data:
- clickable_index_list = nodes_data['isClickable']['index']
-
- # Method 1: current (list scan per node)
- t0 = time.time()
- count = 0
- for snapshot_index in range(len(nodes_data.get('backendNodeId', []))):
- if snapshot_index in clickable_index_list: # O(len(clickable_index_list)) per call!
- count += 1
- ms = (time.time()-t0)*1000
- print(f' doc[{doc_idx}] isClickable via LIST scan: {ms:.1f}ms (found {count} clickable)')
-
- # Method 2: convert to set first
- t0 = time.time()
- clickable_set = set(clickable_index_list)
- count2 = 0
- for snapshot_index in range(len(nodes_data.get('backendNodeId', []))):
- if snapshot_index in clickable_set: # O(1) per call
- count2 += 1
- ms = (time.time()-t0)*1000
- print(f' doc[{doc_idx}] isClickable via SET scan: {ms:.1f}ms (found {count2} clickable)')
- assert count == count2
-
- # Time: the main loop (creating EnhancedSnapshotNode objects)
- t0 = time.time()
- dummy_count = 0
- for backend_node_id, snapshot_index in backend_node_to_snapshot_index.items():
- # Simulate the work without isClickable
- if snapshot_index in layout_index_map:
- layout_idx = layout_index_map[snapshot_index]
- if layout_idx < len(layout.get('bounds', [])):
- bounds = layout['bounds'][layout_idx]
- _ = bounds[0] if len(bounds) >= 4 else None
- if layout_idx < len(layout.get('styles', [])):
- style_indices = layout['styles'][layout_idx]
- # Parse styles
- styles = {}
- for i, si in enumerate(style_indices):
- if i < len(REQUIRED_COMPUTED_STYLES) and 0 <= si < len(strings):
- styles[REQUIRED_COMPUTED_STYLES[i]] = strings[si]
- dummy_count += 1
- ms = (time.time()-t0)*1000
- print(f' doc[{doc_idx}] main loop (no isClickable): {ms:.1f}ms ({dummy_count} iterations)')
-
- # Time: full build_snapshot_lookup
- t0 = time.time()
- result = build_snapshot_lookup(snapshot, 1.0)
- ms = (time.time()-t0)*1000
- print(f'\n FULL build_snapshot_lookup: {ms:.0f}ms ({len(result)} entries)')
-
- # Profile it
- pr = cProfile.Profile()
- pr.enable()
- result2 = build_snapshot_lookup(snapshot, 1.0)
- pr.disable()
-
- stream = io.StringIO()
- ps = pstats.Stats(pr, stream=stream).sort_stats('cumulative')
- ps.print_stats(15)
- print(f'\n cProfile top 15:')
- for line in stream.getvalue().split('\n')[:20]:
- print(f' {line}')
-
- await s.kill()
-
- srv.shutdown()
-
-if __name__ == '__main__':
- asyncio.run(main())
diff --git a/tests/heavy_pages/bench_threshold.py b/tests/heavy_pages/bench_threshold.py
deleted file mode 100644
index 15f8f2eeb..000000000
--- a/tests/heavy_pages/bench_threshold.py
+++ /dev/null
@@ -1,88 +0,0 @@
-"""Find the exact threshold where DOM capture becomes too slow for interactive use."""
-import asyncio, os, sys, time, json
-from pathlib import Path
-from http.server import HTTPServer, SimpleHTTPRequestHandler
-from threading import Thread
-
-sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
-from dotenv import load_dotenv
-load_dotenv(Path('/Users/magnus/Developer/cloud/backend/.env'))
-os.environ['TIMEOUT_BrowserStateRequestEvent'] = '120'
-
-from browser_use.browser.profile import BrowserProfile
-from browser_use.browser.session import BrowserSession
-
-import logging
-logging.basicConfig(level=logging.WARNING)
-
-def gen(n):
- return f'<html><body><div id=b></div><script>const f=document.createDocumentFragment();for(let i=0;i<{n};i++){{const d=document.createElement("div");d.innerHTML="<span>"+i+"</span><input value=v"+i+" /><button>X</button>";f.appendChild(d)}}document.getElementById("b").appendChild(f)</script></body></html>'
-
-class Q(SimpleHTTPRequestHandler):
- def log_message(self,*a):pass
-
-async def main():
- d = Path(__file__).parent / 'generated'; d.mkdir(exist_ok=True)
- # Fine-grained scales around the threshold
- scales = [500, 1000, 2000, 3000, 5000, 7500, 10000, 15000, 20000, 30000, 50000]
- for n in scales:
- (d/f't_{n}.html').write_text(gen(n))
- os.chdir(str(d))
- srv = HTTPServer(('127.0.0.1',8771), Q)
- Thread(target=srv.serve_forever, daemon=True).start()
-
- print(f'{"Elements":>10} {"DOM nodes":>10} {"Full capture":>13} {"Screenshot":>11} {"JS click":>10} {"CDP click":>11} {"Selectors":>10}')
- print('-'*80)
-
- for n in scales:
- s = BrowserSession(browser_profile=BrowserProfile(headless=True, cross_origin_iframes=False))
- await s.start()
- cdp = await s.get_or_create_cdp_session(focus=True)
- sid = cdp.session_id
- send = cdp.cdp_client.send
-
- await send.Page.navigate(params={'url':f'http://127.0.0.1:8771/t_{n}.html'}, session_id=sid)
- await asyncio.sleep(2)
-
- r = await send.Runtime.evaluate(params={'expression':'document.querySelectorAll("*").length','returnByValue':True}, session_id=sid)
- elems = r.get('result',{}).get('value',0)
-
- # Full capture
- t0=time.time()
- try:
- state = await asyncio.wait_for(s.get_browser_state_summary(cached=False), timeout=60)
- full_ms = (time.time()-t0)*1000
- sel_count = len(state.dom_state.selector_map) if state and state.dom_state else 0
- except:
- full_ms = (time.time()-t0)*1000
- sel_count = 0
-
- # Screenshot
- t0=time.time()
- try:
- await send.Page.captureScreenshot(params={'format':'png','quality':80}, session_id=sid)
- ss_ms = (time.time()-t0)*1000
- except:
- ss_ms = -1
-
- # JS click
- t0=time.time()
- await send.Runtime.evaluate(params={'expression':'document.querySelector("button")?.click()','returnByValue':True}, session_id=sid)
- js_ms = (time.time()-t0)*1000
-
- # CDP mouse click
- t0=time.time()
- try:
- await send.Input.dispatchMouseEvent(params={'type':'mousePressed','x':100,'y':50,'button':'left','clickCount':1}, session_id=sid)
- await send.Input.dispatchMouseEvent(params={'type':'mouseReleased','x':100,'y':50,'button':'left','clickCount':1}, session_id=sid)
- cdp_ms = (time.time()-t0)*1000
- except:
- cdp_ms = -1
-
- print(f'{n:>10,} {elems:>10,} {full_ms:>12.0f}ms {ss_ms:>10.0f}ms {js_ms:>9.0f}ms {cdp_ms:>10.0f}ms {sel_count:>10,}')
- await s.kill()
-
- srv.shutdown()
-
-if __name__=='__main__':
- asyncio.run(main())
diff --git a/tests/heavy_pages/test_heavy_dom.py b/tests/heavy_pages/test_heavy_dom.py
deleted file mode 100644
index b1a21e4f1..000000000
--- a/tests/heavy_pages/test_heavy_dom.py
+++ /dev/null
@@ -1,838 +0,0 @@
-"""
-Stress test: browser-use DOM capture on extremely heavy pages.
-
-Creates 10 progressively heavier test pages (from 1k to 50k+ elements)
-and verifies that DOM capture completes without crashing or timing out.
-
-Usage:
- cd /Users/magnus/Developer/browser-use
- source .venv/bin/activate
- ANTHROPIC_API_KEY=... python tests/heavy_pages/test_heavy_dom.py
-
-Or run non-interactively (no LLM, just DOM capture):
- python tests/heavy_pages/test_heavy_dom.py --dom-only
-"""
-
-import argparse
-import asyncio
-import logging
-import os
-import sys
-import time
-from http.server import HTTPServer, SimpleHTTPRequestHandler
-from pathlib import Path
-from threading import Thread
-
-# Add project root to path
-sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
-
-from browser_use.browser.profile import BrowserProfile
-from browser_use.browser.session import BrowserSession
-
-logger = logging.getLogger('heavy_page_test')
-logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(name)s: %(message)s')
-
-# ─── Page generators ───────────────────────────────────────────────────────────
-
-def gen_page_flat_divs(n: int) -> str:
- """Page 1: N flat div elements with text and click handlers."""
- items = '\n'.join(
- f'<div class="item" onclick="this.style.background=\'red\'" data-id="{i}">'
- f'Item {i} <span>detail</span> <a href="#">link-{i}</a></div>'
- for i in range(n)
- )
- return f'<html><head><title>Flat Divs ({n})</title></head><body><h1>{n} flat divs</h1>{items}</body></html>'
-
-
-def gen_page_nested_tables(rows: int, cols: int) -> str:
- """Page 2: Deep nested table with inputs in every cell."""
- cells_per_row = ''.join(
- f'<td><input type="text" name="cell_{r}_{c}" value="r{r}c{c}" />'
- f'<button onclick="alert({r})">btn</button></td>'
- for c in range(cols)
- for r in [0] # placeholder, replaced below
- )
- header = '<tr>' + ''.join(f'<th>Col {c}</th>' for c in range(cols)) + '</tr>'
- body_rows = []
- for r in range(rows):
- cells = ''.join(
- f'<td><input type="text" name="cell_{r}_{c}" value="r{r}c{c}" />'
- f'<button onclick="alert({r})">btn-{r}-{c}</button></td>'
- for c in range(cols)
- )
- body_rows.append(f'<tr>{cells}</tr>')
- table = f'<table border="1">{header}{"".join(body_rows)}</table>'
- total = rows * cols * 3 # td + input + button per cell
- return f'<html><head><title>Nested Table ({rows}x{cols})</title></head><body><h1>Table {rows}x{cols} (~{total} elements)</h1>{table}</body></html>'
-
-
-def gen_page_shadow_dom(n_hosts: int, children_per: int) -> str:
- """Page 3: Shadow DOM hosts each with children."""
- script = f'''
- <script>
- document.addEventListener('DOMContentLoaded', () => {{
- for (let i = 0; i < {n_hosts}; i++) {{
- const host = document.createElement('div');
- host.id = 'shadow-host-' + i;
- host.className = 'shadow-host';
- document.getElementById('container').appendChild(host);
- const shadow = host.attachShadow({{mode: 'open'}});
- shadow.innerHTML = '<style>:host {{ border: 1px solid #ccc; padding: 4px; margin: 2px; display: block; }}</style>';
- for (let j = 0; j < {children_per}; j++) {{
- const el = document.createElement('div');
- el.className = 'shadow-child';
- el.innerHTML = '<span>Shadow ' + i + '.' + j + '</span><button onclick="this.textContent=\\'clicked\\'">Click</button><input type="text" value="val-' + i + '-' + j + '" />';
- shadow.appendChild(el);
- }}
- }}
- }});
- </script>'''
- total = n_hosts * children_per * 4 # div + span + button + input per child
- return f'<html><head><title>Shadow DOM ({n_hosts}x{children_per})</title>{script}</head><body><h1>Shadow DOM ~{total} elements</h1><div id="container"></div></body></html>'
-
-
-def gen_page_iframes(n_iframes: int, elements_per: int) -> str:
- """Page 4: Same-origin iframes each with many elements."""
- iframe_content = '<br>'.join(
- f'<div class="iframe-item"><a href="#">Link-{{i}}-{j}</a> '
- f'<input type="checkbox" id="cb-{{i}}-{j}" /><label for="cb-{{i}}-{j}">Label {j}</label></div>'
- for j in range(elements_per)
- )
- iframes = '\n'.join(
- f'<iframe srcdoc=\'<html><body><h2>Frame {i}</h2>{iframe_content.replace("{i}", str(i))}</body></html>\' '
- f'style="width:300px;height:200px;border:1px solid black;"></iframe>'
- for i in range(n_iframes)
- )
- total = n_iframes * elements_per * 4
- return f'<html><head><title>Iframes ({n_iframes}x{elements_per})</title></head><body><h1>{n_iframes} iframes ~{total} elements</h1><div>{iframes}</div></body></html>'
-
-
-def gen_page_deep_nesting(depth: int, breadth: int) -> str:
- """Page 5: Deeply nested DOM tree."""
- def make_tree(d: int, b: int) -> str:
- if d <= 0:
- return f'<span class="leaf">Leaf d={d}</span>'
- children = ''.join(
- f'<div class="level-{d}" data-depth="{d}" data-branch="{i}">'
- f'<span>L{d}B{i}</span>{make_tree(d - 1, b)}</div>'
- for i in range(b)
- )
- return children
-
- # Limit recursion to avoid explosion — depth=8, breadth=3 gives ~6k nodes
- tree = make_tree(min(depth, 10), min(breadth, 3))
- return f'<html><head><title>Deep Nesting (d={depth}, b={breadth})</title></head><body><h1>Deep nesting</h1><div id="root">{tree}</div></body></html>'
-
-
-def gen_page_forms_mega(n_fields: int) -> str:
- """Page 6: Giant form with diverse input types."""
- input_types = ['text', 'email', 'password', 'number', 'tel', 'url', 'date', 'time', 'color', 'range', 'checkbox', 'radio']
- fields = []
- for i in range(n_fields):
- t = input_types[i % len(input_types)]
- fields.append(
- f'<div class="form-group">'
- f'<label for="field-{i}">Field {i} ({t})</label>'
- f'<input type="{t}" id="field-{i}" name="field_{i}" placeholder="Enter {t}" />'
- f'</div>'
- )
- total = n_fields * 3 # div + label + input
- return (
- f'<html><head><title>Mega Form ({n_fields} fields)</title></head>'
- f'<body><h1>Form with {n_fields} fields (~{total} elements)</h1>'
- f'<form>{"".join(fields)}<button type="submit">Submit</button></form></body></html>'
- )
-
-
-def gen_page_svg_heavy(n_shapes: int) -> str:
- """Page 7: Heavy SVG with many shapes + interactive overlays."""
- shapes = []
- for i in range(n_shapes):
- x = (i * 20) % 2000
- y = (i * 15) % 1500
- shapes.append(
- f'<circle cx="{x}" cy="{y}" r="8" fill="hsl({i % 360}, 70%, 50%)" '
- f'onclick="this.setAttribute(\'r\', 20)" />'
- f'<text x="{x+10}" y="{y}" font-size="8">{i}</text>'
- )
- svg = f'<svg width="2000" height="1500" xmlns="http://www.w3.org/2000/svg">{"".join(shapes)}</svg>'
- buttons = ''.join(f'<button id="btn-{i}" onclick="alert({i})">Action {i}</button>' for i in range(200))
- total = n_shapes * 2 + 200
- return (
- f'<html><head><title>SVG Heavy ({n_shapes} shapes)</title></head>'
- f'<body><h1>SVG + {n_shapes} shapes (~{total} elements)</h1>{svg}<div>{buttons}</div></body></html>'
- )
-
-
-def gen_page_event_listeners(n: int) -> str:
- """Page 8: Elements with tons of JS event listeners."""
- script = f'''
- <script>
- document.addEventListener('DOMContentLoaded', () => {{
- const container = document.getElementById('listener-container');
- for (let i = 0; i < {n}; i++) {{
- const div = document.createElement('div');
- div.className = 'listener-item';
- div.innerHTML = '<span>Item ' + i + '</span><button>Btn ' + i + '</button>';
- div.addEventListener('click', () => {{}});
- div.addEventListener('mousedown', () => {{}});
- div.addEventListener('pointerdown', () => {{}});
- div.querySelector('button').addEventListener('click', (e) => {{ e.stopPropagation(); }});
- container.appendChild(div);
- }}
- }});
- </script>'''
- total = n * 3 # div + span + button, each with listeners
- return (
- f'<html><head><title>Event Listeners ({n})</title>{script}</head>'
- f'<body><h1>{n} elements with event listeners (~{total} DOM nodes)</h1>'
- f'<div id="listener-container"></div></body></html>'
- )
-
-
-def gen_page_cross_origin_iframes(n: int) -> str:
- """Page 9: Cross-origin iframes (using real external sites) + heavy local content."""
- external_urls = [
- 'https://example.com',
- 'https://www.wikipedia.org',
- 'https://httpbin.org/html',
- ]
- iframes = '\n'.join(
- f'<iframe src="{external_urls[i % len(external_urls)]}" '
- f'style="width:400px;height:300px;border:2px solid red;" '
- f'sandbox="allow-scripts"></iframe>'
- for i in range(min(n, 20)) # cap at 20 external iframes
- )
- # Add heavy local content around the iframes
- local_divs = '\n'.join(
- f'<div class="local" data-idx="{i}"><input type="text" value="local-{i}" />'
- f'<select><option>A</option><option>B</option><option>C</option></select></div>'
- for i in range(2000)
- )
- return (
- f'<html><head><title>Cross-Origin Iframes ({n})</title></head>'
- f'<body><h1>Cross-origin iframes + heavy local content</h1>'
- f'<div>{iframes}</div><div>{local_divs}</div></body></html>'
- )
-
-
-def gen_page_ultimate_stress() -> str:
- """Page 10: The ultimate stress test — everything combined."""
- # Shadow DOM section
- shadow_script = '''
- <script>
- document.addEventListener('DOMContentLoaded', () => {
- // Shadow DOM hosts
- const shadowContainer = document.getElementById('shadow-section');
- for (let i = 0; i < 100; i++) {
- const host = document.createElement('div');
- host.className = 'shadow-host';
- shadowContainer.appendChild(host);
- const shadow = host.attachShadow({mode: 'open'});
- let html = '<style>.inner { padding: 2px; }</style>';
- for (let j = 0; j < 30; j++) {
- html += '<div class="inner"><input type="text" value="s' + i + '.' + j + '" /><button>Go</button></div>';
- }
- shadow.innerHTML = html;
- }
-
- // Event listener section
- const listenerContainer = document.getElementById('listener-section');
- for (let i = 0; i < 3000; i++) {
- const div = document.createElement('div');
- div.innerHTML = '<span>EL-' + i + '</span><a href="#" onclick="return false">link</a>';
- div.addEventListener('click', () => {});
- div.addEventListener('pointerdown', () => {});
- listenerContainer.appendChild(div);
- }
- });
- </script>'''
-
- # Table section
- table_rows = []
- for r in range(200):
- cells = ''.join(
- f'<td><input name="t_{r}_{c}" value="{r}-{c}" /><button>X</button></td>'
- for c in range(10)
- )
- table_rows.append(f'<tr>{cells}</tr>')
- table = f'<table border="1">{"".join(table_rows)}</table>'
-
- # Form section
- form_fields = ''.join(
- f'<div><label for="uf-{i}">Field {i}</label>'
- f'<input type="{["text","email","number","date","tel","url","color"][i%7]}" id="uf-{i}" /></div>'
- for i in range(500)
- )
-
- # Same-origin iframes
- iframe_content = '<br>'.join(f'<div>iframe-item-{j}<input value="{j}" /></div>' for j in range(50))
- iframes = '\n'.join(
- f'<iframe srcdoc=\'<html><body>{iframe_content}</body></html>\' '
- f'style="width:250px;height:150px;border:1px solid blue;"></iframe>'
- for i in range(15)
- )
-
- # SVG
- svg_shapes = ''.join(
- f'<rect x="{(i*12)%1000}" y="{(i*8)%500}" width="10" height="10" fill="hsl({i%360},50%,50%)" />'
- for i in range(500)
- )
- svg = f'<svg width="1000" height="500">{svg_shapes}</svg>'
-
- # Deeply nested section
- def nested(d: int) -> str:
- if d <= 0:
- return '<span class="leaf">*</span>'
- return ''.join(f'<div class="n-{d}">{nested(d-1)}</div>' for _ in range(3))
- deep = nested(7)
-
- return (
- f'<html><head><title>ULTIMATE STRESS TEST</title>{shadow_script}</head>'
- f'<body>'
- f'<h1>Ultimate Stress Test (~50k+ elements)</h1>'
- f'<section id="table-section"><h2>Tables</h2>{table}</section>'
- f'<section id="form-section"><h2>Forms</h2><form>{form_fields}</form></section>'
- f'<section id="shadow-section"><h2>Shadow DOM</h2></section>'
- f'<section id="listener-section"><h2>Event Listeners</h2></section>'
- f'<section id="iframe-section"><h2>Iframes</h2>{iframes}</section>'
- f'<section id="svg-section"><h2>SVG</h2>{svg}</section>'
- f'<section id="deep-section"><h2>Deep Nesting</h2>{deep}</section>'
- f'</body></html>'
- )
-
-
-def gen_page_shadow_iframe_combo(n_hosts: int, children_per: int, n_iframes: int) -> str:
- """Page 11: Shadow DOM hosts INSIDE iframes — worst of both worlds."""
- shadow_script = f'''
- <script>
- document.addEventListener('DOMContentLoaded', () => {{
- for (let i = 0; i < {n_hosts}; i++) {{
- const host = document.createElement('div');
- host.className = 'shadow-host';
- document.getElementById('c').appendChild(host);
- const shadow = host.attachShadow({{mode: 'open'}});
- for (let j = 0; j < {children_per}; j++) {{
- const el = document.createElement('div');
- el.innerHTML = '<input type="text" value="s' + i + '.' + j + '" /><button onclick="this.textContent=\\'ok\\'">Go</button><a href="#">Link</a>';
- el.addEventListener('click', () => {{}});
- el.addEventListener('pointerdown', () => {{}});
- shadow.appendChild(el);
- }}
- }}
- }});
- </script>'''
- iframe_body = f'<html><head>{shadow_script}</head><body><div id="c"></div></body></html>'
- # Escape for srcdoc
- iframe_body_escaped = iframe_body.replace("'", "&#39;").replace('"', "&quot;")
- iframes = '\n'.join(
- f"<iframe srcdoc='{iframe_body_escaped}' style='width:400px;height:300px;border:1px solid red;'></iframe>"
- for _ in range(n_iframes)
- )
- return (
- f'<html><head><title>Shadow+Iframe Combo</title></head>'
- f'<body><h1>Shadow DOM inside {n_iframes} iframes ({n_hosts}x{children_per} per frame)</h1>'
- f'<div>{iframes}</div></body></html>'
- )
-
-
-def gen_page_overlapping_layers(n_layers: int, elements_per: int) -> str:
- """Page 12: Many overlapping positioned elements — stress test for paint order."""
- layers = []
- for layer in range(n_layers):
- items = ''.join(
- f'<div class="item" style="position:absolute;left:{(i*30)%800}px;top:{(i*20)%600}px;'
- f'width:100px;height:50px;background:rgba({layer*20%255},{i*10%255},100,0.7);'
- f'z-index:{layer};"><span>L{layer}I{i}</span>'
- f'<button onclick="alert({layer})">btn</button></div>'
- for i in range(elements_per)
- )
- layers.append(
- f'<div class="layer" style="position:relative;width:1000px;height:800px;">{items}</div>'
- )
- total = n_layers * elements_per * 3
- return (
- f'<html><head><title>Overlapping Layers ({n_layers}x{elements_per})</title></head>'
- f'<body><h1>Overlapping layers ~{total} elements</h1>'
- f'<div style="position:relative;">{"".join(layers)}</div></body></html>'
- )
-
-
-def gen_page_mega_shadow_dom(n_hosts: int, children_per: int) -> str:
- """Page 13: Massive shadow DOM — 500 hosts x 50 children = 25k shadow elements."""
- script = f'''
- <script>
- document.addEventListener('DOMContentLoaded', () => {{
- const container = document.getElementById('mega-shadow');
- for (let i = 0; i < {n_hosts}; i++) {{
- const host = document.createElement('div');
- host.id = 'sh-' + i;
- container.appendChild(host);
- const shadow = host.attachShadow({{mode: 'open'}});
- shadow.innerHTML = '<style>:host {{ display: block; border: 1px solid #eee; margin: 1px; }}</style>';
- for (let j = 0; j < {children_per}; j++) {{
- const el = document.createElement('div');
- el.className = 'shadow-item';
- el.innerHTML = `
- <span>S${{i}}.${{j}}</span>
- <input type="text" value="val-${{i}}-${{j}}" />
- <button>Act</button>
- <select><option>A</option><option>B</option><option>C</option></select>
- <a href="#" onclick="return false">Link-${{i}}-${{j}}</a>
- `;
- el.addEventListener('click', () => {{}});
- shadow.appendChild(el);
- }}
- }}
- }});
- </script>'''
- total = n_hosts * children_per * 6 # div + span + input + button + select + a
- return (
- f'<html><head><title>Mega Shadow DOM ({n_hosts}x{children_per})</title>{script}</head>'
- f'<body><h1>Mega Shadow DOM ~{total} elements</h1>'
- f'<div id="mega-shadow"></div></body></html>'
- )
-
-
-def gen_page_cross_origin_shadow_iframe() -> str:
- """Page 14: Cross-origin iframes + shadow DOM + event listeners + forms + deep nesting — everything at once."""
- # Cross-origin iframes
- external_iframes = '\n'.join(
- f'<iframe src="https://example.com" style="width:300px;height:200px;border:2px solid red;" sandbox="allow-scripts"></iframe>'
- for _ in range(15)
- )
- # Same-origin iframes with shadow DOM inside
- shadow_in_iframe_script = '''
- <script>
- document.addEventListener('DOMContentLoaded', () => {
- for (let i = 0; i < 50; i++) {
- const host = document.createElement('div');
- document.body.appendChild(host);
- const shadow = host.attachShadow({mode: 'open'});
- for (let j = 0; j < 20; j++) {
- const el = document.createElement('div');
- el.innerHTML = '<input value="f' + i + '.' + j + '" /><button>X</button>';
- el.addEventListener('click', () => {});
- shadow.appendChild(el);
- }
- }
- });
- </script>'''
- iframe_html = f'<html><head>{shadow_in_iframe_script}</head><body><h3>Iframe with Shadow DOM</h3></body></html>'
- iframe_escaped = iframe_html.replace("'", "&#39;").replace('"', '&quot;')
- same_origin_iframes = '\n'.join(
- f"<iframe srcdoc='{iframe_escaped}' style='width:350px;height:250px;border:2px solid blue;'></iframe>"
- for _ in range(10)
- )
- # Heavy local content with deep nesting
- def deep_nest(d: int) -> str:
- if d <= 0:
- return '<input type="text" value="deep" /><button>X</button>'
- return ''.join(f'<div class="d-{d}" style="padding:1px;border:1px solid #ddd;">{deep_nest(d-1)}</div>' for _ in range(3))
- deep = deep_nest(6)
- # Shadow DOM section
- shadow_script = '''
- <script>
- document.addEventListener('DOMContentLoaded', () => {
- const c = document.getElementById('local-shadow');
- for (let i = 0; i < 200; i++) {
- const host = document.createElement('div');
- c.appendChild(host);
- const shadow = host.attachShadow({mode: 'open'});
- for (let j = 0; j < 30; j++) {
- const el = document.createElement('div');
- el.innerHTML = '<span>LS' + i + '.' + j + '</span><input value="ls-' + i + '-' + j + '" /><button>Go</button>';
- el.addEventListener('click', () => {});
- el.addEventListener('mousedown', () => {});
- shadow.appendChild(el);
- }
- }
- // Event listener heavy section
- const lc = document.getElementById('listener-heavy');
- for (let i = 0; i < 5000; i++) {
- const div = document.createElement('div');
- div.innerHTML = '<span>EL-' + i + '</span><a href="#" onclick="return false">link</a><input type="checkbox" />';
- div.addEventListener('click', () => {});
- div.addEventListener('pointerdown', () => {});
- div.addEventListener('mousedown', () => {});
- lc.appendChild(div);
- }
- });
- </script>'''
- # Forms section
- form_fields = ''.join(
- f'<div><label for="xf-{i}">F{i}</label>'
- f'<input type="{["text","email","number","date","tel","url","color","password","search","range"][i%10]}" id="xf-{i}" value="v{i}" />'
- f'<select><option>Opt1</option><option>Opt2</option><option>Opt3</option></select></div>'
- for i in range(1000)
- )
- # Table
- table_rows = ''.join(
- f'<tr>{"".join(f"<td><input value=r{r}c{c} /><button>X</button></td>" for c in range(15))}</tr>'
- for r in range(200)
- )
- # Overlapping positioned elements
- overlapping = ''.join(
- f'<div style="position:absolute;left:{(i*25)%900}px;top:{(i*18)%500}px;width:80px;height:40px;'
- f'background:rgba({i*7%255},{i*13%255},100,0.6);z-index:{i%20};">'
- f'<button>O{i}</button></div>'
- for i in range(500)
- )
-
- return (
- f'<html><head><title>EXTREME: Cross-Origin + Shadow + Iframes</title>{shadow_script}</head>'
- f'<body>'
- f'<h1>EXTREME STRESS TEST</h1>'
- f'<section><h2>Cross-Origin Iframes (15)</h2>{external_iframes}</section>'
- f'<section><h2>Same-Origin Iframes with Shadow DOM (10)</h2>{same_origin_iframes}</section>'
- f'<section id="local-shadow"><h2>Local Shadow DOM (200x30)</h2></section>'
- f'<section id="listener-heavy"><h2>Event Listeners (5000)</h2></section>'
- f'<section><h2>Forms (1000 fields)</h2><form>{form_fields}</form></section>'
- f'<section><h2>Table (200x15)</h2><table border="1">{table_rows}</table></section>'
- f'<section style="position:relative;width:1000px;height:600px;"><h2>Overlapping Layers (500)</h2>{overlapping}</section>'
- f'<section><h2>Deep Nesting (6x3)</h2>{deep}</section>'
- f'</body></html>'
- )
-
-
-def gen_page_100k_flat() -> str:
- """Page 15: Pure scale — 100k flat interactive elements. Tests raw throughput."""
- script = '''
- <script>
- document.addEventListener('DOMContentLoaded', () => {
- const c = document.getElementById('bulk');
- const frag = document.createDocumentFragment();
- for (let i = 0; i < 33000; i++) {
- const div = document.createElement('div');
- div.innerHTML = '<span>' + i + '</span><input value="v' + i + '" /><button>X</button>';
- frag.appendChild(div);
- }
- c.appendChild(frag);
- });
- </script>'''
- return (
- f'<html><head><title>100k Flat Elements</title>{script}</head>'
- f'<body><h1>~100k flat elements</h1><div id="bulk"></div></body></html>'
- )
-
-
-# ─── Test pages registry ──────────────────────────────────────────────────────
-
-TEST_PAGES = [
- ('01_flat_divs_1k', lambda: gen_page_flat_divs(1000)),
- ('02_table_100x10', lambda: gen_page_nested_tables(100, 10)),
- ('03_shadow_dom_200x10', lambda: gen_page_shadow_dom(200, 10)),
- ('04_iframes_20x50', lambda: gen_page_iframes(20, 50)),
- ('05_deep_nesting_8x3', lambda: gen_page_deep_nesting(8, 3)),
- ('06_mega_form_2000', lambda: gen_page_forms_mega(2000)),
- ('07_svg_5000', lambda: gen_page_svg_heavy(5000)),
- ('08_event_listeners_5k', lambda: gen_page_event_listeners(5000)),
- ('09_cross_origin', lambda: gen_page_cross_origin_iframes(10)),
- ('10_ultimate_stress', lambda: gen_page_ultimate_stress()),
- ('11_shadow_iframe_combo', lambda: gen_page_shadow_iframe_combo(100, 20, 10)),
- ('12_overlapping_layers', lambda: gen_page_overlapping_layers(50, 100)),
- ('13_mega_shadow_dom', lambda: gen_page_mega_shadow_dom(500, 50)),
- ('14_extreme_everything', lambda: gen_page_cross_origin_shadow_iframe()),
- ('15_100k_flat', lambda: gen_page_100k_flat()),
-]
-
-
-# ─── Local HTTP server ────────────────────────────────────────────────────────
-
-class QuietHandler(SimpleHTTPRequestHandler):
- def log_message(self, format, *args):
- pass # Suppress request logging
-
-
-def start_server(directory: str, port: int = 8765) -> HTTPServer:
- os.chdir(directory)
- server = HTTPServer(('127.0.0.1', port), QuietHandler)
- thread = Thread(target=server.serve_forever, daemon=True)
- thread.start()
- return server
-
-
-# ─── Test runner ───────────────────────────────────────────────────────────────
-
-async def test_dom_capture(page_url: str, page_name: str, browser_session: BrowserSession) -> dict:
- """Test DOM capture on a single page. Returns timing info."""
- result = {
- 'name': page_name,
- 'url': page_url,
- 'success': False,
- 'error': None,
- 'time_ms': 0,
- 'element_count': 0,
- 'selector_map_size': 0,
- }
-
- try:
- start = time.time()
-
- # Navigate to the page
- page = await browser_session.get_current_page()
- cdp_session = await browser_session.get_or_create_cdp_session(focus=True)
- await cdp_session.cdp_client.send.Page.navigate(
- params={'url': page_url}, session_id=cdp_session.session_id
- )
- # Wait for page load
- await asyncio.sleep(2.0)
-
- # Get browser state (this is the operation that times out on heavy pages)
- state = await browser_session.get_browser_state_summary(cached=False)
-
- elapsed_ms = (time.time() - start) * 1000
- result['time_ms'] = elapsed_ms
- result['success'] = True
-
- if state and state.dom_state:
- result['selector_map_size'] = len(state.dom_state.selector_map)
-
- # Get element count from page
- try:
- count_result = await cdp_session.cdp_client.send.Runtime.evaluate(
- params={'expression': 'document.querySelectorAll("*").length', 'returnByValue': True},
- session_id=cdp_session.session_id,
- )
- result['element_count'] = count_result.get('result', {}).get('value', 0)
- except Exception:
- pass
-
- except Exception as e:
- result['error'] = str(e)
- result['time_ms'] = (time.time() - start) * 1000
-
- return result
-
-
-async def test_agent_interaction(page_url: str, page_name: str) -> dict:
- """Test full agent interaction on a page (requires LLM API key)."""
- from browser_use import Agent
-
- llm = None
- try:
- from browser_use.llm.anthropic.chat import ChatAnthropic
- llm = ChatAnthropic(model='claude-sonnet-4-20250514', max_tokens=1024)
- except Exception as e:
- logger.warning(f'Failed to init ChatAnthropic: {e}')
- if llm is None:
- try:
- from browser_use.llm.openai.chat import ChatOpenAI
- llm = ChatOpenAI(model='gpt-4o-mini')
- except Exception as e:
- logger.warning(f'Failed to init ChatOpenAI: {e}')
- if llm is None:
- return {'name': page_name, 'success': False, 'error': 'No LLM API key found (set ANTHROPIC_API_KEY or OPENAI_API_KEY)', 'time_ms': 0, 'steps': 0}
-
- result = {'name': page_name, 'success': False, 'error': None, 'time_ms': 0, 'steps': 0}
-
- browser_session = BrowserSession(
- browser_profile=BrowserProfile(headless=True),
- )
-
- start = time.time()
- try:
- await browser_session.start()
-
- agent = Agent(
- task=f'Navigate to {page_url} and tell me the title of the page and how many interactive elements you can see. Just report the count, do not click anything.',
- llm=llm,
- browser_session=browser_session,
- max_steps=3,
- )
- history = await agent.run()
- result['time_ms'] = (time.time() - start) * 1000
- result['success'] = True
- result['steps'] = len(history.history) if history else 0
- except Exception as e:
- result['error'] = str(e)
- result['time_ms'] = (time.time() - start) * 1000
- finally:
- await browser_session.kill()
-
- return result
-
-
-async def run_dom_only_tests():
- """Run DOM capture tests (no LLM needed)."""
- # Generate HTML files
- pages_dir = Path(__file__).parent / 'generated'
- pages_dir.mkdir(exist_ok=True)
-
- logger.info('Generating test pages...')
- for name, generator in TEST_PAGES:
- html = generator()
- (pages_dir / f'{name}.html').write_text(html)
- logger.info(f' Generated {name}.html ({len(html):,} bytes)')
-
- # Start local server
- server = start_server(str(pages_dir))
- logger.info(f'Local server running on http://127.0.0.1:8765')
-
- # Create browser session
- browser_session = BrowserSession(
- browser_profile=BrowserProfile(
- headless=True,
- cross_origin_iframes=True,
- max_iframes=100,
- max_iframe_depth=5,
- ),
- )
- await browser_session.start()
-
- results = []
- try:
- for name, _ in TEST_PAGES:
- url = f'http://127.0.0.1:8765/{name}.html'
- logger.info(f'\n{"="*60}')
- logger.info(f'Testing: {name}')
- logger.info(f'{"="*60}')
-
- result = await test_dom_capture(url, name, browser_session)
-
- # If browser session became unstable, restart it for next test
- if not result['success'] and 'unstable' in str(result.get('error', '')).lower():
- logger.warning(f' Browser session unstable — restarting for next test...')
- try:
- await browser_session.kill()
- except Exception:
- pass
- browser_session = BrowserSession(
- browser_profile=BrowserProfile(
- headless=True,
- cross_origin_iframes=True,
- max_iframes=100,
- max_iframe_depth=5,
- ),
- )
- await browser_session.start()
- # Retry on fresh session
- result = await test_dom_capture(url, name, browser_session)
-
- results.append(result)
-
- status = 'PASS' if result['success'] else 'FAIL'
- logger.info(
- f' [{status}] {name}: {result["time_ms"]:.0f}ms, '
- f'{result["element_count"]} elements, '
- f'{result["selector_map_size"]} in selector_map'
- )
- if result['error']:
- logger.error(f' Error: {result["error"]}')
-
- finally:
- await browser_session.kill()
- server.shutdown()
-
- # Summary
- print('\n' + '=' * 70)
- print('RESULTS SUMMARY')
- print('=' * 70)
- print(f'{"Page":<30} {"Status":<8} {"Time":>8} {"Elements":>10} {"Selector":>10}')
- print('-' * 70)
- passed = 0
- failed = 0
- for r in results:
- status = 'PASS' if r['success'] else 'FAIL'
- if r['success']:
- passed += 1
- else:
- failed += 1
- print(
- f'{r["name"]:<30} {status:<8} {r["time_ms"]:>7.0f}ms '
- f'{r["element_count"]:>10} {r["selector_map_size"]:>10}'
- )
- if r['error']:
- print(f' ERROR: {r["error"][:80]}')
- print('-' * 70)
- print(f'Total: {passed} passed, {failed} failed out of {len(results)}')
-
- return failed == 0
-
-
-async def run_agent_tests():
- """Run full agent tests (requires LLM API key)."""
- # Generate HTML files
- pages_dir = Path(__file__).parent / 'generated'
- pages_dir.mkdir(exist_ok=True)
-
- logger.info('Generating test pages...')
- for name, generator in TEST_PAGES:
- html = generator()
- (pages_dir / f'{name}.html').write_text(html)
-
- # Start local server
- server = start_server(str(pages_dir))
- logger.info(f'Local server running on http://127.0.0.1:8765')
-
- # Only test a subset with the agent (it's slow)
- agent_test_pages = [
- TEST_PAGES[0], # flat divs (light)
- TEST_PAGES[5], # mega form (medium)
- TEST_PAGES[7], # event listeners (heavy)
- TEST_PAGES[9], # ultimate stress (extreme)
- ]
-
- results = []
- for name, _ in agent_test_pages:
- url = f'http://127.0.0.1:8765/{name}.html'
- logger.info(f'\nAgent test: {name}')
- result = await test_agent_interaction(url, name)
- results.append(result)
- status = 'PASS' if result['success'] else 'FAIL'
- logger.info(f' [{status}] {result["time_ms"]:.0f}ms')
- if result['error']:
- logger.error(f' Error: {result["error"]}')
-
- server.shutdown()
-
- print('\n' + '=' * 70)
- print('AGENT TEST RESULTS')
- print('=' * 70)
- for r in results:
- status = 'PASS' if r['success'] else 'FAIL'
- print(f' [{status}] {r["name"]}: {r["time_ms"]:.0f}ms')
- if r['error']:
- print(f' Error: {r["error"][:100]}')
-
- return all(r['success'] for r in results)
-
-
-def main():
- parser = argparse.ArgumentParser(description='Heavy page DOM capture stress test')
- parser.add_argument('--dom-only', action='store_true', help='Only test DOM capture (no LLM needed)')
- parser.add_argument('--agent', action='store_true', help='Run full agent tests (needs API key)')
- parser.add_argument('--verbose', '-v', action='store_true', help='Enable debug logging')
- args = parser.parse_args()
-
- if args.verbose:
- logging.getLogger().setLevel(logging.DEBUG)
- # Also enable browser-use logging
- logging.getLogger('browser_use').setLevel(logging.DEBUG)
- else:
- # Suppress noisy loggers but keep warnings
- logging.getLogger('browser_use').setLevel(logging.WARNING)
-
- # Load env from cloud backend if available
- cloud_env = Path('/Users/magnus/Developer/cloud/backend/.env')
- if cloud_env.exists():
- from dotenv import load_dotenv
- load_dotenv(cloud_env)
- logger.info('Loaded API keys from cloud backend .env')
-
- # Increase the BrowserStateRequest event timeout for extreme test pages.
- # Default is 30s which is fine for normal pages, but 100k+ element pages
- # need more time for Python-side tree construction.
- os.environ['TIMEOUT_BrowserStateRequestEvent'] = '120'
- logger.info('Set TIMEOUT_BrowserStateRequestEvent=120s for stress testing')
-
- if args.dom_only or (not args.agent):
- success = asyncio.run(run_dom_only_tests())
- else:
- success = asyncio.run(run_agent_tests())
-
- sys.exit(0 if success else 1)
-
-
-if __name__ == '__main__':
- main()