Refactored the `_map_action_to_playwright` method in `PlaywrightScriptGenerator` to address its excessive length and complexity stemming from numerous if/elif blocks.
Introduced a dictionary (`_action_handlers`) to map action type strings to dedicated private handler methods (e.g., `_map_go_to_url`, `_map_input_text`). The main `_map_action_to_playwright` method now delegates the call to the appropriate handler via this dictionary lookup.
This change significantly simplifies the main mapping logic, improves code readability, isolates action-specific logic, and makes the overall structure easier to maintain and extend. It directly resolves the concern about the method's length and error-proneness.
Implement the ability to generate executable Playwright scripts from an Agent's run history (`AgentHistoryList`).
This introduces the `PlaywrightScriptGenerator` class which translates recorded agent actions (clicks, inputs, navigation, etc.) into a Python script using Playwright's async API.
Key features:
- Uses XPath selectors derived from the agent's interaction history.
- Includes a robust helper function (`_try_locate_and_act`) with XPath prefix trimming for improved resilience on dynamic pages.
- Handles sensitive data placeholders by mapping them to environment variables in the generated script.
- Supports configuring the browser launch and context based on the original agent's `BrowserConfig` and `BrowserContextConfig`.
- Activated by setting the `save_playwright_script_path` option in `AgentSettings`.
Includes modifications to `Agent` service to trigger generation and `AgentHistoryList` view to add the saving method. Also adds a new example (`examples/features/playwright_script_generation.py`).
This feature enables reliable replay of agent runs, automated testing, and scripting of repetitive tasks discovered by the agent.
The double braces prevented the value from being interpolated properly. Updated to single braces so `format` correctly replaces it with the real value.