@@ -205,61 +205,79 @@ async def mock_create_session():
205
205
await client ._create_session ()
206
206
207
207
@pytest .mark .asyncio
208
- @mock .patch ("asyncio.to_thread " )
209
- async def test_init_playwright_in_thread (self , mock_to_thread ):
210
- """Test that playwright initialization works properly using asyncio.to_thread ."""
208
+ @mock .patch ("stagehand.main.async_playwright " )
209
+ async def test_init_playwright_with_timeout (self , mock_async_playwright ):
210
+ """Test that playwright initialization works properly with timeout ."""
211
211
# Create a mock playwright instance
212
212
mock_playwright_instance = mock .AsyncMock ()
213
213
mock_playwright_instance .stop = mock .AsyncMock ()
214
214
mock_playwright_instance .chromium = mock .MagicMock ()
215
215
mock_playwright_instance .firefox = mock .MagicMock ()
216
216
mock_playwright_instance .webkit = mock .MagicMock ()
217
217
218
- # Mock asyncio.to_thread to return our mock instance
219
- mock_to_thread .return_value = mock_playwright_instance
218
+ # Mock async_playwright().start() to return our mock instance as an awaitable
219
+ async def mock_start ():
220
+ return mock_playwright_instance
221
+
222
+ mock_async_playwright .return_value .start = mock_start
220
223
221
224
# Create a Stagehand client with LOCAL env
222
225
config = StagehandConfig (env = "LOCAL" )
223
226
client = Stagehand (config = config )
224
227
225
- # Test the threaded playwright initialization
226
- result = await client ._init_playwright_in_thread ()
228
+ # Test the playwright initialization with timeout
229
+ result = await client ._init_playwright_with_timeout ()
227
230
228
231
# Verify that the playwright instance was returned
229
232
assert result is mock_playwright_instance
230
233
231
- # Verify that asyncio.to_thread was called
232
- mock_to_thread .assert_called_once ()
233
-
234
234
# Verify the result has the expected attributes
235
235
assert hasattr (result , 'chromium' )
236
236
assert hasattr (result , 'firefox' )
237
237
assert hasattr (result , 'webkit' )
238
238
assert hasattr (result , 'stop' )
239
239
240
240
@pytest .mark .asyncio
241
- @mock .patch ("asyncio.to_thread" )
242
- async def test_init_playwright_in_thread_handles_exceptions (self , mock_to_thread ):
243
- """Test that threaded playwright initialization properly handles exceptions."""
244
- # Mock asyncio.to_thread to raise an exception
245
- mock_to_thread .side_effect = Exception ("Test exception" )
241
+ @mock .patch ("stagehand.main.async_playwright" )
242
+ async def test_init_playwright_with_timeout_handles_exceptions (self , mock_async_playwright ):
243
+ """Test that playwright initialization properly handles exceptions."""
244
+ # Mock async_playwright().start() to raise an exception as an awaitable
245
+ async def mock_start ():
246
+ raise Exception ("Test exception" )
247
+
248
+ mock_async_playwright .return_value .start = mock_start
246
249
247
250
# Create a Stagehand client with LOCAL env
248
251
config = StagehandConfig (env = "LOCAL" )
249
252
client = Stagehand (config = config )
250
253
251
254
# Test that the method raises a RuntimeError with our exception message
252
- with pytest .raises (RuntimeError , match = "Failed to initialize Playwright in background thread" ):
253
- await client ._init_playwright_in_thread ()
255
+ with pytest .raises (RuntimeError , match = "Failed to initialize Playwright" ):
256
+ await client ._init_playwright_with_timeout ()
257
+
258
+ @pytest .mark .asyncio
259
+ @mock .patch ("stagehand.main.asyncio.wait_for" )
260
+ async def test_init_playwright_with_timeout_handles_timeout (self , mock_wait_for ):
261
+ """Test that playwright initialization properly handles timeouts."""
262
+ # Mock asyncio.wait_for to raise a TimeoutError
263
+ mock_wait_for .side_effect = asyncio .TimeoutError ()
264
+
265
+ # Create a Stagehand client with LOCAL env
266
+ config = StagehandConfig (env = "LOCAL" )
267
+ client = Stagehand (config = config )
268
+
269
+ # Test that the method raises a RuntimeError with timeout message
270
+ with pytest .raises (RuntimeError , match = "Playwright initialization timed out" ):
271
+ await client ._init_playwright_with_timeout ()
254
272
255
273
@pytest .mark .asyncio
256
274
@mock .patch ("stagehand.main.cleanup_browser_resources" )
257
275
@mock .patch ("stagehand.main.connect_local_browser" )
258
- @mock .patch .object (Stagehand , "_init_playwright_in_thread " )
259
- async def test_init_uses_threaded_playwright (
276
+ @mock .patch .object (Stagehand , "_init_playwright_with_timeout " )
277
+ async def test_init_uses_playwright_with_timeout (
260
278
self , mock_init_playwright , mock_connect_local , mock_cleanup
261
279
):
262
- """Test that the main init() method uses threaded playwright initialization."""
280
+ """Test that the main init() method uses playwright initialization with timeout ."""
263
281
# Set up mocks
264
282
mock_playwright_instance = mock .AsyncMock ()
265
283
mock_init_playwright .return_value = mock_playwright_instance
@@ -286,7 +304,7 @@ async def test_init_uses_threaded_playwright(
286
304
# Initialize the client
287
305
await client .init ()
288
306
289
- # Verify that threaded playwright initialization was called
307
+ # Verify that playwright initialization with timeout was called
290
308
mock_init_playwright .assert_called_once ()
291
309
292
310
# Verify that the client is properly initialized
0 commit comments