Skip to content

Commit

Permalink
selenium: fix current_url, update get, remove is_running
Browse files Browse the repository at this point in the history
  • Loading branch information
naisanzaa committed Nov 7, 2023
1 parent ab7e85b commit 21585c4
Showing 1 changed file with 30 additions and 41 deletions.
71 changes: 30 additions & 41 deletions automon/integrations/seleniumWrapper/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ def __init__(self, config: SeleniumConfig = None):
self.request = None

def __repr__(self):
if self.webdriver:
try:
return str(dict(
webdriver=self.webdriver.name or None,
request_status=self.request_status,
current_url=self.webdriver.current_url,
window_size=self.window_size,
))
except Exception as error:
pass

return f'{__class__}'

Expand All @@ -57,6 +58,14 @@ def by(self) -> By:
def config(self):
return self._config

@property
def _current_url(self):
try:
self.webdriver.current_url
return self.webdriver.current_url
except Exception as error:
return

@property
def webdriver(self):
return self.config.webdriver
Expand Down Expand Up @@ -99,11 +108,11 @@ def request_status(self):
def url(self):
if self.webdriver:
log.info(str(dict(
current_url=self.webdriver.current_url
current_url=self._current_url
)))
if self.webdriver.current_url == 'data:,':
if self._current_url == 'data:,':
return ''
return self.webdriver.current_url
return self._current_url

log.info(str(dict(
current_url=None
Expand All @@ -114,20 +123,11 @@ def url(self):
def window_size(self):
return self.config.set_webdriver().window_size

def _is_running(func) -> functools.wraps:
@functools.wraps(func)
def wrapped(self, *args, **kwargs):
if self.is_running():
return func(self, *args, **kwargs)
return False

return wrapped

def _screenshot_name(self, prefix=None):
"""Generate a unique filename"""

title = self.webdriver.title
url = self.webdriver.current_url
url = self._current_url
hostname = urlparse(url).hostname

hostname_ = Sanitation.ascii_numeric_only(hostname)
Expand All @@ -140,7 +140,6 @@ def _screenshot_name(self, prefix=None):

return f'{hostname_}_{title_}_{timestamp}.png'

@_is_running
def action_click(self, xpath: str, note: str = None) -> str or False:
"""perform mouse command"""
try:
Expand Down Expand Up @@ -168,7 +167,6 @@ def action_click(self, xpath: str, note: str = None) -> str or False:
)))
return False

@_is_running
def action_type(self, key: str or Keys, secret: bool = True):
"""perform keyboard command"""
try:
Expand Down Expand Up @@ -291,7 +289,6 @@ def get_cookies_summary(self):
log.debug(f'{summary}')
return summary

@_is_running
def close(self):
"""close browser"""
log.info(f'closed')
Expand All @@ -308,7 +305,6 @@ def error_parsing(error) -> tuple:

return message, session, stacktrace

@_is_running
def find_element(
self,
value: str,
Expand All @@ -323,7 +319,6 @@ def find_element(
)))
return element

@_is_running
def find_xpath(self, value: str, by: By = By.XPATH, **kwargs):
"""find xpath"""
xpath = self.find_element(value=value, by=by, **kwargs)
Expand All @@ -334,19 +329,18 @@ def find_xpath(self, value: str, by: By = By.XPATH, **kwargs):
)))
return xpath

@_is_running
def get(self, url: str, **kwargs) -> bool:
"""get url"""
try:
self.webdriver.get(url, **kwargs)
self.request = RequestsClient(url=url)

log.info(str(dict(
url=url,
current_url=self.webdriver.current_url,
request_status=self.request_status,
kwargs=kwargs
)))
if self.webdriver.get(url, **kwargs) is None:
self.request = RequestsClient(url=url)

log.info(str(dict(
url=url,
current_url=self._current_url,
request_status=self.request_status,
kwargs=kwargs
)))
return True
except Exception as error:
self.request = RequestsClient(url=url)
Expand All @@ -357,17 +351,14 @@ def get(self, url: str, **kwargs) -> bool:

return False

@_is_running
def get_page(self, *args, **kwargs):
"""alias to get"""
return self.get(*args, **kwargs)

@_is_running
def get_page_source(self) -> str:
"""get page source"""
return self.webdriver.page_source

@_is_running
def get_page_source_beautifulsoup(self, markdup: str = None, features: str = 'lxml') -> BeautifulSoup:
"""read page source with beautifulsoup"""
if not markdup:
Expand All @@ -377,7 +368,6 @@ def get_page_source_beautifulsoup(self, markdup: str = None, features: str = 'lx
def get_random_user_agent(self, filter: list or str = None, case_sensitive: bool = False) -> list:
return SeleniumUserAgentBuilder().get_random(filter=filter, case_sensitive=case_sensitive)

@_is_running
def get_screenshot_as_base64(self, **kwargs):
"""screenshot as base64"""
screenshot = self.webdriver.get_screenshot_as_base64(**kwargs)
Expand All @@ -400,15 +390,13 @@ def get_screenshot_as_file(
**kwargs
)

@_is_running
def get_screenshot_as_png(self, **kwargs):
"""screenshot as png"""
screenshot = self.webdriver.get_screenshot_as_png(**kwargs)
log.debug(f'{round(len(screenshot) / 1024)} KB')

return screenshot

@_is_running
def get_user_agent(self):
return self.webdriver.execute_script("return navigator.userAgent")

Expand All @@ -420,7 +408,6 @@ def is_running(self) -> bool:
log.error(f'{False}')
return False

@_is_running
def quit(self) -> bool:
"""gracefully quit browser"""
try:
Expand Down Expand Up @@ -450,7 +437,6 @@ def save_cookies_to_file(self, file: str = 'cookies.txt'):
log.error(f'{file}')
return False

@_is_running
def save_screenshot(
self,
filename: str = None,
Expand Down Expand Up @@ -481,7 +467,6 @@ def save_screenshot(
def set_webdriver(self):
return self.config

@_is_running
def set_window_size(self, width=1920, height=1080, device_type=None) -> bool:
"""set browser resolution"""

Expand Down Expand Up @@ -564,7 +549,7 @@ def wait_for(
value=value,
error=error,
)))
Sleeper.seconds(f'wait for', 0.2)
Sleeper.seconds(__name__, 0.2)

retry += 1

Expand All @@ -577,7 +562,11 @@ def wait_for(
break

if fail_on_error:
raise Exception(f'{value}')
raise Exception(str(dict(
by=by,
url=self.url,
value=value,
)))

return False

Expand Down

0 comments on commit 21585c4

Please sign in to comment.