"""Test radio button click interactions with various label association patterns. Verifies that the occlusion check correctly identifies label-input associations so that CDP click dispatch works for radio buttons whose labels visually overlap them. """ import pytest from pytest_httpserver import HTTPServer from browser_use.browser import BrowserSession from browser_use.browser.profile import BrowserProfile from browser_use.tools.service import Tools # -- HTML fixtures -- RADIO_SIBLING_HTML = """ Radio Sibling Label Test
""" RADIO_WRAPPED_HTML = """ Radio Wrapped Label Test
""" RADIO_CUSTOM_HTML = """ Radio Custom Styled Test
""" @pytest.fixture(scope='session') def http_server(): server = HTTPServer() server.start() server.expect_request('/radio-sibling').respond_with_data(RADIO_SIBLING_HTML, content_type='text/html') server.expect_request('/radio-wrapped').respond_with_data(RADIO_WRAPPED_HTML, content_type='text/html') server.expect_request('/radio-custom').respond_with_data(RADIO_CUSTOM_HTML, content_type='text/html') yield server server.stop() @pytest.fixture(scope='session') def base_url(http_server): return f'http://{http_server.host}:{http_server.port}' @pytest.fixture(scope='module') async def browser_session(): browser_session = BrowserSession( browser_profile=BrowserProfile( headless=True, user_data_dir=None, keep_alive=True, chromium_sandbox=False, ) ) await browser_session.start() yield browser_session await browser_session.kill() @pytest.fixture(scope='function') def tools(): return Tools() async def _get_checked_and_result(browser_session: BrowserSession, input_id: str) -> tuple[bool, str]: """Helper: returns (is_checked, result_div_text) via CDP.""" cdp_session = await browser_session.get_or_create_cdp_session() sid = cdp_session.session_id checked_result = await cdp_session.cdp_client.send.Runtime.evaluate( params={ 'expression': f"document.getElementById('{input_id}').checked", 'returnByValue': True, }, session_id=sid, ) is_checked = checked_result.get('result', {}).get('value', False) text_result = await cdp_session.cdp_client.send.Runtime.evaluate( params={ 'expression': "document.getElementById('result').textContent", 'returnByValue': True, }, session_id=sid, ) result_text = text_result.get('result', {}).get('value', '') return is_checked, result_text class TestRadioButtons: """Test radio button clicks across label association patterns.""" async def test_sibling_label_radio_click(self, tools: Tools, browser_session: BrowserSession, base_url: str): """Click a radio whose sibling