@@ -205,17 +205,18 @@ async def mock_create_session():
205205 await client ._create_session ()
206206
207207 @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 ."""
211211 # Create a mock playwright instance
212212 mock_playwright_instance = mock .AsyncMock ()
213213 mock_playwright_instance .stop = mock .AsyncMock ()
214214 mock_playwright_instance .chromium = mock .MagicMock ()
215+ mock_playwright_instance .firefox = mock .MagicMock ()
216+ mock_playwright_instance .webkit = mock .MagicMock ()
215217
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
219220
220221 # Create a Stagehand client with LOCAL env
221222 config = StagehandConfig (env = "LOCAL" )
@@ -227,26 +228,28 @@ async def test_init_playwright_in_thread(self, mock_async_playwright):
227228 # Verify that the playwright instance was returned
228229 assert result is mock_playwright_instance
229230
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 ()
232233
233234 # Verify the result has the expected attributes
234235 assert hasattr (result , 'chromium' )
236+ assert hasattr (result , 'firefox' )
237+ assert hasattr (result , 'webkit' )
235238 assert hasattr (result , 'stop' )
236239
237240 @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 ):
240243 """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" )
243246
244247 # Create a Stagehand client with LOCAL env
245248 config = StagehandConfig (env = "LOCAL" )
246249 client = Stagehand (config = config )
247250
248251 # 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" ):
250253 await client ._init_playwright_in_thread ()
251254
252255 @pytest .mark .asyncio
0 commit comments