update: using global variables in 'selenium' module
This commit is contained in:
@@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
|
|||||||
[project]
|
[project]
|
||||||
name = "common"
|
name = "common"
|
||||||
description = "Reusable code stubs"
|
description = "Reusable code stubs"
|
||||||
version = "0.1.11"
|
version = "0.1.12"
|
||||||
requires-python = ">=3.13"
|
requires-python = ">=3.13"
|
||||||
authors = [
|
authors = [
|
||||||
{ name="BreakerBear", email="breakerbear@autistic.men" },
|
{ name="BreakerBear", email="breakerbear@autistic.men" },
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
from common.utils.timer import Timer
|
from common.utils.timer import Timer
|
||||||
from common.utils.selenium import until, sleep, locate, click
|
from common.utils.selenium import until, sleep, locate, click, setup
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'Timer',
|
'Timer',
|
||||||
@@ -7,4 +7,5 @@ __all__ = [
|
|||||||
'sleep',
|
'sleep',
|
||||||
'locate',
|
'locate',
|
||||||
'click',
|
'click',
|
||||||
|
'setup',
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -9,7 +9,10 @@ from selenium.webdriver.support import expected_conditions as EC
|
|||||||
from selenium.webdriver.support.wait import WebDriverWait
|
from selenium.webdriver.support.wait import WebDriverWait
|
||||||
from selenium.webdriver.remote.webelement import WebElement
|
from selenium.webdriver.remote.webelement import WebElement
|
||||||
|
|
||||||
def until(driver: WebDriver, condition: typing.Callable[[WebDriver], bool], timeout: float, watch=True):
|
driver: WebDriver|None = None
|
||||||
|
attempts, timeout, interval = int(), float(), float()
|
||||||
|
|
||||||
|
def until(condition: typing.Callable[[WebDriver], bool], watch=True):
|
||||||
try:
|
try:
|
||||||
WebDriverWait(driver, timeout).until(condition)
|
WebDriverWait(driver, timeout).until(condition)
|
||||||
except (TimeoutException, StaleElementReferenceException):
|
except (TimeoutException, StaleElementReferenceException):
|
||||||
@@ -23,7 +26,7 @@ def sleep(driver: WebDriver, seconds: float):
|
|||||||
try: WebDriverWait(driver, seconds, seconds).until(lambda _: False)
|
try: WebDriverWait(driver, seconds, seconds).until(lambda _: False)
|
||||||
except: pass
|
except: pass
|
||||||
|
|
||||||
def locate(driver: WebDriver, selector: str, timeout: float, wait=True, condition=None) -> WebElement:
|
def locate(selector: str, wait=True, condition=None) -> WebElement:
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
locator = (By.CSS_SELECTOR, selector)
|
locator = (By.CSS_SELECTOR, selector)
|
||||||
@@ -41,7 +44,7 @@ def locate(driver: WebDriver, selector: str, timeout: float, wait=True, conditio
|
|||||||
except StaleElementReferenceException:
|
except StaleElementReferenceException:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def click(driver: WebDriver, selector: str|WebElement, attempts: int, interval: int, wait=True, condition=None):
|
def click(selector: str|WebElement, wait=True, condition=None):
|
||||||
predicate = condition if condition is not None else EC.element_to_be_clickable
|
predicate = condition if condition is not None else EC.element_to_be_clickable
|
||||||
identity = ''.join(random.choices(string.ascii_uppercase + string.digits, k=8))
|
identity = ''.join(random.choices(string.ascii_uppercase + string.digits, k=8))
|
||||||
element = locate(selector, wait, predicate) if isinstance(selector, str) else selector
|
element = locate(selector, wait, predicate) if isinstance(selector, str) else selector
|
||||||
@@ -65,3 +68,13 @@ def click(driver: WebDriver, selector: str|WebElement, attempts: int, interval:
|
|||||||
break
|
break
|
||||||
except TimeoutException: continue
|
except TimeoutException: continue
|
||||||
except: break
|
except: break
|
||||||
|
|
||||||
|
def setup(a: WebDriver, b: int, c: float, d: float):
|
||||||
|
global driver, attempts, timeout, interval
|
||||||
|
driver = a
|
||||||
|
attempts = b
|
||||||
|
timeout = c
|
||||||
|
interval = d
|
||||||
|
|
||||||
|
from common.jsonrpc2.server import connection
|
||||||
|
until(lambda _: connection is not None, watch=False)
|
||||||
|
|||||||
Reference in New Issue
Block a user