From de2bfd48913e6a7d880284fc315c38c68a743238 Mon Sep 17 00:00:00 2001 From: gruhn Date: Sun, 1 Dec 2024 17:00:58 +0100 Subject: [PATCH 1/6] add: option to share selenium web driver in test or not --- test/test_web.py | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/test/test_web.py b/test/test_web.py index d3ab2cb..d499de6 100644 --- a/test/test_web.py +++ b/test/test_web.py @@ -2,6 +2,7 @@ import enum import json import math +import os import shutil import socket import subprocess @@ -52,28 +53,55 @@ def __repr__(self): return f"StatusTestInterface({self.name})" -@unittest.skipIf(shutil.which("geckodriver") is None, "Selenium's geckodriver is not available in PATH") +@unittest.skipIf(shutil.which("geckodriver") is None, + "Selenium's geckodriver is not available in PATH") class AbstractWebTest(unittest.TestCase): driver: webdriver.Firefox def setUp(self) -> None: + if not self.share_selenium_web_driver(): + opts = selenium.webdriver.firefox.options.Options() + opts.add_argument("-headless") + self.driver = webdriver.Firefox(options=opts) + self.server_runner = InterfaceThreadRunner(shc.web.WebServer, "localhost", 42080, 'index') self.server = self.server_runner.interface + @classmethod def setUpClass(cls) -> None: - opts = selenium.webdriver.firefox.options.Options() - opts.add_argument("-headless") - cls.driver = webdriver.Firefox(options=opts) + if cls.share_selenium_web_driver(): + opts = selenium.webdriver.firefox.options.Options() + opts.add_argument("-headless") + cls.driver = webdriver.Firefox(options=opts) def tearDown(self) -> None: + if not self.share_selenium_web_driver(): + self.driver.close() + self.driver.quit() + self.server_runner.stop() @classmethod def tearDownClass(cls) -> None: - cls.driver.close() - cls.driver.quit() + if cls.share_selenium_web_driver(): + cls.driver.close() + cls.driver.quit() + + @staticmethod + def share_selenium_web_driver() -> bool: + """Should the selenium driver be shared or not. + + Checks whether the environment variable SHARE_SELENIUM_WEB_DRIVER is set. + On WSL default ist not sharing the driver since this causes concirrency conflicts. + """ + if (value := os.getenv('SHARE_SELENIUM_WEB_DRIVER')) is not None: + return value.strip().lower() in ['1', 'true', 'yes', 'on'] + + if "WSL_DISTRO_NAME" in os.environ or "WSL_INTEROP" in os.environ: + return False + return True class SimpleWebTest(AbstractWebTest): def test_basic(self) -> None: From 4051838d03340bb61c73ee21e16f1c3cfa87b1c7 Mon Sep 17 00:00:00 2001 From: gruhn Date: Sun, 1 Dec 2024 17:06:35 +0100 Subject: [PATCH 2/6] fix: flake issues --- test/test_web.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_web.py b/test/test_web.py index d499de6..924bc0b 100644 --- a/test/test_web.py +++ b/test/test_web.py @@ -67,7 +67,6 @@ def setUp(self) -> None: self.server_runner = InterfaceThreadRunner(shc.web.WebServer, "localhost", 42080, 'index') self.server = self.server_runner.interface - @classmethod def setUpClass(cls) -> None: if cls.share_selenium_web_driver(): @@ -103,6 +102,7 @@ def share_selenium_web_driver() -> bool: return True + class SimpleWebTest(AbstractWebTest): def test_basic(self) -> None: self.server_runner.start() From e216c368bd766f891fa84d900fce28248a4a601c Mon Sep 17 00:00:00 2001 From: gruhn Date: Sun, 1 Dec 2024 17:12:53 +0100 Subject: [PATCH 3/6] fix: typo --- test/test_web.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/test_web.py b/test/test_web.py index 924bc0b..d0e8d05 100644 --- a/test/test_web.py +++ b/test/test_web.py @@ -53,8 +53,7 @@ def __repr__(self): return f"StatusTestInterface({self.name})" -@unittest.skipIf(shutil.which("geckodriver") is None, - "Selenium's geckodriver is not available in PATH") +@unittest.skipIf(shutil.which("geckodriver") is None, "Selenium's geckodriver is not available in PATH") class AbstractWebTest(unittest.TestCase): driver: webdriver.Firefox From 26302ab3f6f7ea0e03cc53ac29cd051fca5b7646 Mon Sep 17 00:00:00 2001 From: gruhn Date: Sun, 1 Dec 2024 17:13:32 +0100 Subject: [PATCH 4/6] fix: typo --- test/test_web.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_web.py b/test/test_web.py index d0e8d05..6e27fa8 100644 --- a/test/test_web.py +++ b/test/test_web.py @@ -91,7 +91,7 @@ def share_selenium_web_driver() -> bool: """Should the selenium driver be shared or not. Checks whether the environment variable SHARE_SELENIUM_WEB_DRIVER is set. - On WSL default ist not sharing the driver since this causes concirrency conflicts. + On WSL default ist not sharing the driver since this causes concurrency conflicts. """ if (value := os.getenv('SHARE_SELENIUM_WEB_DRIVER')) is not None: return value.strip().lower() in ['1', 'true', 'yes', 'on'] From 5b1849476a6813d2432df970817d53d35d78db3f Mon Sep 17 00:00:00 2001 From: gruhn Date: Sun, 1 Dec 2024 22:31:29 +0100 Subject: [PATCH 5/6] fix: doc and env var name --- test/test_web.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/test_web.py b/test/test_web.py index 6e27fa8..5df2768 100644 --- a/test/test_web.py +++ b/test/test_web.py @@ -58,7 +58,7 @@ class AbstractWebTest(unittest.TestCase): driver: webdriver.Firefox def setUp(self) -> None: - if not self.share_selenium_web_driver(): + if not self.resue_selenium_web_driver(): opts = selenium.webdriver.firefox.options.Options() opts.add_argument("-headless") self.driver = webdriver.Firefox(options=opts) @@ -68,13 +68,13 @@ def setUp(self) -> None: @classmethod def setUpClass(cls) -> None: - if cls.share_selenium_web_driver(): + if cls.resue_selenium_web_driver(): opts = selenium.webdriver.firefox.options.Options() opts.add_argument("-headless") cls.driver = webdriver.Firefox(options=opts) def tearDown(self) -> None: - if not self.share_selenium_web_driver(): + if not self.resue_selenium_web_driver(): self.driver.close() self.driver.quit() @@ -82,18 +82,18 @@ def tearDown(self) -> None: @classmethod def tearDownClass(cls) -> None: - if cls.share_selenium_web_driver(): + if cls.resue_selenium_web_driver(): cls.driver.close() cls.driver.quit() @staticmethod - def share_selenium_web_driver() -> bool: - """Should the selenium driver be shared or not. + def resue_selenium_web_driver() -> bool: + """Determine whether the selenium driver shall be shared between all consecutive tests in this class. - Checks whether the environment variable SHARE_SELENIUM_WEB_DRIVER is set. + Checks whether the environment variable SHC_TEST_REUSE_WEB_DRIVER is set. On WSL default ist not sharing the driver since this causes concurrency conflicts. """ - if (value := os.getenv('SHARE_SELENIUM_WEB_DRIVER')) is not None: + if (value := os.getenv('SHC_TEST_REUSE_WEB_DRIVER')) is not None: return value.strip().lower() in ['1', 'true', 'yes', 'on'] if "WSL_DISTRO_NAME" in os.environ or "WSL_INTEROP" in os.environ: From 06229fffd0401bb052dfbc33850bddd2080b19f8 Mon Sep 17 00:00:00 2001 From: gruhn Date: Sun, 1 Dec 2024 22:32:54 +0100 Subject: [PATCH 6/6] fix: typo --- test/test_web.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/test_web.py b/test/test_web.py index 5df2768..1020e5c 100644 --- a/test/test_web.py +++ b/test/test_web.py @@ -58,7 +58,7 @@ class AbstractWebTest(unittest.TestCase): driver: webdriver.Firefox def setUp(self) -> None: - if not self.resue_selenium_web_driver(): + if not self.reuse_selenium_web_driver(): opts = selenium.webdriver.firefox.options.Options() opts.add_argument("-headless") self.driver = webdriver.Firefox(options=opts) @@ -68,13 +68,13 @@ def setUp(self) -> None: @classmethod def setUpClass(cls) -> None: - if cls.resue_selenium_web_driver(): + if cls.reuse_selenium_web_driver(): opts = selenium.webdriver.firefox.options.Options() opts.add_argument("-headless") cls.driver = webdriver.Firefox(options=opts) def tearDown(self) -> None: - if not self.resue_selenium_web_driver(): + if not self.reuse_selenium_web_driver(): self.driver.close() self.driver.quit() @@ -82,12 +82,12 @@ def tearDown(self) -> None: @classmethod def tearDownClass(cls) -> None: - if cls.resue_selenium_web_driver(): + if cls.reuse_selenium_web_driver(): cls.driver.close() cls.driver.quit() @staticmethod - def resue_selenium_web_driver() -> bool: + def reuse_selenium_web_driver() -> bool: """Determine whether the selenium driver shall be shared between all consecutive tests in this class. Checks whether the environment variable SHC_TEST_REUSE_WEB_DRIVER is set.