Skip to content

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

Setup structure

magmuellerefe227461d80
diff --git a/instruction.md b/instruction.md
new file mode 100644
index 000000000..d119ed848
--- /dev/null
+++ b/instruction.md
@@ -0,0 +1,602 @@
+# Product Requirements Document (PRD)
+
+---
+
+## **1. Project Overview**
+
+**Project Name**: AgentWeb Foundation Layer
+
+**Objective**: Develop a foundational layer that enables agents (specifically Large Language Models - LLMs) to interact with the web via a set of predefined browser actions. This layer allows developers to build higher-level planning agents capable of automating complex tasks, such as applying for jobs, data scraping, form filling, etc.
+
+---
+
+## **2. Goals and Objectives**
+
+- **Modular Architecture**: Create a modular system with clear separation of concerns, allowing easy maintenance and scalability.
+
+- **Detailed Documentation**: Provide comprehensive documentation, including JSON structures, function names, file names, and file structure.
+
+- **Clean State Representation**: Maintain a minimal and clean state representation after each action, focusing on interactable elements.
+
+- **Action Execution and Validation**: Execute actions reliably using Selenium and validate outcomes, handling ambiguities with LLM assistance.
+
+- **Testing Layer**: Implement an abstract testing layer using the OpenAI API to simulate agent interactions.
+
+---
+
+## **3. Detailed Requirements**
+
+### **3.1 Architecture Overview**
+
+The system consists of the following components:
+
+- **Agent Interface**: Handles communication with the LLM agent, providing state and receiving actions.
+
+- **Action Executor**: Executes browser actions using Selenium WebDriver.
+
+- **State Manager**: Manages the browser state, including current URL and interactable elements.
+
+- **HTML Cleaner**: Cleans up HTML content to minimize token usage when interacting with LLMs.
+
+- **Validation Module**: Validates the success of actions, potentially using a small LLM for ambiguity checks.
+
+- **Testing Layer**: An abstract layer that uses the OpenAI API for testing agent interactions without actual web execution.
+
+---
+
+### **3.2 File Structure**
+
+The project follows this directory structure:
+
+```
+agentweb/
+├── env/ # Virtual environment directory
+├── README.md # Project documentation
+├── requirements.txt # Python dependencies
+├── src/
+│ ├── agent_interface/
+│ │ ├── __init__.py
+│ │ ├── agent.py # Agent class and logic
+│ │ └── prompts.py # Agent and state prompts
+│ ├── actions/
+│ │ ├── __init__.py
+│ │ ├── browser_actions.py # Browser action implementations
+│ │ └── action_validator.py # Action validation logic
+│ ├── state_manager/
+│ │ ├── __init__.py
+│ │ ├── state.py # State management
+│ │ └── html_cleaner.py # HTML cleaning functions
+│ ├── utils/
+│ │ ├── __init__.py
+│ │ └── selenium_utils.py # Selenium setup and utilities
+│ └── tests/
+│ ├── __init__.py
+│ ├── test_agent.py # Tests for agent interface
+│ ├── test_actions.py # Tests for actions
+│ ├── test_state_manager.py # Tests for state manager
+│ └── test_utils.py # Tests for utilities
+└── temp/ # Temporary files (e.g., screenshots)
+```
+
+---
+
+### **3.3 Module Descriptions**
+
+#### **agent_interface/**
+
+- **agent.py**
+
+ ```python
+ class Agent:
+ def __init__(self):
+ # Initialize agent state
+ pass
+
+ def receive_state(self, state: dict, actions: list):
+ """
+ Receives the current state and possible actions.
+ """
+ pass
+
+ def decide_next_action(self) -> dict:
+ """
+ Determines the next action based on the current state.
+ """
+ pass
+ ```
+
+- **prompts.py**
+
+ ```python
+ AGENT_PROMPT = """
+ You are a web scraping agent. Your task is to control the browser where, for every step, you get the current state and a list of actions you can take.
+
+ Your task is:
+ Apply for 50 jobs for software engineering in Zurich.
+
+ Extra context:
+ {extra_context}
+
+ In every step, please only control the web browser with actions.
+ Actions can also give you back follow-up questions about the state of the website if your instructions are ambiguous.
+ """
+
+ STATE_PROMPT = """
+ {
+ "state": {state},
+ "actions": {actions}
+ }
+ """
+ ```
+
+#### **actions/**
+
+- **browser_actions.py**
+
+ ```python
+ from selenium import webdriver
+
+ class BrowserActions:
+ def __init__(self, driver: webdriver.Chrome):
+ self.driver = driver
+
+ def search_google(self, query: str):
+ # Implementation of Google search
+ pass
+
+ def go_to_url(self, url: str):
+ # Navigate to specified URL
+ pass
+
+ def click_element(self, identifier: dict):
+ # Click on an element based on identifier
+ pass
+
+ def input_text(self, identifier: dict, text: str):
+ # Input text into a field
+ pass
+
+ def navigate_back(self):
+ # Navigate back in browser history
+ pass
+
+ def reload_page(self):
+ # Reload the current page
+ pass
+
+ def close_tab(self):
+ # Close the current browser tab
+ pass
+ ```
+
+- **action_validator.py**
+
+ ```python
+ class ActionValidator:
+ def __init__(self, driver: webdriver.Chrome):
+ self.driver = driver
+
+ def is_action_successful(self, action: dict) -> bool:
+ # Check if the action was successful
+ pass
+
+ def check_ambiguity(self, action: dict) -> bool:
+ # Use LLM to check for ambiguity
+ pass
+ ```
+
+#### **state_manager/**
+
+- **state.py**
+
+ ```python
+ class StateManager:
+ def __init__(self, driver: webdriver.Chrome):
+ self.driver = driver
+
+ def get_current_state(self) -> dict:
+ # Retrieve current URL and interactable elements
+ pass
+
+ def get_interactable_elements(self) -> list:
+ # Extract interactable elements from the page
+ pass
+ ```
+
+- **html_cleaner.py**
+
+ ```python
+ import re
+ from bs4 import BeautifulSoup, Comment, Tag
+
+ def cleanup_html(html_content: str) -> str:
+ """
+ Clean up HTML content by removing unnecessary elements and formatting.
+ """
+ # Implementation as provided
+ pass
+ ```
+
+#### **utils/**
+
+- **selenium_utils.py**
+
+ ```python
+ from selenium import webdriver
+ from selenium.webdriver.chrome.options import Options
+ from webdriver_manager.chrome import ChromeDriverManager
+
+ def setup_selenium_driver(headless: bool = False) -> webdriver.Chrome:
+ """
+ Sets up and returns a Selenium WebDriver instance.
+ """
+ # Configure Chrome options
+ chrome_options = Options()
+ if headless:
+ chrome_options.add_argument("--headless")
+
+ # Disable automation flags
+ chrome_options.add_argument('--disable-blink-features=AutomationControlled')
+ chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
+ chrome_options.add_experimental_option('useAutomationExtension', False)
+
+ # Initialize the Chrome driver
+ driver = webdriver.Chrome(
+ service=Service(ChromeDriverManager().install()),
+ options=chrome_options
+ )
+ return driver
+ ```
+
+#### **tests/**
+
+- **test_agent.py**
+
+ ```python
+ import unittest
+ from src.agent_interface.agent import Agent
+
+ class TestAgent(unittest.TestCase):
+ def test_decide_next_action(self):
+ # Test the agent's decision-making
+ pass
+ ```
+
+- **test_actions.py**
+
+ ```python
+ import unittest
+ from src.actions.browser_actions import BrowserActions
+
+ class TestBrowserActions(unittest.TestCase):
+ def test_search_google(self):
+ # Test Google search action
+ pass
+ ```
+
+- **test_state_manager.py**
+
+ ```python
+ import unittest
+ from src.state_manager.state import StateManager
+
+ class TestStateManager(unittest.TestCase):
+ def test_get_current_state(self):
+ # Test state retrieval
+ pass
+ ```
+
+- **test_utils.py**
+
+ ```python
+ import unittest
+ from src.utils.selenium_utils import setup_selenium_driver
+
+ class TestSeleniumUtils(unittest.TestCase):
+ def test_setup_selenium_driver(self):
+ # Test Selenium driver setup
+ pass
+ ```
+
+---
+
+### **3.4 JSON Structures**
+
+#### **Agent Prompt Structure**
+
+```json
+{
+ "task": "Apply for 50 jobs for software engineering in Zurich.",
+ "extra_context": "{user_resume_and_additional_info}",
+ "state": {
+ "current_url": "string",
+ "interactable_elements": [
+ {
+ "type": "button",
+ "identifier": {
+ "id": "string",
+ "class": "string",
+ "name": "string",
+ "text": "string"
+ }
+ },
+ {
+ "type": "input",
+ "identifier": {
+ "id": "string",
+ "class": "string",
+ "name": "string",
+ "placeholder": "string"
+ }
+ },
+ // Additional elements...
+ ]
+ },
+ "actions": [
+ {
+ "type": "search_google",
+ "params": {
+ "query": "string"
+ }
+ },
+ {
+ "type": "go_to_url",
+ "params": {
+ "url": "string"
+ }
+ },
+ {
+ "type": "click_element",
+ "params": {
+ "identifier": {
+ "id": "string",
+ "class": "string",
+ "name": "string",
+ "text": "string"
+ }
+ }
+ },
+ // Additional actions...
+ ]
+}
+```
+
+#### **Action Response from Agent**
+
+```json
+{
+ "action": {
+ "type": "search_google",
+ "params": {
+ "query": "software engineering jobs in Zurich"
+ }
+ }
+}
+```
+
+#### **State Representation**
+
+```json
+{
+ "current_url": "https://www.example.com",
+ "interactable_elements": [
+ {
+ "type": "button",
+ "identifier": {
+ "id": "submit-btn",
+ "text": "Apply Now"
+ }
+ },
+ {
+ "type": "input",
+ "identifier": {
+ "name": "email",
+ "placeholder": "Enter your email"
+ }
+ },
+ // Additional elements...
+ ]
+}
+```
+
+---
+
+### **3.5 Function Names and Descriptions**
+
+- **Agent Class (`agent.py`):**
+
+ - `receive_state(state: dict, actions: list)`: Receives the current state and possible actions from the system.
+
+ - `decide_next_action() -> dict`: Determines the next action to execute based on the current state.
+
+- **Browser Actions (`browser_actions.py`):**
+
+ - `search_google(query: str)`: Performs a Google search with the provided query.
+
+ - `go_to_url(url: str)`: Navigates the browser to the specified URL.
+
+ - `click_element(identifier: dict)`: Clicks an element identified by attributes like ID, class, or text.
+
+ - `input_text(identifier: dict, text: str)`: Inputs text into a field identified by attributes.
+
+ - `navigate_back()`: Navigates back in the browser history.
+
+ - `reload_page()`: Reloads the current page.
+
+ - `close_tab()`: Closes the current browser tab.
+
+- **Action Validator (`action_validator.py`):**
+
+ - `is_action_successful(action: dict) -> bool`: Checks if the action was executed successfully.
+
+ - `check_ambiguity(action: dict) -> bool`: Determines if the action is ambiguous and requires clarification.
+
+- **State Manager (`state.py`):**
+
+ - `get_current_state() -> dict`: Retrieves the current browser state, including URL and interactable elements.
+
+ - `get_interactable_elements() -> list`: Extracts interactable elements from the page.
+
+- **HTML Cleaner (`html_cleaner.py`):**
+
+ - `cleanup_html(html_content: str) -> str`: Cleans up HTML content to reduce token usage.
+
+- **Selenium Utils (`selenium_utils.py`):**
+
+ - `setup_selenium_driver(headless: bool = False) -> webdriver.Chrome`: Sets up and returns a Selenium WebDriver instance.
+
+---
+
+## **4. Workflows**
+
+### **4.1 Agent Interaction Flow**
+
+1. **Initialization**: The agent is provided with a task and extra context.
+
+2. **State Presentation**: The system sends the current state and possible actions to the agent.
+
+3. **Agent Decision**: The agent decides on the next action and parameters.
+
+4. **Action Execution**: The system executes the action using Selenium.
+
+5. **State Update**: The state manager updates the current state.
+
+6. **Validation**: The action validator checks if the action was successful.
+
+ - If ambiguous, the agent is prompted for clarification.
+
+7. **Loop**: Steps 2-6 repeat until the task is completed.
+
+---
+
+## **5. Testing**
+
+### **5.1 Testing Layer**
+
+- Implement an abstract testing layer using the OpenAI API to simulate agent decisions without actual web interactions.
+
+### **5.2 Unit Tests**
+
+- **Agent Interface**: Test agent's ability to process state and decide actions.
+
+- **Browser Actions**: Test each browser action individually.
+
+- **State Manager**: Test state retrieval and interactable element extraction.
+
+- **HTML Cleaner**: Test HTML cleanup functionality.
+
+### **5.3 Integration Tests**
+
+- Simulate full agent workflows in a controlled environment.
+
+### **5.4 Test Data**
+
+- Use mock HTML pages and predefined states for consistent testing.
+
+---
+
+## **6. Dependencies**
+
+- **Python Packages**:
+
+ - `selenium`
+
+ - `beautifulsoup4`
+
+ - `webdriver-manager`
+
+ - `openai` (for testing purposes)
+
+ - `requests`
+
+ - `tokencost` (for calculating token costs)
+
+- **Browser Drivers**:
+
+ - Chrome WebDriver (managed via `webdriver-manager`)
+
+---
+
+## **7. Environment Setup**
+
+- **Virtual Environment**: Use `env/` directory for virtual environment setup.
+
+- **Installation**:
+
+ ```bash
+ python -m venv env
+ source env/bin/activate # On Windows: env\Scripts\activate
+ pip install -r requirements.txt
+ ```
+
+- **Requirements File (`requirements.txt`)**:
+
+ ```
+ selenium
+ beautifulsoup4
+ webdriver-manager
+ openai
+ requests
+ tokencost
+ ```
+
+---
+
+## **8. Additional Notes**
+
+- **Token Efficiency**: The HTML cleaner reduces the amount of data sent to the LLM, minimizing token usage and costs.
+
+- **Automation Detection Evasion**: Selenium is configured to avoid detection by disabling automation flags.
+
+- **State Representation**: Focuses on interactable elements to allow the agent to make informed decisions without being overwhelmed by unnecessary data.
+
+---
+
+## **9. Milestones**
+
+1. **Project Setup**: Establish the project structure and environment.
+
+2. **Implement HTML Cleaner**: Integrate the `cleanup_html` function.
+
+3. **Develop Browser Actions**: Implement basic browser actions in `browser_actions.py`.
+
+4. **Build State Manager**: Develop state retrieval and element extraction in `state.py`.
+
+5. **Create Agent Interface**: Implement the agent logic in `agent.py` and define prompts.
+
+6. **Implement Action Validator**: Develop validation logic in `action_validator.py`.
+
+7. **Set Up Testing Layer**: Use OpenAI API for simulating agent decisions.
+
+8. **Write Unit Tests**: Cover all modules with unit tests.
+
+9. **Integration Testing**: Perform end-to-end testing of agent workflows.
+
+10. **Documentation**: Update `README.md` with usage instructions and API documentation.
+
+---
+
+## **10. Future Enhancements**
+
+- **Advanced Actions**: Implement handling for complex web elements like dropdowns, modals, and file uploads.
+
+- **Concurrency**: Support multiple agents interacting simultaneously.
+
+- **Error Handling**: Enhance exception handling and logging mechanisms.
+
+- **User Interface**: Develop a GUI or web dashboard for monitoring agent activities.
+
+---
+
+## **11. Questions for Clarification**
+
+- **Specific Actions**: Are there any additional actions that need to be implemented beyond the ones listed?
+
+- **LLM Integration**: Should the system support real-time interaction with LLMs during operation, or is the OpenAI API solely for testing?
+
+- **Security Measures**: Are there any specific security protocols or data handling policies that need to be followed?
+
+- **Data Storage**: Will there be a need to store any data persistently (e.g., logs, states), and if so, what storage solutions are preferred?
+
+- **Deployment**: Is there a preferred deployment platform or containerization strategy (e.g., Docker)?
+
diff --git a/sample.ipynb b/sample.ipynb
index cdfda441a..245f966ee 100644
--- a/sample.ipynb
+++ b/sample.ipynb
@@ -146,7 +146,7 @@
],
"source": [
"from tokencost import calculate_prompt_cost, count_string_tokens\n",
- "from src.cleanup.utils import cleanup_html\n",
+ "from src.state_manager.html_cleaner import cleanup_html\n",
"\n",
"# Clean up html\n",
"# print(html_content)\n",
diff --git a/src/actions/action_validator.py b/src/actions/action_validator.py
new file mode 100644
index 000000000..b1817ae29
--- /dev/null
+++ b/src/actions/action_validator.py
@@ -0,0 +1,45 @@
+from selenium import webdriver
+from selenium.common.exceptions import TimeoutException, WebDriverException
+
+
+class ActionValidator:
+ def __init__(self, driver: webdriver.Chrome):
+ self.driver = driver
+
+ def is_action_successful(self, action: dict) -> bool:
+ """
+ Checks if the action was executed successfully.
+
+ Args:
+ action (dict): The action that was attempted
+
+ Returns:
+ bool: True if action was successful, False otherwise
+ """
+ try:
+ # Check for common failure indicators
+ if "error" in self.driver.title.lower():
+ return False
+
+ # Add specific checks based on action type
+ if action["type"] == "click_element":
+ # Verify element was actually clicked
+ pass
+
+ return True
+
+ except (TimeoutException, WebDriverException):
+ return False
+
+ def check_ambiguity(self, action: dict) -> bool:
+ """
+ Determines if the action is ambiguous and requires clarification.
+
+ Args:
+ action (dict): The action to check for ambiguity
+
+ Returns:
+ bool: True if action is ambiguous, False otherwise
+ """
+ # Implementation will use LLM to check for ambiguity
+ pass
diff --git a/src/actions/browser_actions.py b/src/actions/browser_actions.py
new file mode 100644
index 000000000..c8af58b20
--- /dev/null
+++ b/src/actions/browser_actions.py
@@ -0,0 +1,64 @@
+from selenium import webdriver
+from selenium.webdriver.common.by import By
+from selenium.webdriver.support.ui import WebDriverWait
+from selenium.webdriver.support import expected_conditions as EC
+
+
+class BrowserActions:
+ def __init__(self, driver: webdriver.Chrome):
+ self.driver = driver
+ self.wait = WebDriverWait(driver, 10)
+
+ def search_google(self, query: str):
+ """
+ Performs a Google search with the provided query.
+ """
+ self.driver.get("https://www.google.com")
+ search_box = self.wait.until(
+ EC.presence_of_element_located((By.NAME, "q"))
+ )
+ search_box.send_keys(query)
+ search_box.submit()
+
+ def go_to_url(self, url: str):
+ """
+ Navigates the browser to the specified URL.
+ """
+ self.driver.get(url)
+
+ def click_element(self, identifier: dict):
+ """
+ Clicks an element identified by attributes.
+ """
+ element = self._find_element(identifier)
+ element.click()
+
+ def input_text(self, identifier: dict, text: str):
+ """
+ Inputs text into a field identified by attributes.
+ """
+ element = self._find_element(identifier)
+ element.clear()
+ element.send_keys(text)
+
+ def _find_element(self, identifier: dict):
+ """
+ Helper method to find elements based on various identifiers.
+ """
+ for key, value in identifier.items():
+ try:
+ if key == "id":
+ return self.wait.until(
+ EC.presence_of_element_located((By.ID, value))
+ )
+ elif key == "class":
+ return self.wait.until(
+ EC.presence_of_element_located((By.CLASS_NAME, value))
+ )
+ elif key == "name":
+ return self.wait.until(
+ EC.presence_of_element_located((By.NAME, value))
+ )
+ except:
+ continue
+ raise Exception("Element not found with provided identifiers")
diff --git a/src/agent_interface/agent.py b/src/agent_interface/agent.py
new file mode 100644
index 000000000..ad6751ce8
--- /dev/null
+++ b/src/agent_interface/agent.py
@@ -0,0 +1,30 @@
+from typing import Dict, List
+
+
+class Agent:
+ def __init__(self):
+ self.current_state = None
+ self.available_actions = None
+ self.task = None
+ self.context = None
+
+ def receive_state(self, state: Dict, actions: List):
+ """
+ Receives the current state and possible actions.
+
+ Args:
+ state (Dict): Current browser state including URL and interactable elements
+ actions (List): List of possible actions that can be taken
+ """
+ self.current_state = state
+ self.available_actions = actions
+
+ def decide_next_action(self) -> Dict:
+ """
+ Determines the next action based on the current state.
+
+ Returns:
+ Dict: Action to be executed with its parameters
+ """
+ # Implementation will use LLM to decide next action
+ pass
diff --git a/src/agent_interface/prompts.py b/src/agent_interface/prompts.py
new file mode 100644
index 000000000..162bd3bc8
--- /dev/null
+++ b/src/agent_interface/prompts.py
@@ -0,0 +1,24 @@
+# System prompts for the agent
+AGENT_PROMPT = """
+You are a web scraping agent. Your task is to control the browser where, for every step,
+you get the current state and a list of actions you can take.
+
+Your task is:
+{task}
+
+Extra context:
+{extra_context}
+
+In every step, please only control the web browser with actions.
+Actions can also give you back follow-up questions about the state of the website
+if your instructions are ambiguous.
+"""
+
+# State representation prompt
+STATE_PROMPT = """
+Current browser state:
+{
+ "state": {state},
+ "actions": {actions}
+}
+"""
diff --git a/src/cleanup/utils.py b/src/state_manager/html_cleaner.py
similarity index 98%
rename from src/cleanup/utils.py
rename to src/state_manager/html_cleaner.py
index b8dff73e4..da0e3d0ec 100644
--- a/src/cleanup/utils.py
+++ b/src/state_manager/html_cleaner.py
@@ -1,5 +1,5 @@
+from bs4 import BeautifulSoup, Comment
import re
-from bs4 import BeautifulSoup, Comment, Tag
def cleanup_html(html_content):
diff --git a/src/state_manager/state.py b/src/state_manager/state.py
new file mode 100644
index 000000000..d15f36c5a
--- /dev/null
+++ b/src/state_manager/state.py
@@ -0,0 +1,55 @@
+from selenium import webdriver
+from selenium.webdriver.common.by import By
+from typing import Dict, List
+
+
+class StateManager:
+ def __init__(self, driver: webdriver.Chrome):
+ self.driver = driver
+
+ def get_current_state(self) -> Dict:
+ """
+ Retrieves current URL and interactable elements.
+
+ Returns:
+ Dict: Current state including URL and interactable elements
+ """
+ return {
+ "current_url": self.driver.current_url,
+ "interactable_elements": self.get_interactable_elements()
+ }
+
+ def get_interactable_elements(self) -> List:
+ """
+ Extracts interactable elements from the page.
+
+ Returns:
+ List: List of interactable elements with their properties
+ """
+ interactable_elements = []
+
+ # Find buttons
+ buttons = self.driver.find_elements(By.TAG_NAME, "button")
+ for button in buttons:
+ interactable_elements.append({
+ "type": "button",
+ "identifier": {
+ "id": button.get_attribute("id"),
+ "class": button.get_attribute("class"),
+ "text": button.text
+ }
+ })
+
+ # Find input fields
+ inputs = self.driver.find_elements(By.TAG_NAME, "input")
+ for input_field in inputs:
+ interactable_elements.append({
+ "type": "input",
+ "identifier": {
+ "id": input_field.get_attribute("id"),
+ "name": input_field.get_attribute("name"),
+ "placeholder": input_field.get_attribute("placeholder")
+ }
+ })
+
+ return interactable_elements
diff --git a/src/tests/test_actions.py b/src/tests/test_actions.py
new file mode 100644
index 000000000..c3d6bb02c
--- /dev/null
+++ b/src/tests/test_actions.py
@@ -0,0 +1,18 @@
+import unittest
+from selenium import webdriver
+from src.actions.browser_actions import BrowserActions
+from src.utils.selenium_utils import setup_selenium_driver
+
+
+class TestBrowserActions(unittest.TestCase):
+ def setUp(self):
+ self.driver = setup_selenium_driver(headless=True)
+ self.actions = BrowserActions(self.driver)
+
+ def tearDown(self):
+ self.driver.quit()
+
+ def test_go_to_url(self):
+ test_url = "https://www.example.com"
+ self.actions.go_to_url(test_url)
+ self.assertEqual(self.driver.current_url, test_url)
diff --git a/src/tests/test_agent.py b/src/tests/test_agent.py
new file mode 100644
index 000000000..8891b66a2
--- /dev/null
+++ b/src/tests/test_agent.py
@@ -0,0 +1,18 @@
+import unittest
+from src.agent_interface.agent import Agent
+
+
+class TestAgent(unittest.TestCase):
+ def setUp(self):
+ self.agent = Agent()
+
+ def test_receive_state(self):
+ state = {"current_url": "https://example.com"}
+ actions = [{"type": "click", "params": {}}]
+ self.agent.receive_state(state, actions)
+ self.assertEqual(self.agent.current_state, state)
+ self.assertEqual(self.agent.available_actions, actions)
+
+ def test_decide_next_action(self):
+ # Test will be implemented when decide_next_action is completed
+ pass
diff --git a/src/tests/test_state_manager.py b/src/tests/test_state_manager.py
new file mode 100644
index 000000000..88a2b347f
--- /dev/null
+++ b/src/tests/test_state_manager.py
@@ -0,0 +1,19 @@
+import unittest
+from selenium import webdriver
+from src.state_manager.state import StateManager
+from src.utils.selenium_utils import setup_selenium_driver
+
+
+class TestStateManager(unittest.TestCase):
+ def setUp(self):
+ self.driver = setup_selenium_driver(headless=True)
+ self.state_manager = StateManager(self.driver)
+
+ def tearDown(self):
+ self.driver.quit()
+
+ def test_get_current_state(self):
+ test_url = "https://www.example.com"
+ self.driver.get(test_url)
+ state = self.state_manager.get_current_state()
+ self.assertEqual(state["current_url"], test_url)
diff --git a/src/tests/test_utils.py b/src/tests/test_utils.py
new file mode 100644
index 000000000..8e75e4ffa
--- /dev/null
+++ b/src/tests/test_utils.py
@@ -0,0 +1,14 @@
+import unittest
+from src.utils.selenium_utils import setup_selenium_driver
+
+
+class TestSeleniumUtils(unittest.TestCase):
+ def test_setup_selenium_driver(self):
+ driver = setup_selenium_driver(headless=True)
+ self.assertIsNotNone(driver)
+ driver.quit()
+
+ def test_headless_mode(self):
+ driver = setup_selenium_driver(headless=True)
+ self.assertTrue("--headless" in driver.options.arguments)
+ driver.quit()
diff --git a/src/utils/selenium_utils.py b/src/utils/selenium_utils.py
new file mode 100644
index 000000000..4ae29ee5b
--- /dev/null
+++ b/src/utils/selenium_utils.py
@@ -0,0 +1,33 @@
+from selenium import webdriver
+from selenium.webdriver.chrome.options import Options
+from selenium.webdriver.chrome.service import Service
+from webdriver_manager.chrome import ChromeDriverManager
+
+
+def setup_selenium_driver(headless: bool = False) -> webdriver.Chrome:
+ """
+ Sets up and returns a Selenium WebDriver instance.
+
+ Args:
+ headless (bool): Whether to run browser in headless mode
+
+ Returns:
+ webdriver.Chrome: Configured Chrome WebDriver instance
+ """
+ # Configure Chrome options
+ chrome_options = Options()
+ if headless:
+ chrome_options.add_argument("--headless")
+
+ # Disable automation flags
+ chrome_options.add_argument('--disable-blink-features=AutomationControlled')
+ chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
+ chrome_options.add_experimental_option('useAutomationExtension', False)
+
+ # Initialize the Chrome driver
+ driver = webdriver.Chrome(
+ service=Service(ChromeDriverManager().install()),
+ options=chrome_options
+ )
+
+ return driver