Update examples

This commit is contained in:
Michael Mintz 2024-03-07 20:45:31 -05:00
parent 49a8f98027
commit 0deefa37d7
7 changed files with 58 additions and 32 deletions

View File

@ -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"')

View File

@ -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()

View File

@ -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"])

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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! ")