Skip to content

Commit

Permalink
selenium: add cookies from env var
Browse files Browse the repository at this point in the history
  • Loading branch information
naisanzaa committed Oct 29, 2023
1 parent cfbc341 commit 29323ce
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class InstagramClientTest(unittest.TestCase):

if c.is_running():
c.browser.get(c.urls.domain)
c.browser.add_cookie_from_file()
c.browser.add_cookie_from_base64()
c.browser.refresh()
if c.is_authenticated():
def test_authenticate(self):
Expand Down
63 changes: 54 additions & 9 deletions automon/integrations/seleniumWrapper/browser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import json
import base64
import datetime
import tempfile
import functools
Expand Down Expand Up @@ -195,24 +196,56 @@ def action_type(self, key: str or Keys, secret: bool = True):
)))
return False

def add_cookie(self, cookie_dict: dict):
def add_cookie(self, cookie_dict: dict) -> bool:
result = self.webdriver.add_cookie(cookie_dict=cookie_dict)
log.info(f'{result}')
return result

def add_cookie_from_file(self, file: str = 'cookies.txt') -> [dict]:
if result is None:
log.debug(f'{True}')
return True

log.error(f'{False}')
return False

def add_cookies(self, cookies_list: list):
for cookie in cookies_list:
self.add_cookie(cookie_dict=cookie)
self.get_cookies_summary()

log.debug(f'{True}')
return True

def add_cookie_from_file(self, file: str = None) -> bool:
if not file and self.config.cookies_file:
file = self.config.cookies_file
else:
file = 'cookies.txt'

if os.path.exists(file):
log.debug(f'{file}')
with open(file, 'r') as cookies_file:
for cookie in json.loads(cookies_file.read()):
self.add_cookie(cookie_dict=cookie)
self.get_cookies()
self.add_cookies(
json.loads(cookies_file.read())
)
return True
else:
log.error(f'{file}')

return False

def add_cookie_from_base64(self, base64_str: str = None):
if not base64_str and self.config.cookies_base64:
base64_str = self.config.cookies_base64

if base64_str:
self.add_cookies(
json.loads(base64.b64decode(base64_str))
)
log.debug(f'{True}')
return True

log.error(f'{False}')
return False

def delete_all_cookies(self) -> None:
result = self.webdriver.delete_all_cookies()
log.info(f'{True}')
Expand All @@ -225,6 +258,18 @@ def get_cookie(self, name: str) -> dict:

def get_cookies(self) -> [dict]:
result = self.webdriver.get_cookies()
log.debug(f'{True}')
return result

def get_cookies_base64(self) -> base64:
result = self.get_cookies()
log.debug(f'{True}')
return base64.b64encode(
json.dumps(result).encode()
).decode()

def get_cookies_summary(self):
result = self.get_cookies()
summary = {}
if result:
for cookie in result:
Expand All @@ -243,8 +288,8 @@ def get_cookies(self) -> [dict]:
else:
summary[domain] = [name]

log.info(f'{summary}')
return result
log.debug(f'{summary}')
return summary

@_is_running
def close(self):
Expand Down
14 changes: 14 additions & 0 deletions automon/integrations/seleniumWrapper/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import selenium.webdriver

from automon.log import logger
from automon.helpers.osWrapper import environ

from .config_webdriver_chrome import ConfigChrome

Expand All @@ -13,6 +14,9 @@ def __init__(self):
self._webdriver = None
self._webdriver_wrapper = None

self._cookies_base64 = environ('SELENIUM_COOKIES_BASE64')
self._cookies_file = environ('SELENIUM_COOKIES_FILE')

self._chrome = ConfigChrome()
self._edge = NotImplemented
self._firefox = NotImplemented
Expand All @@ -36,6 +40,16 @@ def window_size(self):
if self.webdriver_wrapper:
return self.webdriver_wrapper.window_size

@property
def cookies_base64(self):
log.debug(f'{len(self._cookies_base64)}')
return self._cookies_base64

@property
def cookies_file(self):
log.info(f'{self._cookies_file}')
return self._cookies_file

def Chrome(self):
"""selenium Chrome webdriver
Expand Down
2 changes: 2 additions & 0 deletions env-example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ TWINE_PASSWORD=
# Selenium
SELENIUM_CHROMEDRIVER_PATH=
SELENIUM_OPT=
SELENIUM_COOKIES_BASE64=
SELENIUM_COOKIES_FILE=

# Sentry.io
SENTRY_DSN=
Expand Down

0 comments on commit 29323ce

Please sign in to comment.