From 0deefa37d79c684a76b62de31756b7b0262835d6 Mon Sep 17 00:00:00 2001 From: Michael Mintz Date: Thu, 7 Mar 2024 20:45:31 -0500 Subject: [PATCH] Update examples --- examples/presenter/multi_uc.py | 13 +++++++------ examples/presenter/uc_presentation.py | 17 +++++++---------- examples/raw_call.py | 5 +++-- examples/raw_multi_drivers.py | 27 +++++++++++++++++++++++++++ examples/raw_turnstile.py | 2 +- examples/raw_uc_mode.py | 14 +++++++------- examples/verify_undetected.py | 12 ++++++------ 7 files changed, 58 insertions(+), 32 deletions(-) create mode 100644 examples/raw_multi_drivers.py diff --git a/examples/presenter/multi_uc.py b/examples/presenter/multi_uc.py index b1e64a82..de7e1853 100644 --- a/examples/presenter/multi_uc.py +++ b/examples/presenter/multi_uc.py @@ -7,17 +7,18 @@ BaseCase.main(__name__, __file__, "--uc", "-n3") @pytest.mark.parametrize("", [[]] * 3) def test_multi_threaded(sb): - sb.driver.uc_open_with_reconnect("https://top.gg/", 5) + url = "https://gitlab.com/users/sign_in" + sb.driver.uc_open_with_reconnect(url, 3) sb.set_window_rect(randint(0, 755), randint(38, 403), 700, 500) try: - sb.assert_text("Discord Bots", "h1", timeout=2) - sb.post_message("Selenium wasn't detected!", duration=4) + sb.assert_text("Username", '[for="user_login"]', timeout=3) + sb.post_message("SeleniumBase wasn't detected", duration=4) sb._print("\n Success! Website did not detect Selenium! ") except Exception: - sb.driver.uc_open_with_reconnect("https://top.gg/", 5) + sb.driver.uc_open_with_reconnect(url, 3) try: - sb.assert_text("Discord Bots", "h1", timeout=2) - sb.post_message("Selenium wasn't detected!", duration=4) + sb.assert_text("Username", '[for="user_login"]', timeout=3) + sb.post_message("SeleniumBase wasn't detected", duration=4) sb._print("\n Success! Website did not detect Selenium! ") except Exception: sb.fail('Selenium was detected! Try using: "pytest --uc"') diff --git a/examples/presenter/uc_presentation.py b/examples/presenter/uc_presentation.py index cadf761f..ba6247cd 100644 --- a/examples/presenter/uc_presentation.py +++ b/examples/presenter/uc_presentation.py @@ -28,19 +28,16 @@ class UCPresentationClass(BaseCase): self.begin_presentation(filename="uc_presentation.html") self.get_new_driver(undetectable=True) + url = "https://gitlab.com/users/sign_in" try: - self.driver.uc_open_with_reconnect( - "https://top.gg/", reconnect_time=4 - ) + self.driver.uc_open_with_reconnect(url, reconnect_time=3) try: - self.assert_text("Discord Bots", "h1", timeout=3) - self.post_message("Selenium wasn't detected!", duration=4) + self.assert_text("Username", '[for="user_login"]', timeout=3) + self.post_message("SeleniumBase wasn't detected", duration=4) except Exception: - self.driver.uc_open_with_reconnect( - "https://top.gg/", reconnect_time=5 - ) - self.assert_text("Discord Bots", "h1", timeout=2) - self.post_message("Selenium wasn't detected!", duration=4) + self.driver.uc_open_with_reconnect(url, reconnect_time=4) + self.assert_text("Username", '[for="user_login"]', timeout=3) + self.post_message("SeleniumBase wasn't detected", duration=4) finally: self.quit_extra_driver() diff --git a/examples/raw_call.py b/examples/raw_call.py index 613162ec..8d5092be 100644 --- a/examples/raw_call.py +++ b/examples/raw_call.py @@ -4,5 +4,6 @@ Two examples: pytest.main() and subprocess.call().""" import pytest import subprocess -pytest.main(["test_coffee_cart.py", "--chrome", "-v"]) -subprocess.call(["pytest", "test_mfa_login.py", "--chrome", "-v"]) +if __name__ == "__main__": + pytest.main(["test_coffee_cart.py", "--chrome", "-v"]) + subprocess.call(["pytest", "test_mfa_login.py", "--chrome", "-v"]) diff --git a/examples/raw_multi_drivers.py b/examples/raw_multi_drivers.py new file mode 100644 index 00000000..485bb80a --- /dev/null +++ b/examples/raw_multi_drivers.py @@ -0,0 +1,27 @@ +import sys +import threading +from concurrent.futures import ThreadPoolExecutor +from random import randint, seed +from seleniumbase import Driver +sys.argv.append("-n") # Tell SeleniumBase to do thread-locking as needed + + +def launch_driver(url): + seed(len(threading.enumerate())) # Random seed for browser placement + driver = Driver() + try: + driver.set_window_rect(randint(4, 720), randint(8, 410), 700, 500) + driver.get(url=url) + if driver.is_element_visible("h1"): + driver.highlight("h1", loops=9) + else: + driver.sleep(2.2) + finally: + driver.quit() + + +if __name__ == "__main__": + urls = ['https://seleniumbase.io/demo_page' for i in range(4)] + with ThreadPoolExecutor(max_workers=len(urls)) as executor: + for url in urls: + executor.submit(launch_driver, url) diff --git a/examples/raw_turnstile.py b/examples/raw_turnstile.py index 63a0a778..bae7cd69 100644 --- a/examples/raw_turnstile.py +++ b/examples/raw_turnstile.py @@ -21,4 +21,4 @@ with SB(uc=True, test=True) as sb: open_the_turnstile_page(sb) click_turnstile_and_verify(sb) sb.set_messenger_theme(location="top_left") - sb.post_message("Selenium wasn't detected!", duration=3) + sb.post_message("SeleniumBase wasn't detected", duration=3) diff --git a/examples/raw_uc_mode.py b/examples/raw_uc_mode.py index f69379ec..13e050bc 100644 --- a/examples/raw_uc_mode.py +++ b/examples/raw_uc_mode.py @@ -2,10 +2,10 @@ from seleniumbase import SB with SB(uc=True, test=True) as sb: - sb.driver.uc_open_with_reconnect("https://top.gg/", 5) - if not sb.is_text_visible("Discord Bots", "h1"): - sb.driver.uc_open_with_reconnect("https://top.gg/", 5) - sb.assert_text("Discord Bots", "h1", timeout=3) - sb.highlight("h1", loops=3) - sb.set_messenger_theme(location="top_center") - sb.post_message("Selenium wasn't detected!", duration=3) + url = "https://gitlab.com/users/sign_in" + sb.driver.uc_open_with_reconnect(url, 3) + if not sb.is_text_visible("Username", '[for="user_login"]'): + sb.driver.uc_open_with_reconnect(url, 4) + sb.assert_text("Username", '[for="user_login"]', timeout=3) + sb.highlight('label[for="user_login"]', loops=3) + sb.post_message("SeleniumBase wasn't detected", duration=4) diff --git a/examples/verify_undetected.py b/examples/verify_undetected.py index 53aa9ab8..00c02f75 100644 --- a/examples/verify_undetected.py +++ b/examples/verify_undetected.py @@ -7,13 +7,13 @@ BaseCase.main(__name__, __file__, "--uc", "-s") class UndetectedTest(BaseCase): def test_browser_is_undetected(self): + url = "https://gitlab.com/users/sign_in" if not self.undetectable: self.get_new_driver(undetectable=True) - self.driver.uc_open_with_reconnect("https://top.gg/", 5) - if not self.is_text_visible("Discord Bots", "h1"): + self.driver.uc_open_with_reconnect(url, 3) + if not self.is_text_visible("Username", '[for="user_login"]'): self.get_new_driver(undetectable=True) - self.driver.uc_open_with_reconnect("https://top.gg/", 5) - self.assert_text("Discord Bots", "h1", timeout=3) - self.set_messenger_theme(location="top_center") - self.post_message("Selenium wasn't detected!", duration=2.8) + self.driver.uc_open_with_reconnect(url, 4) + self.assert_text("Username", '[for="user_login"]', timeout=3) + self.post_message("SeleniumBase wasn't detected", duration=4) self._print("\n Success! Website did not detect Selenium! ")