@@ -205,17 +205,18 @@ async def mock_create_session():
205
205
await client ._create_session ()
206
206
207
207
@pytest .mark .asyncio
208
- @mock .patch ("stagehand.main.async_playwright " )
209
- async def test_init_playwright_in_thread (self , mock_async_playwright ):
210
- """Test that playwright initialization works properly in a separate thread ."""
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 ."""
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
+ mock_playwright_instance .firefox = mock .MagicMock ()
216
+ mock_playwright_instance .webkit = mock .MagicMock ()
215
217
216
- # Mock the async_playwright().start() to return our mock instance
217
- mock_async_playwright_start = mock .AsyncMock (return_value = mock_playwright_instance )
218
- mock_async_playwright .return_value .start = mock_async_playwright_start
218
+ # Mock asyncio.to_thread to return our mock instance
219
+ mock_to_thread .return_value = mock_playwright_instance
219
220
220
221
# Create a Stagehand client with LOCAL env
221
222
config = StagehandConfig (env = "LOCAL" )
@@ -227,26 +228,28 @@ async def test_init_playwright_in_thread(self, mock_async_playwright):
227
228
# Verify that the playwright instance was returned
228
229
assert result is mock_playwright_instance
229
230
230
- # Verify that async_playwright().start() was called
231
- mock_async_playwright_start .assert_called_once ()
231
+ # Verify that asyncio.to_thread was called
232
+ mock_to_thread .assert_called_once ()
232
233
233
234
# Verify the result has the expected attributes
234
235
assert hasattr (result , 'chromium' )
236
+ assert hasattr (result , 'firefox' )
237
+ assert hasattr (result , 'webkit' )
235
238
assert hasattr (result , 'stop' )
236
239
237
240
@pytest .mark .asyncio
238
- @mock .patch ("stagehand.main.async_playwright " )
239
- async def test_init_playwright_in_thread_handles_exceptions (self , mock_async_playwright ):
241
+ @mock .patch ("asyncio.to_thread " )
242
+ async def test_init_playwright_in_thread_handles_exceptions (self , mock_to_thread ):
240
243
"""Test that threaded playwright initialization properly handles exceptions."""
241
- # Mock async_playwright().start() to raise an exception
242
- mock_async_playwright . return_value . start .side_effect = Exception ("Test exception" )
244
+ # Mock asyncio.to_thread to raise an exception
245
+ mock_to_thread .side_effect = Exception ("Test exception" )
243
246
244
247
# Create a Stagehand client with LOCAL env
245
248
config = StagehandConfig (env = "LOCAL" )
246
249
client = Stagehand (config = config )
247
250
248
251
# Test that the method raises a RuntimeError with our exception message
249
- with pytest .raises (RuntimeError , match = "Failed to initialize Playwright in thread" ):
252
+ with pytest .raises (RuntimeError , match = "Failed to initialize Playwright in background thread" ):
250
253
await client ._init_playwright_in_thread ()
251
254
252
255
@pytest .mark .asyncio
0 commit comments