From 305286b8b20b37a99c35b47b365c38c8500502fe Mon Sep 17 00:00:00 2001 From: naisanzaa Date: Mon, 7 Oct 2024 23:13:03 +0800 Subject: [PATCH] selenium: raise exception when browser does not start --- .../integrations/seleniumWrapper/browser.py | 2 +- .../seleniumWrapper/tests/test_browser.py | 39 ++++++++++--------- .../tests/test_browser_cookies_autosave.py | 24 ++++++------ .../tests/test_browser_headless.py | 17 ++++---- .../tests/test_browser_useragent.py | 11 ++++-- .../seleniumWrapper/tests/test_logs.py | 21 +++++----- .../seleniumWrapper/tests/test_new_browser.py | 13 +++++-- 7 files changed, 72 insertions(+), 55 deletions(-) diff --git a/automon/integrations/seleniumWrapper/browser.py b/automon/integrations/seleniumWrapper/browser.py index 62ba8d0f..8253deb2 100644 --- a/automon/integrations/seleniumWrapper/browser.py +++ b/automon/integrations/seleniumWrapper/browser.py @@ -803,7 +803,7 @@ def run(self) -> bool: return run except Exception as exception: logger.error(f'webdriver :: run :: failed :: {exception=}') - return False + raise Exception(f'webdriver :: run :: failed :: {exception=}') def save_cookies_for_current_url(self) -> bool: """save cookies for current url""" diff --git a/automon/integrations/seleniumWrapper/tests/test_browser.py b/automon/integrations/seleniumWrapper/tests/test_browser.py index 7d5956d5..b2b7c478 100644 --- a/automon/integrations/seleniumWrapper/tests/test_browser.py +++ b/automon/integrations/seleniumWrapper/tests/test_browser.py @@ -1,6 +1,5 @@ import unittest - from automon.integrations.seleniumWrapper import SeleniumBrowser, ChromeWrapper browser = SeleniumBrowser() @@ -9,29 +8,33 @@ class SeleniumClientTest(unittest.TestCase): - if browser.run(): + try: + if browser.run(): - def test_fake_page(self): - self.assertFalse(browser.get('http://555.555.555.555')) + def test_fake_page(self): + self.assertFalse(browser.get('http://555.555.555.555')) - def test_real_page(self): - if browser.get('http://1.1.1.1'): - self.assertTrue(True) + def test_real_page(self): + if browser.get('http://1.1.1.1'): + self.assertTrue(True) - def test_screenshot_png(self): - if browser.get('http://google.com'): - self.assertTrue(browser.get_screenshot_as_png()) + def test_screenshot_png(self): + if browser.get('http://google.com'): + self.assertTrue(browser.get_screenshot_as_png()) - def test_screenshot_base64(self): - if browser.get('http://yahoo.com'): - self.assertTrue(browser.get_screenshot_as_base64()) + def test_screenshot_base64(self): + if browser.get('http://yahoo.com'): + self.assertTrue(browser.get_screenshot_as_base64()) - def test_screenshot_file(self): - if browser.get('http://bing.com'): - self.assertTrue(browser.save_screenshot()) - self.assertTrue(browser.save_screenshot(folder='./')) + def test_screenshot_file(self): + if browser.get('http://bing.com'): + self.assertTrue(browser.save_screenshot()) + self.assertTrue(browser.save_screenshot(folder='./')) - browser.quit() + browser.quit() + + except: + pass if __name__ == '__main__': diff --git a/automon/integrations/seleniumWrapper/tests/test_browser_cookies_autosave.py b/automon/integrations/seleniumWrapper/tests/test_browser_cookies_autosave.py index 63a82614..b9130a00 100644 --- a/automon/integrations/seleniumWrapper/tests/test_browser_cookies_autosave.py +++ b/automon/integrations/seleniumWrapper/tests/test_browser_cookies_autosave.py @@ -1,24 +1,24 @@ import unittest - from automon.integrations.seleniumWrapper import SeleniumBrowser, ChromeWrapper +browser = SeleniumBrowser() +browser.config.webdriver_wrapper = ChromeWrapper() +browser.config.webdriver_wrapper.enable_defaults().enable_headless() -class Test(unittest.TestCase): - browser = SeleniumBrowser() - browser.config.webdriver_wrapper = ChromeWrapper() - browser.config.webdriver_wrapper.enable_defaults().enable_headless() - # if browser.run(): - browser.run() +class Test(unittest.TestCase): + try: + if browser.run(): - def test_autosave(self): - if self.browser.run(): + def test_autosave(self): + if browser.get('http://bing.com'): + self.assertTrue(browser.autosave_cookies()) - if self.browser.get('http://bing.com'): - self.assertTrue(self.browser.autosave_cookies()) + browser.quit() - self.browser.quit() + except: + pass if __name__ == '__main__': diff --git a/automon/integrations/seleniumWrapper/tests/test_browser_headless.py b/automon/integrations/seleniumWrapper/tests/test_browser_headless.py index 6e2bf057..19867a01 100644 --- a/automon/integrations/seleniumWrapper/tests/test_browser_headless.py +++ b/automon/integrations/seleniumWrapper/tests/test_browser_headless.py @@ -1,6 +1,5 @@ import unittest - from automon.integrations.seleniumWrapper import SeleniumBrowser, ChromeWrapper browser = SeleniumBrowser() @@ -9,18 +8,22 @@ class SeleniumClientTest(unittest.TestCase): - if browser.run(): + try: + if browser.run(): - def test(self): + def test(self): - if browser.get('http://1.1.1.1'): + if browser.get('http://1.1.1.1'): - if browser.check_page_load_finished(): - self.assertTrue(browser.save_screenshot()) - self.assertTrue(browser.save_screenshot(folder='./')) + if browser.check_page_load_finished(): + self.assertTrue(browser.save_screenshot()) + self.assertTrue(browser.save_screenshot(folder='./')) browser.quit() + except: + pass + if __name__ == '__main__': unittest.main() diff --git a/automon/integrations/seleniumWrapper/tests/test_browser_useragent.py b/automon/integrations/seleniumWrapper/tests/test_browser_useragent.py index 17145308..df4c1b3b 100644 --- a/automon/integrations/seleniumWrapper/tests/test_browser_useragent.py +++ b/automon/integrations/seleniumWrapper/tests/test_browser_useragent.py @@ -1,6 +1,5 @@ import unittest - from automon.integrations.seleniumWrapper import SeleniumBrowser, ChromeWrapper browser = SeleniumBrowser() @@ -13,10 +12,14 @@ class SeleniumClientTest(unittest.TestCase): - if browser.run(): - def test_user_agent(self): - self.assertEqual(browser.user_agent, agent) + try: + if browser.run(): + def test_user_agent(self): + self.assertEqual(browser.user_agent, agent) + browser.quit() + except: + pass if __name__ == '__main__': diff --git a/automon/integrations/seleniumWrapper/tests/test_logs.py b/automon/integrations/seleniumWrapper/tests/test_logs.py index f516f73b..fc372e99 100644 --- a/automon/integrations/seleniumWrapper/tests/test_logs.py +++ b/automon/integrations/seleniumWrapper/tests/test_logs.py @@ -1,6 +1,5 @@ import unittest - from automon.integrations.seleniumWrapper import SeleniumBrowser, ChromeWrapper browser = SeleniumBrowser() @@ -9,20 +8,24 @@ class SeleniumClientTest(unittest.TestCase): - if browser.run(): + try: + if browser.run(): - def test_logs(self): + def test_logs(self): - if browser.get('http://binance.com'): - logs = browser.get_log_performance() - logs = browser.get_log_browser() - logs = browser.get_log_driver() - logs = browser.get_logs() + if browser.get('http://binance.com'): + logs = browser.get_log_performance() + logs = browser.get_log_browser() + logs = browser.get_log_driver() + logs = browser.get_logs() - pass + pass browser.quit() + except: + pass + if __name__ == '__main__': unittest.main() diff --git a/automon/integrations/seleniumWrapper/tests/test_new_browser.py b/automon/integrations/seleniumWrapper/tests/test_new_browser.py index a8c88906..f8dda6b2 100644 --- a/automon/integrations/seleniumWrapper/tests/test_new_browser.py +++ b/automon/integrations/seleniumWrapper/tests/test_new_browser.py @@ -1,6 +1,5 @@ import unittest - from automon.integrations.seleniumWrapper import SeleniumBrowser, ChromeWrapper browser = SeleniumBrowser() @@ -9,9 +8,15 @@ class SeleniumClientTest(unittest.TestCase): - if browser.run(): - def test(self): - self.assertTrue(browser.quit()) + try: + if browser.run(): + def test(self): + self.assertTrue(browser.quit()) + + browser.quit() + + except: + pass if __name__ == '__main__':