Skip to content

Commit 7413732

Browse files
Add unit tests for ScriptGenerator class
Co-Authored-By: Sean Yang <[email protected]>
1 parent bddac18 commit 7413732

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

tests/test_script_generator.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
"""Unit tests for the ScriptGenerator class."""
2+
import sys
3+
from pathlib import Path
4+
import unittest
5+
6+
sys.path.append(str(Path(__file__).parent.parent))
7+
8+
from browser_use.script_generation.service import ScriptGenerator
9+
10+
11+
class TestScriptGenerator(unittest.TestCase):
12+
"""Test the ScriptGenerator class."""
13+
14+
def setUp(self):
15+
"""Set up test fixtures."""
16+
self.action_log = [
17+
{
18+
"action_type": "go_to_url",
19+
"params": {"url": "https://example.com"},
20+
"selector": None,
21+
"timestamp": 1620000000.0
22+
},
23+
{
24+
"action_type": "click_element_by_index",
25+
"params": {"index": 1},
26+
"selector": {
27+
"xpath": "//a[@href='https://www.iana.org/domains/example']",
28+
"tag_name": "a",
29+
"attributes": {"href": "https://www.iana.org/domains/example"},
30+
"index": 1,
31+
"is_visible": True,
32+
"is_interactive": True
33+
},
34+
"timestamp": 1620000001.0
35+
},
36+
{
37+
"action_type": "input_text",
38+
"params": {"index": 2, "text": "search term"},
39+
"selector": {
40+
"xpath": "//input[@id='search']",
41+
"tag_name": "input",
42+
"attributes": {"id": "search", "type": "text"},
43+
"index": 2,
44+
"is_visible": True,
45+
"is_interactive": True
46+
},
47+
"timestamp": 1620000002.0
48+
}
49+
]
50+
self.generator = ScriptGenerator(self.action_log)
51+
52+
def test_browserql_generation(self):
53+
"""Test BrowserQL script generation."""
54+
script = self.generator.to_browserql()
55+
56+
self.assertIn('goto(url: "https://example.com")', script)
57+
self.assertIn('click(selector: "//a[@href=\'https://www.iana.org/domains/example\']")', script)
58+
self.assertIn('type(selector: "//input[@id=\'search\']", text: "search term")', script)
59+
60+
self.assertTrue(script.startswith("mutation AutomateTask {"))
61+
self.assertTrue(script.endswith("}"))
62+
63+
def test_baas_v2_javascript_generation(self):
64+
"""Test BaaS V2 JavaScript script generation."""
65+
script = self.generator.to_baas_v2(language="javascript")
66+
67+
self.assertIn('await page.goto("https://example.com");', script)
68+
self.assertIn('await page.click("//a[@href=\'https://www.iana.org/domains/example\']");', script)
69+
self.assertIn('await page.type("//input[@id=\'search\']", "search term");', script)
70+
71+
self.assertIn("const puppeteer = require('puppeteer');", script)
72+
self.assertIn("async function run() {", script)
73+
self.assertIn("await browser.close();", script)
74+
75+
def test_baas_v2_python_generation(self):
76+
"""Test BaaS V2 Python script generation."""
77+
script = self.generator.to_baas_v2(language="python")
78+
79+
self.assertIn('await page.goto("https://example.com")', script)
80+
self.assertIn('await page.click("//a[@href=\'https://www.iana.org/domains/example\']")', script)
81+
self.assertIn('await page.fill("//input[@id=\'search\']", "search term")', script)
82+
83+
self.assertIn("import asyncio", script)
84+
self.assertIn("from playwright.async_api import async_playwright", script)
85+
self.assertIn("async def run():", script)
86+
self.assertIn("await browser.close()", script)
87+
88+
def test_empty_action_log(self):
89+
"""Test script generation with empty action log."""
90+
empty_generator = ScriptGenerator([])
91+
92+
browserql_script = empty_generator.to_browserql()
93+
self.assertEqual(browserql_script, "# No actions to convert")
94+
95+
baas_js_script = empty_generator.to_baas_v2(language="javascript")
96+
self.assertEqual(baas_js_script, "# No actions to convert")
97+
98+
baas_py_script = empty_generator.to_baas_v2(language="python")
99+
self.assertEqual(baas_py_script, "# No actions to convert")
100+
101+
def test_unsupported_action_types(self):
102+
"""Test handling of unsupported action types."""
103+
action_log_with_unsupported = self.action_log + [
104+
{
105+
"action_type": "unsupported_action",
106+
"params": {},
107+
"selector": None,
108+
"timestamp": 1620000003.0
109+
}
110+
]
111+
112+
generator = ScriptGenerator(action_log_with_unsupported)
113+
114+
browserql_script = generator.to_browserql()
115+
self.assertIn("# Unsupported action: unsupported_action", browserql_script)
116+
117+
baas_js_script = generator.to_baas_v2(language="javascript")
118+
self.assertIn("// Unsupported action: unsupported_action", baas_js_script)
119+
120+
baas_py_script = generator.to_baas_v2(language="python")
121+
self.assertIn("# Unsupported action: unsupported_action", baas_py_script)
122+
123+
124+
if __name__ == "__main__":
125+
unittest.main()

0 commit comments

Comments
 (0)