| 1 |
import base64 |
| 2 |
import json |
| 3 |
import os |
| 4 |
import shutil |
| 5 |
import sys |
| 6 |
import tarfile |
| 7 |
import tempfile |
| 8 |
import time |
| 9 |
import unittest |
| 10 |
|
| 11 |
from selenium import webdriver |
| 12 |
from selenium.webdriver.chrome.service import Service as ChromeService |
| 13 |
from selenium.webdriver.edge.service import Service as EdgeService |
| 14 |
from selenium.webdriver.firefox.service import Service as FirefoxService |
| 15 |
from webdriver_manager.chrome import ChromeDriverManager |
| 16 |
from webdriver_manager.core.utils import ChromeType |
| 17 |
from webdriver_manager.firefox import GeckoDriverManager |
| 18 |
from webdriver_manager.microsoft import EdgeChromiumDriverManager |
| 19 |
from webdriver_manager.opera import OperaDriverManager |
| 20 |
|
| 21 |
from browser_cookie3 import (brave, chrome, chromium, edge, firefox, librewolf, |
| 22 |
load, opera, opera_gx, vivaldi) |
| 23 |
|
| 24 |
from .utils import BrowserName, logger |
| 25 |
from .utils.browser_paths import BinaryLocation |
| 26 |
from .utils.driver_version import get_vivaldi_driver_version |
| 27 |
|
| 28 |
FIREFOX_BASED_PROFILE_DIR_TAR_XZ_B64 = '/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4Cf/AYFdABoeCqdqx8Ww3rP7opUNoguNkPsxJ2/LebvQiBz6BsBQHaW+I1WQtjmf3unq5qmmUUrSZDK1J4u30H9wRGMKCVjK3Fc4k2kZ6V9v2ySBjXGozKv3Fk/Ai8MCLrCO+SZEQvWOKlVVCBs798tiUloPgnBdT3fRm60SZOSq9gz0ac+/B5M3kI2E9sc6zTn1BVUGf3XpssfNTbyq3Htm/XyjGXwsjpxHjVVfoijHC5ldnaE09Ro14TLFRs56FUolOssUzvXnWt0VrFd3dD/oZxVJ7XDS/1lirTYUWMkiPu4lU6icGJWzIVIEh5MA/cBoO7LDHd6ehXlyhbOGPeNpVuk7a0GGOq295Zi/4jFT2JeIm9QOtw/pcRFsP/sn6Y1MS2BBiO9A30qT0zyb+mqho8kxvK2gMnZWJSFczG/9lyWyNc8gDtpYYyGBjfinCYdJlcOAMoa5pS3zS5g4AIvCSKVwEH1Ba3gn1StsUCGMT4Nw9Pom/Wkd3JGSfq30VB+bRRNlch0AAAAA7jsEu81WUn0AAZ0DgFAAABsz6KSxxGf7AgAAAAAEWVo=' |
| 29 |
FIREFOX_BASED_PROFILE_DIR_NAME = '4xutesqi.default-release' |
| 30 |
GO_TO_URLS = ['https://google.com', 'https://facebook.com', |
| 31 |
'https://aka.ms', 'https://github.com'] |
| 32 |
|
| 33 |
|
| 34 |
class Test(unittest.TestCase): |
| 35 |
@classmethod |
| 36 |
def setUpClass(cls) -> None: |
| 37 |
cls.__is_github_actions = not not os.environ.get( |
| 38 |
'GITHUB_ACTIONS', False) |
| 39 |
if cls.__is_github_actions: |
| 40 |
cls.__headless = True |
| 41 |
else: |
| 42 |
cls.__headless = not not os.environ.get('RUN_HEADLESS', False) |
| 43 |
if cls.__headless: |
| 44 |
logger.info('Running headless') |
| 45 |
cls.__binary_location = BinaryLocation( |
| 46 |
raise_not_found=cls.__is_github_actions) |
| 47 |
|
| 48 |
def setUp(self) -> None: |
| 49 |
self.__temp_dir = tempfile.mktemp(prefix='browser_cookie3_test_') |
| 50 |
os.mkdir(self.__temp_dir) |
| 51 |
logger.info('Starting test: %s', self._testMethodName) |
| 52 |
super().setUp() |
| 53 |
|
| 54 |
def tearDown(self) -> None: |
| 55 |
try: |
| 56 |
shutil.rmtree(self.__temp_dir) |
| 57 |
except PermissionError: |
| 58 |
pass |
| 59 |
logger.info('Finished test: %s', self._testMethodName) |
| 60 |
super().tearDown() |
| 61 |
|
| 62 |
def __get_data_dir(self): |
| 63 |
data_dir = os.path.join(self.__temp_dir, self._testMethodName) |
| 64 |
return data_dir |
| 65 |
|
| 66 |
def __is_key_path_okay(self, key_path): |
| 67 |
if not key_path: |
| 68 |
return True |
| 69 |
if sys.platform != 'win32': |
| 70 |
return True |
| 71 |
if not os.path.exists(key_path): |
| 72 |
return False |
| 73 |
with open(key_path, 'r') as f: |
| 74 |
data = json.load(f).get('os_crypt', {}).get('encrypted_key', None) |
| 75 |
return data is not None |
| 76 |
|
| 77 |
def __wait_for_cookies_to_be_detected(self, browser_func, cookies_path, key_path, timeout): |
| 78 |
end_time = time.time() + timeout |
| 79 |
while time.time() < end_time: |
| 80 |
if not self.__is_key_path_okay(key_path): |
| 81 |
time.sleep(1) |
| 82 |
continue |
| 83 |
if len(self.__call_browser_func(browser_func, cookies_path, key_path)) > 0: |
| 84 |
return |
| 85 |
time.sleep(1) |
| 86 |
|
| 87 |
def __setup_firefox_based(self, profile_containing_dir, binary_location): |
| 88 |
if os.path.exists(profile_containing_dir): |
| 89 |
raise Exception( |
| 90 |
f'Profile dir already exists: {profile_containing_dir}') |
| 91 |
os.makedirs(profile_containing_dir) |
| 92 |
|
| 93 |
xz_file = tempfile.mktemp(suffix='.tar.xz') |
| 94 |
with open(xz_file, 'wb') as f: |
| 95 |
f.write(base64.b64decode(FIREFOX_BASED_PROFILE_DIR_TAR_XZ_B64)) |
| 96 |
with tarfile.open(xz_file) as f: |
| 97 |
f.extractall(profile_containing_dir) |
| 98 |
os.remove(xz_file) |
| 99 |
|
| 100 |
profile_dir = os.path.join( |
| 101 |
profile_containing_dir, FIREFOX_BASED_PROFILE_DIR_NAME) |
| 102 |
|
| 103 |
options = webdriver.FirefoxOptions() |
| 104 |
options.binary_location = binary_location |
| 105 |
options.add_argument('-profile') |
| 106 |
options.add_argument(profile_dir) |
| 107 |
|
| 108 |
options.set_preference('privacy.clearOnShutdown.cookies', False) |
| 109 |
|
| 110 |
if self.__headless: |
| 111 |
options.add_argument('--headless') |
| 112 |
options.add_argument('--disable-gpu') |
| 113 |
|
| 114 |
executable_path = os.environ.get( |
| 115 |
'GECKOWEBDRIVER', None) or GeckoDriverManager().install() |
| 116 |
|
| 117 |
self.driver = webdriver.Firefox( |
| 118 |
service=FirefoxService(executable_path), options=options) |
| 119 |
|
| 120 |
@staticmethod |
| 121 |
def __call_browser_func(func, cookies_path, key_path): |
| 122 |
if not key_path: |
| 123 |
return func(cookies_path) |
| 124 |
return func(cookies_path, key_file=key_path) |
| 125 |
|
| 126 |
def __test_browser(self, browser_func, cookies_path=None, key_path=None, max_wait_seconds=45): |
| 127 |
for url in GO_TO_URLS: |
| 128 |
self.driver.get(url) |
| 129 |
self.driver.implicitly_wait(10) |
| 130 |
|
| 131 |
time.sleep(5) |
| 132 |
self.driver.quit() |
| 133 |
time.sleep(5) |
| 134 |
self.__wait_for_cookies_to_be_detected( |
| 135 |
browser_func, cookies_path, key_path, max_wait_seconds) |
| 136 |
total_cookies = len(self.__call_browser_func( |
| 137 |
browser_func, cookies_path, key_path)) |
| 138 |
self.assertGreaterEqual(total_cookies, 0) |
| 139 |
if total_cookies < 1: |
| 140 |
logger.warning( |
| 141 |
'Cookie database was empty after waiting for cookies to be detected') |
| 142 |
|
| 143 |
def __setup_chromium_based(self, chrome_type, binary_location, driver_version=None): |
| 144 |
options = webdriver.ChromeOptions() |
| 145 |
options.binary_location = binary_location |
| 146 |
options.add_argument(f'--user-data-dir={self.__get_data_dir()}') |
| 147 |
if self.__headless: |
| 148 |
options.add_argument('--headless=new') |
| 149 |
options.add_argument('--disable-gpu') |
| 150 |
self.driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager( |
| 151 |
version=driver_version, chrome_type=chrome_type).install()), options=options) |
| 152 |
|
| 153 |
def __test_chromium_based(self, browser_func, max_wait_seconds=45): |
| 154 |
paths = ['Default', 'Cookies'] |
| 155 |
key_path = None |
| 156 |
if sys.platform == 'win32': |
| 157 |
paths.insert(1, 'Network') |
| 158 |
key_path = os.path.join(self.__get_data_dir(), 'Local State') |
| 159 |
|
| 160 |
cookies_path = os.path.join(self.__get_data_dir(), *paths) |
| 161 |
self.__test_browser(browser_func, cookies_path, |
| 162 |
key_path, max_wait_seconds) |
| 163 |
|
| 164 |
def __setup_opera_based(self, binary_location): |
| 165 |
if sys.platform != 'win32': |
| 166 |
return self.__setup_chromium_based(ChromeType.GOOGLE, binary_location) |
| 167 |
self.__opera_service = ChromeService(OperaDriverManager().install()) |
| 168 |
self.__opera_service.start() |
| 169 |
options = webdriver.ChromeOptions() |
| 170 |
options.binary_location = binary_location |
| 171 |
options.add_argument(f'--user-data-dir={self.__get_data_dir()}') |
| 172 |
options.add_experimental_option('w3c', True) |
| 173 |
if self.__headless: |
| 174 |
options.add_argument('--headless=new') |
| 175 |
options.add_argument('--disable-gpu') |
| 176 |
|
| 177 |
self.driver = webdriver.Remote( |
| 178 |
self.__opera_service.service_url, options=options) |
| 179 |
|
| 180 |
def __test_opera_based(self, browser_func): |
| 181 |
cookie_path = ['Cookies'] |
| 182 |
key_path = None |
| 183 |
if sys.platform == 'win32': |
| 184 |
cookie_path.insert(0, 'Network') |
| 185 |
key_path = os.path.join(self.__get_data_dir(), 'Local State') |
| 186 |
try: |
| 187 |
self.__test_browser(browser_func, os.path.join( |
| 188 |
self.__get_data_dir(), *cookie_path), key_path) |
| 189 |
except KeyError as e: |
| 190 |
if 'os_crypt' in str(e) and sys.platform == 'win32': |
| 191 |
logger.warning('os_crypt not in keyfile, skipping test') |
| 192 |
logger.warning( |
| 193 |
'it is a common issue with Opera-based browsers on Windows') |
| 194 |
self.skipTest('os_crypt not in keyfile') |
| 195 |
else: |
| 196 |
raise e |
| 197 |
|
| 198 |
def __setup_edge(self): |
| 199 |
options = webdriver.EdgeOptions() |
| 200 |
options.binary_location = self.__binary_location.get(BrowserName.EDGE) |
| 201 |
options.add_argument(f'--user-data-dir={self.__get_data_dir()}') |
| 202 |
if self.__headless: |
| 203 |
options.add_argument('--headless=new') |
| 204 |
options.add_argument('--disable-gpu') |
| 205 |
|
| 206 |
self.driver = webdriver.Edge( |
| 207 |
service=EdgeService(EdgeChromiumDriverManager().install()), options=options) |
| 208 |
|
| 209 |
def test_edge(self): |
| 210 |
|
| 211 |
self.__setup_edge() |
| 212 |
if not self.__headless: |
| 213 |
|
| 214 |
|
| 215 |
time.sleep(5) |
| 216 |
self.__test_chromium_based(edge) |
| 217 |
|
| 218 |
def test_brave(self): |
| 219 |
self.__setup_chromium_based( |
| 220 |
ChromeType.BRAVE, self.__binary_location.get(BrowserName.BRAVE)) |
| 221 |
self.__test_chromium_based(brave) |
| 222 |
|
| 223 |
def test_chromium(self): |
| 224 |
self.__setup_chromium_based( |
| 225 |
ChromeType.CHROMIUM, self.__binary_location.get(BrowserName.CHROMIUM)) |
| 226 |
self.__test_chromium_based(chromium) |
| 227 |
|
| 228 |
def test_chrome(self): |
| 229 |
self.__setup_chromium_based( |
| 230 |
ChromeType.GOOGLE, self.__binary_location.get(BrowserName.CHROME)) |
| 231 |
self.__test_chromium_based(chrome) |
| 232 |
|
| 233 |
def test_firefox(self): |
| 234 |
profile_containing_dir = os.path.join( |
| 235 |
self.__temp_dir, '.mozilla', 'firefox') |
| 236 |
cookie_path = os.path.join( |
| 237 |
profile_containing_dir, FIREFOX_BASED_PROFILE_DIR_NAME, 'cookies.sqlite') |
| 238 |
binary_location = self.__binary_location.get(BrowserName.FIREFOX) |
| 239 |
|
| 240 |
self.__setup_firefox_based(profile_containing_dir, binary_location) |
| 241 |
self.__test_browser(firefox, cookie_path) |
| 242 |
|
| 243 |
def test_librewolf(self): |
| 244 |
profile_containing_dir = os.path.join(self.__temp_dir, '.librewolf') |
| 245 |
cookie_path = os.path.join( |
| 246 |
profile_containing_dir, FIREFOX_BASED_PROFILE_DIR_NAME, 'cookies.sqlite') |
| 247 |
binary_location = self.__binary_location.get(BrowserName.LIBREWOLF) |
| 248 |
|
| 249 |
self.__setup_firefox_based(profile_containing_dir, binary_location) |
| 250 |
self.__test_browser(librewolf, cookie_path) |
| 251 |
|
| 252 |
def test_opera(self): |
| 253 |
self.__setup_opera_based(self.__binary_location.get(BrowserName.OPERA)) |
| 254 |
self.__test_opera_based(opera) |
| 255 |
|
| 256 |
@unittest.skipIf(sys.platform not in ['win32', 'darwin'], 'Only supported on Windows and macOS') |
| 257 |
def test_opera_gx(self): |
| 258 |
self.__setup_opera_based( |
| 259 |
self.__binary_location.get(BrowserName.OPERA_GX)) |
| 260 |
self.__test_opera_based(opera_gx) |
| 261 |
|
| 262 |
def test_vivaldi(self): |
| 263 |
|
| 264 |
driver_version = get_vivaldi_driver_version( |
| 265 |
self.__binary_location.get(BrowserName.VIVALDI), self.__is_github_actions) |
| 266 |
logger.info(f'Using chromedriver version {driver_version} for Vivaldi') |
| 267 |
self.__setup_chromium_based(ChromeType.GOOGLE, self.__binary_location.get( |
| 268 |
BrowserName.VIVALDI), driver_version) |
| 269 |
self.__test_chromium_based(vivaldi) |
| 270 |
|
| 271 |
def test_z_load(self): |
| 272 |
logger.info('Testing load() at the end of the test suite') |
| 273 |
load() |
| 274 |
|
| 275 |
def test_a_load(self): |
| 276 |
logger.info('Testing load() at the beginning of the test suite') |
| 277 |
load() |
| 278 |
|
| 279 |
|
| 280 |
if __name__ == '__main__': |
| 281 |
unittest.main() |