66import trio
77from trio_websocket import serve_websocket
88
9- from trio_cdp import BrowserError , open_cdp_connection
9+ from . import fail_after
10+ from trio_cdp import BrowserError , open_cdp
1011
1112
1213HOST = '127.0.0.1'
@@ -26,6 +27,7 @@ def test_browser_error():
2627 'This is extra data about the error'
2728
2829
30+ @fail_after (1 )
2931async def test_connection_execute (nursery ):
3032 ''' Open a connection and execute a command on it. '''
3133 async def handler (request ):
@@ -53,13 +55,13 @@ async def handler(request):
5355 except Exception :
5456 logging .exception ('Server exception' )
5557 server = await start_server (nursery , handler )
56-
57- async with open_cdp_connection (server ) as conn :
58+ async with open_cdp (server ) as conn :
5859 targets = await conn .execute (target .get_targets ())
5960 assert len (targets ) == 1
6061 assert isinstance (targets [0 ], target .TargetInfo )
6162
6263
64+ @fail_after (1 )
6365async def test_connection_invalid_json ():
6466 ''' If the server sends invalid JSON, that exception is raised on the reader
6567 task, which crashes the entire connection. Therefore, the entire test needs
@@ -79,11 +81,12 @@ async def handler(request):
7981 logging .exception ('Server exception' )
8082 server = await start_server (nursery , handler )
8183
82- async with open_cdp_connection (server ) as conn :
84+ async with open_cdp (server ) as conn :
8385 targets = await conn .execute (target .get_targets ())
8486 assert exc_info .value .code == - 32700 # JSON parse error
8587
8688
89+ @fail_after (1 )
8790async def test_connection_browser_error (nursery ):
8891 ''' If the browser sends an error with a valid command ID, then that error
8992 should be raised at the point where the command was executed. Compare to
@@ -110,13 +113,14 @@ async def handler(request):
110113 logging .exception ('Server exception' )
111114 server = await start_server (nursery , handler )
112115
113- async with open_cdp_connection (server ) as conn :
116+ async with open_cdp (server ) as conn :
114117 with pytest .raises (BrowserError ) as exc_info :
115118 targets = await conn .execute (target .get_targets ())
116119
117120 assert exc_info .value .code == - 32000
118121
119122
123+ @fail_after (1 )
120124async def test_session_execute (nursery ):
121125 ''' Open a session and execute a command on it. '''
122126 async def handler (request ):
@@ -159,14 +163,15 @@ async def handler(request):
159163 logging .exception ('Server exception' )
160164 server = await start_server (nursery , handler )
161165
162- async with open_cdp_connection (server ) as conn :
166+ async with open_cdp (server ) as conn :
163167 session = await conn .open_session (target .TargetID ('target1' ))
164168 assert session .session_id == 'session1'
165169 node_id = await session .execute (
166170 dom .query_selector (dom .NodeId (0 ),'p.foo' ))
167171 assert node_id == 1
168172
169173
174+ @fail_after (1 )
170175async def test_wait_for_event (nursery ):
171176 ''' The server sends 2 different events. The client is listening for a
172177 specific type of event and therefore only sees the 2nd one. '''
@@ -196,7 +201,7 @@ async def handler(request):
196201 logging .exception ('Server exception' )
197202 server = await start_server (nursery , handler )
198203
199- async with open_cdp_connection (server ) as conn :
204+ async with open_cdp (server ) as conn :
200205 async with conn .wait_for (page .LoadEventFired ) as event :
201206 # In real code we would do something here to trigger a load event,
202207 # e.g. clicking a link.
@@ -205,6 +210,7 @@ async def handler(request):
205210 assert event .value .timestamp == 2
206211
207212
213+ @fail_after (1 )
208214async def test_listen_for_events (nursery ):
209215 ''' The server sends 2 different events. The client is listening for a
210216 specific type of event and therefore only sees the 2nd one. '''
@@ -234,7 +240,7 @@ async def handler(request):
234240 logging .exception ('Server exception' )
235241 server = await start_server (nursery , handler )
236242
237- async with open_cdp_connection (server ) as conn :
243+ async with open_cdp (server ) as conn :
238244 n = 1
239245 async for event in conn .listen (page .LoadEventFired ):
240246 assert isinstance (event , page .LoadEventFired )
0 commit comments