Skip to content

Commit

Permalink
selenium: better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
naisanzaa committed Jul 25, 2024
1 parent 683218e commit d9a2fb7
Showing 1 changed file with 44 additions and 33 deletions.
77 changes: 44 additions & 33 deletions automon/integrations/seleniumWrapper/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,45 +323,56 @@ async def autosave_cookies(self) -> bool:

async def delete_all_cookies(self) -> None:
result = self.webdriver.delete_all_cookies()
logger.info(f'{True}')
logger.debug(dict(
delete_all_cookies='done'
))
return result

async def _url_filename(self, url: str):
parsed = await self.urlparse(url)
hostname = parsed.hostname
cookie_file = f'cookies-{hostname}.json'
logger.debug(dict(
_url_filename=cookie_file
cookie_file=cookie_file
))
return cookie_file

async def get_cookie(self, name: str) -> dict:
result = self.webdriver.get_cookie(name=name)
logger.info(f'{result}')
return result
get_cookie = self.webdriver.get_cookie(name=name)
logger.debug(dict(
name=name,
get_cookie=get_cookie,
))
return get_cookie

async def get_cookies(self) -> [dict]:
cookies = self.webdriver.get_cookies()
get_cookies = self.webdriver.get_cookies()
logger.debug(dict(
get_cookies=len(cookies)
get_cookies=len(get_cookies)
))
return cookies
return get_cookies

async def get_cookies_base64(self) -> base64:
cookies = await self.get_cookies()
logger.debug(f'{True}')
return base64.b64encode(
json.dumps(cookies).encode()
async def get_cookies_base64(self) -> str:
get_cookies_base64 = await self.get_cookies()
get_cookies_base64 = base64.b64encode(
json.dumps(get_cookies_base64).encode()
).decode()

logger.debug(dict(
get_cookies_base64=get_cookies_base64
))
return get_cookies_base64

async def get_cookies_json(self) -> json.dumps:
cookies = await self.get_cookies()
get_cookies_json = await self.get_cookies()
get_cookies_json = json.dumps(get_cookies_json)

logger.debug(dict(
get_cookies_json=len(cookies)
get_cookies_json=f'{len(get_cookies_json)} B',
))
return json.dumps(cookies)
return get_cookies_json

async def get_cookies_summary(self):
async def get_cookies_summary(self) -> dict:
result = await self.get_cookies()
summary = {}
if result:
Expand All @@ -376,12 +387,12 @@ async def get_cookies_summary(self):
else:
summary[domain] = [cookie]

logger.debug(f'{summary}')
logger.debug(summary)
return summary

async def close(self):
"""close browser"""
logger.info(f'closed')
logger.info(f'closing webdriver')
self.webdriver.close()

@staticmethod
Expand Down Expand Up @@ -418,8 +429,8 @@ async def find_anything(
find all tags
find all matches within meta data
"""
logger.info(dict(
current_url=self.current_url,
logger.debug(dict(
find_anything=self.current_url,
value=value,
by=by,
case_sensitive=case_sensitive,
Expand Down Expand Up @@ -507,8 +518,8 @@ async def find_elements(
by: selenium.webdriver.common.by.By,
**kwargs) -> list:
"""find elements"""
logger.info(dict(
current_url=self.current_url,
logger.debug(dict(
find_elements=self.current_url,
value=value,
by=by,
))
Expand All @@ -521,7 +532,7 @@ async def find_xpath(
**kwargs) -> selenium.webdriver.Chrome.find_element:
"""find xpath"""
logger.info(dict(
current_url=self.current_url,
find_xpath=self.current_url,
value=value,
by=by,
))
Expand All @@ -535,11 +546,11 @@ async def get(self, url: str, **kwargs) -> bool:

try:
if self.webdriver.get(url, **kwargs) is None:
logger.info(str(dict(
url=url,
logger.debug(dict(
get=url,
current_url=self.current_url,
kwargs=kwargs
)))
))

if self.config.cookies_autosave:
await self.autosave_cookies()
Expand All @@ -552,9 +563,9 @@ async def get(self, url: str, **kwargs) -> bool:

return False

async def get_page(self, *args, **kwargs):
async def get_page(self, *args, **kwargs) -> bool:
"""alias to get"""
return self.get(*args, **kwargs)
return await self.get(*args, **kwargs)

async def get_page_source(self) -> str:
"""get page source"""
Expand Down Expand Up @@ -609,14 +620,14 @@ async def get_screenshot_as_png(self, **kwargs):
async def is_running(self) -> bool:
"""browser is running"""
if self.webdriver:
logger.info(f'{True}')
logger.info(f'webdriver is running')
return True
logger.error(f'{False}')
logger.error(f'webdriver is not running')
return False

async def load_cookies_for_current_url(self) -> bool:
filename = await self._url_filename(url=self.url)
logger.info(dict(
logger.debug(dict(
load_cookies_for_current_url=filename,
url=self.url,
))
Expand Down Expand Up @@ -649,7 +660,7 @@ async def quit(self) -> bool:
return False
return True

async def run(self):
async def run(self) -> bool:
"""run browser"""
try:
return await self.config.run()
Expand Down

0 comments on commit d9a2fb7

Please sign in to comment.