Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Add tests for browser agent (#2031)

Boxuan Li78241d9d4336
diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py
index 83abe9d7f2..d800386fad 100644
--- a/tests/integration/conftest.py
+++ b/tests/integration/conftest.py
@@ -3,6 +3,8 @@ import os
import re
import sys
from functools import partial
+from http.server import HTTPServer, SimpleHTTPRequestHandler
+from threading import Thread
import pytest
from litellm import completion
@@ -145,6 +147,25 @@ def patch_completion(monkeypatch, request):
monkeypatch.setattr('sys.stdin', user_responses)
+def http_server():
+ web_dir = os.path.join(os.path.dirname(__file__), 'static')
+ os.chdir(web_dir)
+ handler = SimpleHTTPRequestHandler
+
+ # Start the server
+ server = HTTPServer(('localhost', 8000), handler)
+ thread = Thread(target=server.serve_forever)
+ thread.setDaemon(True)
+ thread.start()
+
+ yield server
+
+ # Stop the server
+ server.shutdown()
+ thread.join()
+
+
def set_up():
global cur_id
cur_id = 0
diff --git a/tests/integration/mock/BrowsingAgent/test_browse_internet/prompt_001.log b/tests/integration/mock/BrowsingAgent/test_browse_internet/prompt_001.log
new file mode 100644
index 0000000000..dcde11e4e3
--- /dev/null
+++ b/tests/integration/mock/BrowsingAgent/test_browse_internet/prompt_001.log
@@ -0,0 +1,126 @@
+
+
+----------
+
+# Instructions
+Review the current state of the page and all other information to find the best
+possible next action to accomplish your goal. Your answer will be interpreted
+and executed by a program, make sure to follow the formatting instructions.
+
+# Goal:
+Browse localhost:8000, and tell me the ultimate answer to life. Do not ask me for confirmation at any point.
+
+# Action Space
+
+16 different types of actions are available.
+
+noop(wait_ms: float = 1000)
+ Examples:
+ noop()
+
+ noop(500)
+
+send_msg_to_user(text: str)
+ Examples:
+ send_msg_to_user('Based on the results of my search, the city was built in 1751.')
+
+scroll(delta_x: float, delta_y: float)
+ Examples:
+ scroll(0, 200)
+
+ scroll(-50.2, -100.5)
+
+fill(bid: str, value: str)
+ Examples:
+ fill('237', 'example value')
+
+ fill('45', 'multi-line\nexample')
+
+ fill('a12', 'example with "quotes"')
+
+select_option(bid: str, options: str | list[str])
+ Examples:
+ select_option('48', 'blue')
+
+ select_option('48', ['red', 'green', 'blue'])
+
+click(bid: str, button: Literal['left', 'middle', 'right'] = 'left', modifiers: list[typing.Literal['Alt', 'Control', 'Meta', 'Shift']] = [])
+ Examples:
+ click('51')
+
+ click('b22', button='right')
+
+ click('48', button='middle', modifiers=['Shift'])
+
+dblclick(bid: str, button: Literal['left', 'middle', 'right'] = 'left', modifiers: list[typing.Literal['Alt', 'Control', 'Meta', 'Shift']] = [])
+ Examples:
+ dblclick('12')
+
+ dblclick('ca42', button='right')
+
+ dblclick('178', button='middle', modifiers=['Shift'])
+
+hover(bid: str)
+ Examples:
+ hover('b8')
+
+press(bid: str, key_comb: str)
+ Examples:
+ press('88', 'Backspace')
+
+ press('a26', 'Control+a')
+
+ press('a61', 'Meta+Shift+t')
+
+focus(bid: str)
+ Examples:
+ focus('b455')
+
+clear(bid: str)
+ Examples:
+ clear('996')
+
+drag_and_drop(from_bid: str, to_bid: str)
+ Examples:
+ drag_and_drop('56', '498')
+
+upload_file(bid: str, file: str | list[str])
+ Examples:
+ upload_file('572', 'my_receipt.pdf')
+
+ upload_file('63', ['/home/bob/Documents/image.jpg', '/home/bob/Documents/file.zip'])
+
+go_back()
+ Examples:
+ go_back()
+
+go_forward()
+ Examples:
+ go_forward()
+
+goto(url: str)
+ Examples:
+ goto('http://www.example.com')
+
+Multiple actions can be provided at once. Example:
+fill('a12', 'example with "quotes"')
+click('51')
+click('48', button='middle', modifiers=['Shift'])
+Multiple actions are meant to be executed sequentially without any feedback from the page.
+Don't execute multiple actions at once if you need feedback from the page.
+
+
+
+----------
+
+# Current Accessibility Tree:
+
+
+# Previous Actions
+
+
+Here is an example with chain of thought of a valid action when clicking on a button:
+"
+In order to accomplish my goal I need to click on the button with bid 12
+```click("12")```
+"
diff --git a/tests/integration/mock/BrowsingAgent/test_browse_internet/prompt_002.log b/tests/integration/mock/BrowsingAgent/test_browse_internet/prompt_002.log
new file mode 100644
index 0000000000..7c03568e16
--- /dev/null
+++ b/tests/integration/mock/BrowsingAgent/test_browse_internet/prompt_002.log
@@ -0,0 +1,131 @@
+
+
+----------
+
+# Instructions
+Review the current state of the page and all other information to find the best
+possible next action to accomplish your goal. Your answer will be interpreted
+and executed by a program, make sure to follow the formatting instructions.
+
+# Goal:
+Browse localhost:8000, and tell me the ultimate answer to life. Do not ask me for confirmation at any point.
+
+# Action Space
+
+16 different types of actions are available.
+
+noop(wait_ms: float = 1000)
+ Examples:
+ noop()
+
+ noop(500)
+
+send_msg_to_user(text: str)
+ Examples:
+ send_msg_to_user('Based on the results of my search, the city was built in 1751.')
+
+scroll(delta_x: float, delta_y: float)
+ Examples:
+ scroll(0, 200)
+
+ scroll(-50.2, -100.5)
+
+fill(bid: str, value: str)
+ Examples:
+ fill('237', 'example value')
+
+ fill('45', 'multi-line\nexample')
+
+ fill('a12', 'example with "quotes"')
+
+select_option(bid: str, options: str | list[str])
+ Examples:
+ select_option('48', 'blue')
+
+ select_option('48', ['red', 'green', 'blue'])
+
+click(bid: str, button: Literal['left', 'middle', 'right'] = 'left', modifiers: list[typing.Literal['Alt', 'Control', 'Meta', 'Shift']] = [])
+ Examples:
+ click('51')
+
+ click('b22', button='right')
+
+ click('48', button='middle', modifiers=['Shift'])
+
+dblclick(bid: str, button: Literal['left', 'middle', 'right'] = 'left', modifiers: list[typing.Literal['Alt', 'Control', 'Meta', 'Shift']] = [])
+ Examples:
+ dblclick('12')
+
+ dblclick('ca42', button='right')
+
+ dblclick('178', button='middle', modifiers=['Shift'])
+
+hover(bid: str)
+ Examples:
+ hover('b8')
+
+press(bid: str, key_comb: str)
+ Examples:
+ press('88', 'Backspace')
+
+ press('a26', 'Control+a')
+
+ press('a61', 'Meta+Shift+t')
+
+focus(bid: str)
+ Examples:
+ focus('b455')
+
+clear(bid: str)
+ Examples:
+ clear('996')
+
+drag_and_drop(from_bid: str, to_bid: str)
+ Examples:
+ drag_and_drop('56', '498')
+
+upload_file(bid: str, file: str | list[str])
+ Examples:
+ upload_file('572', 'my_receipt.pdf')
+
+ upload_file('63', ['/home/bob/Documents/image.jpg', '/home/bob/Documents/file.zip'])
+
+go_back()
+ Examples:
+ go_back()
+
+go_forward()
+ Examples:
+ go_forward()
+
+goto(url: str)
+ Examples:
+ goto('http://www.example.com')
+
+Multiple actions can be provided at once. Example:
+fill('a12', 'example with "quotes"')
+click('51')
+click('48', button='middle', modifiers=['Shift'])
+Multiple actions are meant to be executed sequentially without any feedback from the page.
+Don't execute multiple actions at once if you need feedback from the page.
+
+
+
+----------
+
+# Current Accessibility Tree:
+RootWebArea 'The Ultimate Answer', focused
+ [8] heading 'The Ultimate Answer'
+ [9] paragraph ''
+ StaticText 'Click the button to reveal the answer to life, the universe, and everything.'
+ [10] button 'Click me'
+
+# Previous Actions
+goto('http://localhost:8000')
+
+
+Here is an example with chain of thought of a valid action when clicking on a button:
+"
+In order to accomplish my goal I need to click on the button with bid 12
+```click("12")```
+"
diff --git a/tests/integration/mock/BrowsingAgent/test_browse_internet/prompt_003.log b/tests/integration/mock/BrowsingAgent/test_browse_internet/prompt_003.log
new file mode 100644
index 0000000000..093173525d
--- /dev/null
+++ b/tests/integration/mock/BrowsingAgent/test_browse_internet/prompt_003.log
@@ -0,0 +1,133 @@
+
+
+----------
+
+# Instructions
+Review the current state of the page and all other information to find the best
+possible next action to accomplish your goal. Your answer will be interpreted
+and executed by a program, make sure to follow the formatting instructions.
+
+# Goal:
+Browse localhost:8000, and tell me the ultimate answer to life. Do not ask me for confirmation at any point.
+
+# Action Space
+
+16 different types of actions are available.
+
+noop(wait_ms: float = 1000)
+ Examples:
+ noop()
+
+ noop(500)
+
+send_msg_to_user(text: str)
+ Examples:
+ send_msg_to_user('Based on the results of my search, the city was built in 1751.')
+
+scroll(delta_x: float, delta_y: float)
+ Examples:
+ scroll(0, 200)
+
+ scroll(-50.2, -100.5)
+
+fill(bid: str, value: str)
+ Examples:
+ fill('237', 'example value')
+
+ fill('45', 'multi-line\nexample')
+
+ fill('a12', 'example with "quotes"')
+
+select_option(bid: str, options: str | list[str])
+ Examples:
+ select_option('48', 'blue')
+
+ select_option('48', ['red', 'green', 'blue'])
+
+click(bid: str, button: Literal['left', 'middle', 'right'] = 'left', modifiers: list[typing.Literal['Alt', 'Control', 'Meta', 'Shift']] = [])
+ Examples:
+ click('51')
+
+ click('b22', button='right')
+
+ click('48', button='middle', modifiers=['Shift'])
+
+dblclick(bid: str, button: Literal['left', 'middle', 'right'] = 'left', modifiers: list[typing.Literal['Alt', 'Control', 'Meta', 'Shift']] = [])
+ Examples:
+ dblclick('12')
+
+ dblclick('ca42', button='right')
+
+ dblclick('178', button='middle', modifiers=['Shift'])
+
+hover(bid: str)
+ Examples:
+ hover('b8')
+
+press(bid: str, key_comb: str)
+ Examples:
+ press('88', 'Backspace')
+
+ press('a26', 'Control+a')
+
+ press('a61', 'Meta+Shift+t')
+
+focus(bid: str)
+ Examples:
+ focus('b455')
+
+clear(bid: str)
+ Examples:
+ clear('996')
+
+drag_and_drop(from_bid: str, to_bid: str)
+ Examples:
+ drag_and_drop('56', '498')
+
+upload_file(bid: str, file: str | list[str])
+ Examples:
+ upload_file('572', 'my_receipt.pdf')
+
+ upload_file('63', ['/home/bob/Documents/image.jpg', '/home/bob/Documents/file.zip'])
+
+go_back()
+ Examples:
+ go_back()
+
+go_forward()
+ Examples:
+ go_forward()
+
+goto(url: str)
+ Examples:
+ goto('http://www.example.com')
+
+Multiple actions can be provided at once. Example:
+fill('a12', 'example with "quotes"')
+click('51')
+click('48', button='middle', modifiers=['Shift'])
+Multiple actions are meant to be executed sequentially without any feedback from the page.
+Don't execute multiple actions at once if you need feedback from the page.
+
+
+
+----------
+
+# Current Accessibility Tree:
+RootWebArea 'The Ultimate Answer', focused
+ [8] heading 'The Ultimate Answer'
+ [9] paragraph ''
+ StaticText 'Click the button to reveal the answer to life, the universe, and everything.'
+ [10] button 'Click me', focused
+ StaticText 'The answer is OpenDevin is all you need!'
+
+# Previous Actions
+goto('http://localhost:8000')
+click("10")
+
+
+Here is an example with chain of thought of a valid action when clicking on a button:
+"
+In order to accomplish my goal I need to click on the button with bid 12
+```click("12")```
+"
diff --git a/tests/integration/mock/BrowsingAgent/test_browse_internet/response_001.log b/tests/integration/mock/BrowsingAgent/test_browse_internet/response_001.log
new file mode 100644
index 0000000000..47ca048b3c
--- /dev/null
+++ b/tests/integration/mock/BrowsingAgent/test_browse_internet/response_001.log
@@ -0,0 +1,2 @@
+In order to accomplish my goal, I need to navigate to the localhost:8000 page.
+```goto('http://localhost:8000')```
diff --git a/tests/integration/mock/BrowsingAgent/test_browse_internet/response_002.log b/tests/integration/mock/BrowsingAgent/test_browse_internet/response_002.log
new file mode 100644
index 0000000000..2a952c1fb7
--- /dev/null
+++ b/tests/integration/mock/BrowsingAgent/test_browse_internet/response_002.log
@@ -0,0 +1,2 @@
+In order to accomplish my goal, I need to click on the button with bid 10 to reveal the answer to life, the universe, and everything.
+```click("10")```
diff --git a/tests/integration/mock/BrowsingAgent/test_browse_internet/response_003.log b/tests/integration/mock/BrowsingAgent/test_browse_internet/response_003.log
new file mode 100644
index 0000000000..5188618c0e
--- /dev/null
+++ b/tests/integration/mock/BrowsingAgent/test_browse_internet/response_003.log
@@ -0,0 +1,3 @@
+In order to accomplish my goal, I need to read the static text that reveals the answer to life, the universe, and everything. The text states: "The answer is OpenDevin is all you need!"
+
+```send_msg_to_user('The answer is OpenDevin is all you need!')```
diff --git a/tests/integration/regenerate.sh b/tests/integration/regenerate.sh
index d4987701bb..3d6f6097de 100755
--- a/tests/integration/regenerate.sh
+++ b/tests/integration/regenerate.sh
@@ -20,18 +20,20 @@ WORKSPACE_MOUNT_PATH_IN_SANDBOX="/workspace"
SANDBOX_TYPE="${SANDBOX_TYPE:-ssh}"
MAX_ITERATIONS=10
-agents=("MonologueAgent" "CodeActAgent" "PlannerAgent")
+agents=("BrowsingAgent" "MonologueAgent" "CodeActAgent" "PlannerAgent")
tasks=(
"Fix typos in bad.txt."
"Write a shell script 'hello.sh' that prints 'hello'."
"Use Jupyter IPython to write a text file containing 'hello world' to '/workspace/test.txt'."
"Install and import pymsgbox==1.0.9 and print it's version in /workspace/test.txt."
+ "Browse localhost:8000, and tell me the ultimate answer to life."
)
test_names=(
"test_edits"
"test_write_simple_script"
"test_ipython"
"test_ipython_module"
+ "test_browse_internet"
)
num_of_tests=${#test_names[@]}
@@ -61,6 +63,13 @@ run_test() {
return $?
}
+# browsing capability needs a local http server
+launch_http_server() {
+ poetry run python tests/integration/start_http_server.py &
+ HTTP_SERVER_PID=$!
+ sleep 10
+}
+
# generate prompts again, using existing LLM responses under tests/integration/mock/[agent]/[test_name]/response_*.log
# this is a compromise; the prompts might be non-sense yet still pass the test, because we don't use a real LLM to
# respond to the prompts. The benefit is developers don't have to regenerate real responses from LLM, if they only
@@ -80,6 +89,10 @@ regenerate_without_llm() {
}
regenerate_with_llm() {
+ if [[ "$test_name" = "test_browse_internet" ]]; then
+ launch_http_server
+ fi
+
rm -rf $WORKSPACE_BASE
mkdir -p $WORKSPACE_BASE
if [ -d "tests/integration/workspace/$test_name" ]; then
@@ -104,6 +117,13 @@ regenerate_with_llm() {
mkdir -p tests/integration/mock/$agent/$test_name/
mv logs/llm/**/* tests/integration/mock/$agent/$test_name/
+
+ if [[ "$test_name" = "test_browse_internet" ]]; then
+ # Terminate the HTTP server
+ kill $HTTP_SERVER_PID
+ fi
+
+
}
##############################################################
diff --git a/tests/integration/start_http_server.py b/tests/integration/start_http_server.py
new file mode 100644
index 0000000000..c498ef71bf
--- /dev/null
+++ b/tests/integration/start_http_server.py
@@ -0,0 +1,10 @@
+import os
+from http.server import HTTPServer, SimpleHTTPRequestHandler
+
+web_dir = os.path.join(os.path.dirname(__file__), 'static')
+os.chdir(web_dir)
+handler = SimpleHTTPRequestHandler
+
+# Start the server
+server = HTTPServer(('localhost', 8000), handler)
+server.serve_forever()
diff --git a/tests/integration/static/index.html b/tests/integration/static/index.html
new file mode 100644
index 0000000000..70e92b9a69
--- /dev/null
+++ b/tests/integration/static/index.html
@@ -0,0 +1,66 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <title>The Ultimate Answer</title>
+ <style>
+ body {
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ height: 100vh;
+ margin: 0;
+ background: linear-gradient(to right, #1e3c72, #2a5298);
+ color: #fff;
+ font-family: 'Arial', sans-serif;
+ text-align: center;
+ }
+ .container {
+ text-align: center;
+ padding: 20px;
+ background: rgba(255, 255, 255, 0.1);
+ border-radius: 10px;
+ box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
+ }
+ h1 {
+ font-size: 36px;
+ margin-bottom: 20px;
+ }
+ p {
+ font-size: 18px;
+ margin-bottom: 30px;
+ }
+ #showButton {
+ padding: 10px 20px;
+ font-size: 16px;
+ color: #1e3c72;
+ background: #fff;
+ border: none;
+ border-radius: 5px;
+ cursor: pointer;
+ transition: background 0.3s ease;
+ }
+ #showButton:hover {
+ background: #f0f0f0;
+ }
+ #result {
+ margin-top: 20px;
+ font-size: 24px;
+ }
+ </style>
+</head>
+<body>
+ <div class="container">
+ <h1>The Ultimate Answer</h1>
+ <p>Click the button to reveal the answer to life, the universe, and everything.</p>
+ <button id="showButton">Click me</button>
+ <div id="result"></div>
+ </div>
+ <script>
+ document.getElementById('showButton').addEventListener('click', function() {
+ document.getElementById('result').innerText = 'The answer is OpenDevin is all you need!';
+ });
+ </script>
+</body>
+</html>
diff --git a/tests/integration/test_agent.py b/tests/integration/test_agent.py
index 850a6ef587..5fa6579fc4 100644
--- a/tests/integration/test_agent.py
+++ b/tests/integration/test_agent.py
@@ -8,10 +8,18 @@ import pytest
from opendevin.controller.state.state import State
from opendevin.core.main import main
from opendevin.core.schema import AgentState
+from opendevin.events.action import (
+ AgentFinishAction,
+ MessageAction,
+)
workspace_base = os.getenv('WORKSPACE_BASE')
+ os.getenv('AGENT') == 'BrowsingAgent',
+ reason='BrowsingAgent is a specialized agent',
+)
@pytest.mark.skipif(
os.getenv('AGENT') == 'CodeActAgent' and os.getenv('SANDBOX_TYPE').lower() != 'ssh',
reason='CodeActAgent only supports ssh sandbox which is stateful',
@@ -34,6 +42,10 @@ def test_write_simple_script():
), f'Expected output "hello", but got "{result.stdout.strip()}"'
+ os.getenv('AGENT') == 'BrowsingAgent',
+ reason='BrowsingAgent is a specialized agent',
+)
@pytest.mark.skipif(
os.getenv('AGENT') == 'CodeActAgent' and os.getenv('SANDBOX_TYPE').lower() != 'ssh',
reason='CodeActAgent only supports ssh sandbox which is stateful',
@@ -122,3 +134,17 @@ def test_ipython_module():
assert (
content.strip() == '1.0.9'
), f'Expected content "1.0.9", but got "{content.strip()}"'
+
+
+ os.getenv('AGENT') != 'BrowsingAgent',
+ reason='currently only BrowsingAgent is capable of searching the internet',
+)
+def test_browse_internet(http_server):
+ # Execute the task
+ task = 'Browse localhost:8000, and tell me the ultimate answer to life. Do not ask me for confirmation at any point.'
+ final_state: State = asyncio.run(main(task, exit_on_message=True))
+ assert final_state.agent_state == AgentState.STOPPED
+ assert isinstance(final_state.history[-1][0], AgentFinishAction)
+ assert isinstance(final_state.history[-2][0], MessageAction)
+ assert 'OpenDevin is all you need!' in final_state.history[-2][0].content