Skip to content

Commit

Permalink
Merge pull request #2187 from seleniumbase/various-updates-and-fixes
Browse files Browse the repository at this point in the history
Various updates and fixes
  • Loading branch information
mdmintz authored Oct 13, 2023
2 parents cb1564d + 0567c4d commit b8f3e97
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 31 deletions.
7 changes: 3 additions & 4 deletions examples/hack_the_planet.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,9 @@ def test_all_your_base_are_belong_to_us(self):
self.set_text_content('a[href*="jira/enterprise"]', abtu)
self.set_text_content('a[href="/software/jira/features"]', "")
self.set_text_content("h1", aybabtu)
self.highlight("ul.imkt-navbar__link-list", loops=2, scroll=False)
self.highlight('a[href*="jira/pricing"]', loops=3, scroll=False)
self.highlight('a[href*="jira/enterprise"]', loops=3, scroll=False)
self.highlight("h1", loops=6, scroll=False)
self.highlight('a[href*="jira/pricing"]', loops=5, scroll=False)
self.highlight('a[href*="jira/enterprise"]', loops=6, scroll=False)
self.highlight("h1", loops=8, scroll=False)

self.open("https://status.iboss.com/ibcloud/app/cloudStatus.html")
self.wait_for_element_clickable('div[translate*="cloudStatus"]')
Expand Down
2 changes: 1 addition & 1 deletion examples/presenter/edge_presentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ def test_presentation(self):
self.assert_text("Skype for Mobile", "h2")
self.highlight("h2")
self.highlight("#get-skype-0")
self.highlight_click('[data-bi-name="arrow-dropdown-mobile"]')
self.highlight_click("span[data-dropdown-icon]")
self.highlight("#get-skype-0_android-download")
self.highlight('[data-bi-id*="ios"]')
self.quit_extra_driver()
Expand Down
17 changes: 17 additions & 0 deletions examples/raw_mobile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from seleniumbase import Driver

driver = Driver(mobile=True)
try:
driver.open("https://www.skype.com/en/get-skype/")
driver.assert_element('[aria-label="Microsoft"]')
driver.assert_text("Download Skype", "h1")
driver.highlight("div.appBannerContent")
driver.highlight("h1")
driver.assert_text("Skype for Mobile", "h2")
driver.highlight("h2")
driver.highlight("#get-skype-0")
driver.highlight_click("span[data-dropdown-icon]")
driver.highlight("#get-skype-0_android-download")
driver.highlight('[data-bi-id*="ios"]')
finally:
driver.quit()
6 changes: 3 additions & 3 deletions examples/raw_uc_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

