From 7844e5c0c69424d91e70a5ea08961881db4b575b Mon Sep 17 00:00:00 2001 From: kayqueGovetri Date: Thu, 24 Nov 2022 11:06:25 -0300 Subject: [PATCH 1/2] ENH: Implement __enter__ and __exit__ to open and stop browser --- botcity/web/bot.py | 6 ++++++ tests/test_browser.py | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/botcity/web/bot.py b/botcity/web/bot.py index dfa2455..dd8bec1 100644 --- a/botcity/web/bot.py +++ b/botcity/web/bot.py @@ -77,6 +77,12 @@ def __init__(self, headless=False): self._download_folder_path = os.getcwd() + def __enter__(self): + self.start_browser() + + def __exit__(self, exc_type, exc_value, traceback): + self.stop_browser() + @property def driver(self): """ diff --git a/tests/test_browser.py b/tests/test_browser.py index 0d98626..8178a89 100644 --- a/tests/test_browser.py +++ b/tests/test_browser.py @@ -6,6 +6,13 @@ from botcity.web import WebBot, By +def test_context(web: WebBot): + with web: + web.browse(conftest.INDEX_PAGE) + assert web.driver + assert web.driver is None + + def test_create_tab(web: WebBot): web.browse(conftest.INDEX_PAGE) title = web.page_title() From 17f3cdd3dfdbcc63549e021d7267fa646dad045e Mon Sep 17 00:00:00 2001 From: kayqueGovetri Date: Tue, 29 Nov 2022 17:16:06 -0300 Subject: [PATCH 2/2] FIX: Remove start_browser in __enter__ --- botcity/web/bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/botcity/web/bot.py b/botcity/web/bot.py index dd8bec1..faf858a 100644 --- a/botcity/web/bot.py +++ b/botcity/web/bot.py @@ -78,7 +78,7 @@ def __init__(self, headless=False): self._download_folder_path = os.getcwd() def __enter__(self): - self.start_browser() + pass def __exit__(self, exc_type, exc_value, traceback): self.stop_browser()