Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
Add test for radio button
Magnus Müllera157aa75771b
diff --git a/tests/ci/test_radio_buttons.html b/tests/ci/test_radio_buttons.htmlnew file mode 100644index 000000000..f2b5d7726--- /dev/null+++ b/tests/ci/test_radio_buttons.html@@ -0,0 +1,106 @@+<!DOCTYPE html>+<html>+<head>+ <title>Radio Button Test</title>+</head>+<body>+ <h1>Radio Button Test Page</h1>++ <form>+ <fieldset>+ <legend>Select your favorite color:</legend>++ <label>+ <input type="radio" name="color" value="red" id="radio-red">+ Red+ </label>+ <br>++ <label>+ <input type="radio" name="color" value="blue" id="radio-blue">+ Blue+ </label>+ <br>++ <label>+ <input type="radio" name="color" value="green" id="radio-green">+ Green+ </label>+ <br>+ </fieldset>++ <fieldset>+ <legend>Select your favorite animal:</legend>++ <label>+ <input type="radio" name="animal" value="cat" id="radio-cat">+ Cat+ </label>+ <br>++ <label>+ <input type="radio" name="animal" value="dog" id="radio-dog">+ Dog+ </label>+ <br>++ <label>+ <input type="radio" name="animal" value="bird" id="radio-bird">+ Bird+ </label>+ <br>+ </fieldset>++ <div id="result-message" style="margin-top: 20px; padding: 10px; background-color: #f0f0f0; display: none;">+ <p id="secret-text"></p>+ </div>+ </form>++ <script>+ function checkSelection() {+ const colorRadios = document.querySelectorAll('input[name="color"]');+ const animalRadios = document.querySelectorAll('input[name="animal"]');++ let selectedColor = null;+ let selectedAnimal = null;++ // Get selected color+ for (const radio of colorRadios) {+ if (radio.checked) {+ selectedColor = radio.value;+ break;+ }+ }++ // Get selected animal+ for (const radio of animalRadios) {+ if (radio.checked) {+ selectedAnimal = radio.value;+ break;+ }+ }++ const resultDiv = document.getElementById('result-message');+ const secretText = document.getElementById('secret-text');++ // Show secret if both Blue and Dog are selected+ if (selectedColor === 'blue' && selectedAnimal === 'dog') {+ secretText.textContent = 'SECRET_SUCCESS_12345: Blue dog combination unlocked!';+ resultDiv.style.display = 'block';+ resultDiv.style.backgroundColor = '#d4edda';+ } else if (selectedColor && selectedAnimal) {+ secretText.textContent = `Selected: ${selectedColor} ${selectedAnimal}`;+ resultDiv.style.display = 'block';+ resultDiv.style.backgroundColor = '#f8d7da';+ } else {+ resultDiv.style.display = 'none';+ }+ }++ // Add event listeners to all radio buttons+ document.querySelectorAll('input[type="radio"]').forEach(radio => {+ radio.addEventListener('change', checkSelection);+ });+ </script>+</body>+</html>diff --git a/tests/ci/test_radio_buttons.py b/tests/ci/test_radio_buttons.pynew file mode 100644index 000000000..2a58b202d--- /dev/null+++ b/tests/ci/test_radio_buttons.py@@ -0,0 +1,98 @@+# @file purpose: Test radio button interactions and serialization in browser-use+"""+Test file for verifying radio button clicking functionality and DOM serialization.++This test creates a simple HTML page with radio buttons, sends an agent to click them,+and logs the final agent message to show how radio buttons are represented in the serializer.++The serialization shows radio buttons as:+[index]<input type=radio name=groupname value=optionvalue checked=true/false />++Usage:+ uv run pytest tests/ci/test_radio_buttons.py -v -s+"""++from pathlib import Path++import pytest+from pytest_httpserver import HTTPServer++from browser_use.agent.service import Agent+from browser_use.browser import BrowserSession+from browser_use.browser.profile import BrowserProfile++[email protected](scope='session')+def http_server():+ """Create and provide a test HTTP server that serves static content."""+ server = HTTPServer()+ server.start()++ # Read the HTML file content+ html_file = Path(__file__).parent / 'test_radio_buttons.html'+ with open(html_file, 'r') as f:+ html_content = f.read()++ # Add route for radio buttons test page+ server.expect_request('/radio-test').respond_with_data(+ html_content,+ content_type='text/html',+ )++ yield server+ server.stop()++[email protected](scope='session')+def base_url(http_server):+ """Return the base URL for the test HTTP server."""+ return f'http://{http_server.host}:{http_server.port}'++[email protected](scope='module')+async def browser_session():+ """Create and provide a Browser instance with security disabled."""+ browser_session = BrowserSession(+ browser_profile=BrowserProfile(+ headless=True,+ user_data_dir=None,+ keep_alive=True,+ )+ )+ await browser_session.start()+ yield browser_session+ await browser_session.kill()+++class TestRadioButtons:+ """Test cases for radio button interactions."""++ async def test_radio_button_clicking(self, browser_session, base_url):+ """Test that agent can click radio buttons by checking for secret message."""++ task = f"Go to {base_url}/radio-test and click on the 'Blue' radio button and the 'Dog' radio button. After clicking both buttons, look for any text message that appears on the page and report exactly what you see."++ agent = Agent(+ task=task,+ browser_session=browser_session,+ max_actions_per_step=5,+ flash_mode=True,+ )++ # Run the agent+ history = await agent.run(max_steps=8)++ # Check if the secret message appears in the final response+ secret_found = False+ final_response = history.final_result()++ if final_response and 'SECRET_SUCCESS_12345' in final_response:+ secret_found = True+ print('\n✅ SUCCESS: Secret message found! Radio buttons were clicked correctly.')++ assert secret_found, (+ "Secret message 'SECRET_SUCCESS_12345' should be present, indicating both Blue and Dog radio buttons were clicked. Actual response: "+ + str(final_response)+ )++ print(f'\n🎉 Test completed successfully! Agent completed {len(history)} steps and found the secret message.')