with SB(uc=True) as sb:
sb.driver.get("https://nowsecure.nl/#relax")
sb.sleep(2)
sb.sleep(1)
if not sb.is_text_visible("OH YEAH, you passed!", "h1"):
sb.get_new_driver(undetectable=True)
sb.driver.get("https://nowsecure.nl/#relax")
sb.sleep(2)
sb.sleep(1)
if not sb.is_text_visible("OH YEAH, you passed!", "h1"):
if sb.is_element_visible('iframe[src*="challenge"]'):
with sb.frame_switch('iframe[src*="challenge"]'):
sb.click("span.mark")
sb.sleep(4)
sb.sleep(2)
sb.activate_demo_mode()
sb.assert_text("OH YEAH, you passed!", "h1", timeout=3)
10 changes: 3 additions & 7 deletions examples/test_skype_site.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
"""This is a mobile device test for Chromium-based browsers (such as MS Edge)
Usage: pytest test_skype_site.py --mobile --edge
Default mobile settings for User Agent and Device Metrics if not specified:
User Agent: --agent="Mozilla/5.0 (Linux; Android 11; Pixel 4 XL)"
CSS Width, CSS Height, Pixel-Ratio: --metrics="360,640,2"
"""Mobile device test for Chromium-based browsers (such as Edge)
Example: "pytest test_skype_site.py --mobile --edge"
"""
from seleniumbase import BaseCase

Expand All @@ -27,6 +23,6 @@ def test_skype_mobile_site(self):
self.assert_text("Skype for Mobile", "h2")
self.highlight("h2")
self.highlight("#get-skype-0")
self.highlight_click('[data-bi-name="arrow-dropdown-mobile"]')
self.highlight_click("span[data-dropdown-icon]")
self.highlight("#get-skype-0_android-download")
self.highlight('[data-bi-id*="ios"]')
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ mccabe==0.7.0
pyflakes==2.5.0;python_version<"3.9"
pyflakes==3.1.0;python_version>="3.9"
pycodestyle==2.9.1;python_version<"3.9"
pycodestyle==2.11.0;python_version>="3.9"
pycodestyle==2.11.1;python_version>="3.9"
2 changes: 1 addition & 1 deletion seleniumbase/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# seleniumbase package
__version__ = "4.20.1"
__version__ = "4.20.2"
19 changes: 12 additions & 7 deletions seleniumbase/core/browser_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ def extend_driver(driver):
driver.get_origin = DM.get_origin
driver.get_user_agent = DM.get_user_agent
driver.highlight = DM.highlight
driver.highlight_click = DM.highlight_click
driver.sleep = time.sleep
driver.get_page_source = DM.get_page_source
driver.get_title = DM.get_title
Expand Down Expand Up @@ -412,12 +413,14 @@ def uc_open_with_tab(driver, url):
return None


def uc_open_with_reconnect(driver, url):
"""Open a url, then reconnect with UC before switching to the window."""
def uc_open_with_reconnect(driver, url, reconnect_time=None):
"""Open a url, disconnect chromedriver, wait, and reconnect."""
if not reconnect_time:
reconnect_time = constants.UC.RECONNECT_TIME
if (url.startswith("http:") or url.startswith("https:")):
driver.execute_script('window.open("%s","_blank");' % url)
driver.reconnect(2.65)
driver.close()
driver.reconnect(reconnect_time)
driver.switch_to.window(driver.window_handles[-1])
else:
driver.default_get(url) # The original one
Expand Down Expand Up @@ -1314,9 +1317,9 @@ def get_driver(
binary_location = None
if (uc_cdp_events or uc_subprocess) and not undetectable:
undetectable = True
if is_using_uc(undetectable, browser_name) and mobile_emulator:
if not user_agent:
user_agent = constants.Mobile.AGENT
if mobile_emulator and not user_agent:
# Use a Pixel user agent by default if not specified
user_agent = constants.Mobile.AGENT
if page_load_strategy and page_load_strategy.lower() == "none":
settings.PAGE_LOAD_STRATEGY = "none"
proxy_auth = False
Expand Down Expand Up @@ -3478,7 +3481,9 @@ def get_local_driver(
lambda url: uc_open_with_tab(driver, url)
)
driver.uc_open_with_reconnect = (
lambda url: uc_open_with_reconnect(driver, url)
lambda *args, **kwargs: uc_open_with_reconnect(
driver, *args, **kwargs
)
)
if mobile_emulator:
uc_metrics = {}
Expand Down
14 changes: 14 additions & 0 deletions seleniumbase/core/sb_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,22 @@ def get_user_agent(self, *args, **kwargs):
return js_utils.get_user_agent(self.driver, *args, **kwargs)

def highlight(self, *args, **kwargs):
w_args = kwargs
if "loops" in w_args:
w_args.pop("loops")
element = page_actions.wait_for_element(self.driver, *args, **w_args)
browser = self.driver.capabilities["browserName"].lower()
js_utils.slow_scroll_to_element(self.driver, element, browser)
if "timeout" in kwargs:
kwargs.pop("timeout")
js_utils.highlight(self.driver, *args, **kwargs)

def highlight_click(self, *args, **kwargs):
self.highlight(*args, **kwargs)
if "loops" in kwargs:
kwargs.pop("loops")
page_actions.click(self.driver, *args, **kwargs)

def set_wire_proxy(self, string):
"""Set a proxy server for selenium-wire mode ("--wire")
Examples: (ONLY avilable if using selenium-wire mode!)
Expand Down
12 changes: 11 additions & 1 deletion seleniumbase/fixtures/base_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,23 @@ def test_example(self):
Now, if they accidentally type "python", the tests will still run.
Eg. "python my_test.py" instead of "pytest my_test.py"."""
if name == "__main__": # Test called with "python"
import subprocess
from pytest import main as pytest_main
all_args = []
for arg in args:
all_args.append(arg)
for arg in sys.argv[1:]:
all_args.append(arg)
pytest_main([file, "-s", *all_args])
multi = False
for arg in all_args:
if arg.startswith("-n") or arg.startswith("--numprocesses"):
multi = True
if multi:
subprocess.call(
[sys.executable, "-m", "pytest", file, "-s", *all_args]
)
else:
pytest_main([file, "-s", *all_args])

def open(self, url):
"""Navigates the current browser window to the specified page."""
Expand Down
4 changes: 4 additions & 0 deletions seleniumbase/fixtures/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,10 @@ class Mobile:
)


class UC:
RECONNECT_TIME = 2.15 # Seconds


class ValidBrowsers:
valid_browsers = [
"chrome",
Expand Down
7 changes: 2 additions & 5 deletions seleniumbase/undetected/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def __init__(
options.binary_location = (
browser_executable_path or find_chrome_executable()
)
self._delay = 2.05
self._delay = constants.UC.RECONNECT_TIME
self.user_data_dir = user_data_dir
self.keep_user_data_dir = keep_user_data_dir
if suppress_welcome:
Expand Down Expand Up @@ -491,10 +491,7 @@ def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.service.stop()
time.sleep(self._delay)
self.service.start()
self.start_session()
self.reconnect(timeout=self._delay)

def __hash__(self):
return hash(self.options.debugger_address)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@
'pyflakes==2.5.0;python_version<"3.9"',
'pyflakes==3.1.0;python_version>="3.9"',
'pycodestyle==2.9.1;python_version<"3.9"',
'pycodestyle==2.11.0;python_version>="3.9"',
'pycodestyle==2.11.1;python_version>="3.9"',
],
# pip install -e .[ipdb]
# (Not needed for debugging anymore. SeleniumBase now includes "pdbp".)
Expand Down

0 comments on commit b8f3e97

Please sign in to comment.