-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathtest_devtools_server.py
More file actions
369 lines (281 loc) · 11.9 KB
/
test_devtools_server.py
File metadata and controls
369 lines (281 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/env python3
"""
Pytest test suite for Chrome DevTools MCP
This test suite validates all MCP tools using pytest framework.
"""
from __future__ import annotations
import asyncio
import logging
import os
import platform
import subprocess
import sys
from collections.abc import AsyncGenerator
from typing import Any
import pytest
import pytest_asyncio
sys.path.insert(0, os.path.dirname(__file__))
from src.client import ChromeDevToolsClient
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
def get_chrome_path() -> str | None:
"""Get Chrome executable path for testing."""
system = platform.system()
paths = []
if system == "Darwin": # macOS
paths = [
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
"/Applications/Chromium.app/Contents/MacOS/Chromium",
]
elif system == "Linux":
paths = [
"/usr/bin/google-chrome",
"/usr/bin/google-chrome-stable",
"/usr/bin/chromium-browser",
"/usr/bin/chromium",
]
elif system == "Windows":
paths = [
"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",
]
for path in paths:
if os.path.exists(path):
return path
return None
@pytest_asyncio.fixture(scope="session")
async def chrome_setup() -> AsyncGenerator[dict[str, Any], None]:
"""Set up Chrome instance for testing."""
test_port = 9223
chrome_path = get_chrome_path()
if not chrome_path:
pytest.skip("Chrome not found for testing")
logger.info("Setting up test environment...")
cmd = [
str(chrome_path),
f"--remote-debugging-port={test_port}",
"--headless=new",
"--disable-gpu",
"--no-sandbox",
"--disable-dev-shm-usage",
"--user-data-dir=/tmp/chrome-test-profile",
]
chrome_process = subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
await asyncio.sleep(3)
logger.info(f"Chrome started for testing on port {test_port}")
yield {"port": test_port}
# Cleanup
logger.info("Cleaning up test environment...")
chrome_process.terminate()
chrome_process.wait()
logger.info("Test environment cleaned up")
@pytest_asyncio.fixture
async def cdp_client(chrome_setup: dict[str, Any]) -> AsyncGenerator[ChromeDevToolsClient, None]:
"""Create and connect CDP client."""
test_port = chrome_setup["port"]
client = ChromeDevToolsClient(port=test_port)
connected = await client.connect()
if not connected:
pytest.skip("Failed to connect to Chrome for testing")
await client.enable_domains()
logger.info("CDP client connected and ready")
yield client
await client.disconnect()
async def setup_test_page(cdp_client: ChromeDevToolsClient) -> None:
"""Set up a test page with elements for testing."""
html = """
<html>
<head><title>Test Page</title></head>
<body>
<div id="test-element" class="test-class">Test Content</div>
<button id="test-button">Test Button</button>
<script>console.log('Test page loaded');</script>
</body>
</html>
"""
await cdp_client.send_command("Page.navigate", {"url": f"data:text/html,{html}"})
await asyncio.sleep(1)
@pytest.mark.asyncio
async def test_chrome_detection() -> None:
"""Test Chrome detection and startup."""
chrome_path = get_chrome_path()
assert chrome_path is not None, "Chrome executable not found"
assert os.path.exists(chrome_path), f"Chrome path does not exist: {chrome_path}"
@pytest.mark.asyncio
async def test_connection_status(cdp_client: ChromeDevToolsClient) -> None:
"""Test CDP connection status."""
assert cdp_client.connected, "CDP client should be connected"
target_info = await cdp_client.get_target_info()
assert target_info is not None, "Should be able to get target info"
@pytest.mark.asyncio
async def test_navigation(cdp_client: ChromeDevToolsClient) -> None:
"""Test page navigation."""
await cdp_client.send_command(
"Page.navigate",
{"url": "data:text/html,<html><body><h1>Navigation Test</h1></body></html>"},
)
await asyncio.sleep(1)
@pytest.mark.asyncio
async def test_get_document(cdp_client: ChromeDevToolsClient) -> None:
"""Test document retrieval."""
await setup_test_page(cdp_client)
result = await cdp_client.send_command("DOM.getDocument", {"depth": 2})
assert "root" in result, "Document should have root element"
assert "nodeId" in result["root"], "Root should have node ID"
@pytest.mark.asyncio
async def test_query_selector(cdp_client: ChromeDevToolsClient) -> None:
"""Test CSS selector querying."""
await setup_test_page(cdp_client)
doc_result = await cdp_client.send_command("DOM.getDocument", {"depth": 2})
root_id = doc_result["root"]["nodeId"]
element_result = await cdp_client.send_command(
"DOM.querySelector", {"nodeId": root_id, "selector": "#test-element"}
)
assert element_result["nodeId"] != 0, "Should find test element"
@pytest.mark.asyncio
async def test_element_attributes(cdp_client: ChromeDevToolsClient) -> None:
"""Test element attribute retrieval."""
await setup_test_page(cdp_client)
doc_result = await cdp_client.send_command("DOM.getDocument", {"depth": 2})
root_id = doc_result["root"]["nodeId"]
element_result = await cdp_client.send_command(
"DOM.querySelector", {"nodeId": root_id, "selector": "#test-element"}
)
if element_result["nodeId"] != 0:
attrs_result = await cdp_client.send_command(
"DOM.getAttributes", {"nodeId": element_result["nodeId"]}
)
assert "attributes" in attrs_result, "Should return attributes"
@pytest.mark.asyncio
async def test_element_outer_html(cdp_client: ChromeDevToolsClient) -> None:
"""Test element HTML retrieval."""
await setup_test_page(cdp_client)
doc_result = await cdp_client.send_command("DOM.getDocument", {"depth": 2})
root_id = doc_result["root"]["nodeId"]
element_result = await cdp_client.send_command(
"DOM.querySelector", {"nodeId": root_id, "selector": "#test-element"}
)
if element_result["nodeId"] != 0:
html_result = await cdp_client.send_command(
"DOM.getOuterHTML", {"nodeId": element_result["nodeId"]}
)
assert "outerHTML" in html_result, "Should return outer HTML"
assert "test-element" in html_result["outerHTML"], "HTML should contain element ID"
@pytest.mark.asyncio
async def test_computed_styles(cdp_client: ChromeDevToolsClient) -> None:
"""Test computed style retrieval."""
await setup_test_page(cdp_client)
doc_result = await cdp_client.send_command("DOM.getDocument", {"depth": 2})
root_id = doc_result["root"]["nodeId"]
element_result = await cdp_client.send_command(
"DOM.querySelector", {"nodeId": root_id, "selector": "#test-element"}
)
if element_result["nodeId"] != 0:
styles_result = await cdp_client.send_command(
"CSS.getComputedStyleForNode", {"nodeId": element_result["nodeId"]}
)
assert "computedStyle" in styles_result, "Should return computed styles"
@pytest.mark.asyncio
async def test_javascript_execution(cdp_client: ChromeDevToolsClient) -> None:
"""Test JavaScript code execution."""
result = await cdp_client.send_command(
"Runtime.evaluate", {"expression": "2 + 2", "returnByValue": True}
)
assert "result" in result, "Should return result"
assert result["result"]["value"] == 4, "JavaScript execution should work correctly"
@pytest.mark.asyncio
async def test_console_logs(cdp_client: ChromeDevToolsClient) -> None:
"""Test console log capture."""
await cdp_client.send_command(
"Runtime.evaluate",
{"expression": "console.log('Test log message')", "returnByValue": True},
)
await asyncio.sleep(1)
# Note: Console log capture may vary by Chrome version
@pytest.mark.asyncio
async def test_network_monitoring(cdp_client: ChromeDevToolsClient) -> None:
"""Test network request monitoring."""
initial_requests = len(cdp_client.network_requests)
await cdp_client.send_command(
"Runtime.evaluate",
{
"expression": "fetch('data:text/plain,test').then(r => r.text())",
"returnByValue": True,
"awaitPromise": True,
},
)
await asyncio.sleep(1)
# Network monitoring may capture some requests
final_requests = len(cdp_client.network_requests)
assert final_requests >= initial_requests, "Should track network activity"
@pytest.mark.asyncio
async def test_performance_metrics(cdp_client: ChromeDevToolsClient) -> None:
"""Test performance metrics collection."""
await cdp_client.send_command("Performance.enable")
metrics_result = await cdp_client.send_command("Performance.getMetrics")
assert "metrics" in metrics_result, "Should return performance metrics"
assert len(metrics_result["metrics"]) > 0, "Should have performance data"
@pytest.mark.asyncio
async def test_page_info(cdp_client: ChromeDevToolsClient) -> None:
"""Test page information retrieval."""
await setup_test_page(cdp_client)
result = await cdp_client.send_command(
"Runtime.evaluate",
{
"expression": (
"({title: document.title, url: window.location.href, "
"readyState: document.readyState})"
),
"returnByValue": True,
},
)
assert "result" in result, "Should return page information"
page_info = result["result"]["value"]
assert "title" in page_info, "Should include page title"
assert "url" in page_info, "Should include page URL"
@pytest.mark.asyncio
async def test_storage_operations(cdp_client: ChromeDevToolsClient) -> None:
"""Test storage operations."""
try:
# Test storage quota check
await cdp_client.send_command("Storage.getUsageAndQuota", {"origin": "http://localhost"})
# Test storage clearing
await cdp_client.send_command(
"Storage.clearDataForOrigin", {"origin": "http://localhost", "storageTypes": "cookies"}
)
except Exception:
# Storage operations may not be fully available in headless mode
pytest.skip("Storage operations not fully available in test environment")
# Test for CSS operations
@pytest.mark.asyncio
async def test_css_media_queries(cdp_client: ChromeDevToolsClient) -> None:
"""Test CSS media queries retrieval."""
try:
result = await cdp_client.send_command("CSS.getMediaQueries")
assert "medias" in result, "Should return media queries"
except Exception:
# CSS domain may not be fully available in headless mode
pytest.skip("CSS operations not fully available in test environment")
@pytest.mark.asyncio
async def test_search_elements(cdp_client: ChromeDevToolsClient) -> None:
"""Test element searching."""
await setup_test_page(cdp_client)
try:
search_result = await cdp_client.send_command(
"DOM.performSearch", {"query": "test", "includeUserAgentShadowDOM": False}
)
search_id = search_result["searchId"]
result_count = search_result["resultCount"]
if result_count > 0:
results = await cdp_client.send_command(
"DOM.getSearchResults",
{"searchId": search_id, "fromIndex": 0, "toIndex": min(result_count, 10)},
)
assert "nodeIds" in results, "Should return search results"
# Cleanup search
await cdp_client.send_command("DOM.discardSearchResults", {"searchId": search_id})
except Exception:
# DOM search may not be fully available in all Chrome versions
pytest.skip("DOM search not fully available in test environment")