From 52ca315b0c775d278ff61b7f37d82059a89a3819 Mon Sep 17 00:00:00 2001 From: Patolovisk Date: Wed, 19 Mar 2025 11:19:11 -0300 Subject: [PATCH 1/3] feat: add flag to run browser on headless mode on start function --- pydoll/browser/base.py | 7 ++++++- pydoll/connection/connection.py | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pydoll/browser/base.py b/pydoll/browser/base.py index 7334e939..925f0f59 100644 --- a/pydoll/browser/base.py +++ b/pydoll/browser/base.py @@ -82,7 +82,7 @@ async def __aexit__(self, exc_type, exc_val, exc_tb): await self._connection_handler.close() - async def start(self) -> None: + async def start(self, headless: bool = False) -> None: """ Main method to start the browser. @@ -96,6 +96,11 @@ async def start(self) -> None: self.options.binary_location or self._get_default_binary_location() ) + if headless: + headless_arg = '--headless' + if headless_arg not in self.options.arguments: + self.options.add_argument(headless_arg) + self._setup_user_dir() proxy_config = self._proxy_manager.get_proxy_credentials() diff --git a/pydoll/connection/connection.py b/pydoll/connection/connection.py index 82f97cf5..0dc381ed 100644 --- a/pydoll/connection/connection.py +++ b/pydoll/connection/connection.py @@ -180,7 +180,8 @@ async def close(self): Closes the WebSocket connection and clears all event callbacks. """ await self.clear_callbacks() - await self._ws_connection.close() + if self._ws_connection is not None: + await self._ws_connection.close() logger.info('WebSocket connection closed.') async def _ensure_active_connection(self): From 533ad552b4489fac047be733d6025a4ecbaa28b4 Mon Sep 17 00:00:00 2001 From: Patolovisk Date: Sun, 23 Mar 2025 13:34:41 -0300 Subject: [PATCH 2/3] test: add tests for running on headless mode --- tests/test_chrome.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_chrome.py b/tests/test_chrome.py index f5fdaa69..ecadd6f3 100644 --- a/tests/test_chrome.py +++ b/tests/test_chrome.py @@ -102,6 +102,14 @@ async def test_start_browser_failure(mock_browser): await mock_browser.start() +@pytest.mark.asyncio +async def test_start_headless(mock_browser): + mock_browser._connection_handler.ping.return_value = True + await mock_browser.start(headless=True) + + assert "--headless" in mock_browser.options.arguments + + @pytest.mark.asyncio async def test_proxy_configuration(mock_browser): mock_browser._proxy_manager.get_proxy_credentials = MagicMock( From 7c6b4032b473a57ada85e74a220a76a3ec4783a2 Mon Sep 17 00:00:00 2001 From: Patolovisk Date: Mon, 24 Mar 2025 09:20:55 -0300 Subject: [PATCH 3/3] fix: move connection closed log inside if statement --- pydoll/connection/connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydoll/connection/connection.py b/pydoll/connection/connection.py index 0dc381ed..57cf63bd 100644 --- a/pydoll/connection/connection.py +++ b/pydoll/connection/connection.py @@ -182,7 +182,7 @@ async def close(self): await self.clear_callbacks() if self._ws_connection is not None: await self._ws_connection.close() - logger.info('WebSocket connection closed.') + logger.info('WebSocket connection closed.') async def _ensure_active_connection(self): """