| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
import json |
| 4 |
import os |
| 5 |
import re |
| 6 |
import tempfile |
| 7 |
import time |
| 8 |
import unittest |
| 9 |
|
| 10 |
from contextlib import contextmanager |
| 11 |
from shutil import copytree, which |
| 12 |
|
| 13 |
from selenium import webdriver |
| 14 |
from selenium.common.exceptions import ( |
| 15 |
InvalidArgumentException, |
| 16 |
NoSuchWindowException, |
| 17 |
TimeoutException, |
| 18 |
WebDriverException, |
| 19 |
) |
| 20 |
from selenium.webdriver.chrome.options import Options as ChromeOptions |
| 21 |
from selenium.webdriver.edge.options import Options as EdgeOptions |
| 22 |
from selenium.webdriver.firefox.options import Options as FirefoxOptions |
| 23 |
from selenium.webdriver.firefox.service import Service as FirefoxService |
| 24 |
from selenium.webdriver.support.ui import WebDriverWait |
| 25 |
from selenium.webdriver.support import expected_conditions as EC |
| 26 |
from selenium.webdriver.common.by import By |
| 27 |
|
| 28 |
try: |
| 29 |
from xvfbwrapper import Xvfb |
| 30 |
except ImportError: |
| 31 |
print("\n\nxvfbwrapper Python package import failed") |
| 32 |
print("headless mode (ENABLE_XVFB=1) is not supported") |
| 33 |
|
| 34 |
|
| 35 |
SEL_DEFAULT_WAIT_TIMEOUT = 30 |
| 36 |
|
| 37 |
BROWSER_TYPES = ['chrome', 'firefox', 'edge'] |
| 38 |
BROWSER_NAMES = ['google-chrome', 'google-chrome-stable', 'google-chrome-beta', |
| 39 |
'firefox', 'microsoft-edge', 'microsoft-edge-beta'] |
| 40 |
|
| 41 |
|
| 42 |
class WindowNotFoundException(Exception): |
| 43 |
pass |
| 44 |
|
| 45 |
|
| 46 |
def get_browser_type(string): |
| 47 |
for t in BROWSER_TYPES: |
| 48 |
if t in string.lower(): |
| 49 |
return t |
| 50 |
raise ValueError(f"Could not get browser type from {string}") |
| 51 |
|
| 52 |
|
| 53 |
def get_browser_name(string): |
| 54 |
for bn in BROWSER_NAMES: |
| 55 |
if string in bn: |
| 56 |
bn_path = which(bn) |
| 57 |
if bn_path: |
| 58 |
return os.path.basename(bn_path) |
| 59 |
raise ValueError(f"Could not get browser name from {string}") |
| 60 |
|
| 61 |
|
| 62 |
class Shim: |
| 63 |
_browser_msg = '''BROWSER should be one of: |
| 64 |
* /path/to/a/browser |
| 65 |
* a browser executable name so we can find the browser with "which $BROWSER" |
| 66 |
* something from BROWSER_TYPES |
| 67 |
''' |
| 68 |
__doc__ = 'Chooses the correct driver and extension_url based on the BROWSER environment\nvariable. ' + _browser_msg |
| 69 |
|
| 70 |
def __init__(self): |
| 71 |
print("\n\nConfiguring the test run ...") |
| 72 |
|
| 73 |
browser = os.environ.get('BROWSER') |
| 74 |
|
| 75 |
# get browser_path and browser_type first |
| 76 |
if browser is None: |
| 77 |
raise ValueError("The BROWSER environment variable is not set. " + self._browser_msg) |
| 78 |
|
| 79 |
if ("/" in browser) or ("\\" in browser): |
| 80 |
# path to a browser binary |
| 81 |
self.browser_path = browser |
| 82 |
self.browser_type = get_browser_type(browser) |
| 83 |
elif which(browser): |
| 84 |
# executable browser name like 'google-chrome-stable' |
| 85 |
self.browser_path = which(browser) |
| 86 |
self.browser_type = get_browser_type(browser) |
| 87 |
elif get_browser_type(browser): |
| 88 |
# browser type like 'firefox' or 'chrome' |
| 89 |
bname = get_browser_name(browser) |
| 90 |
self.browser_path = which(bname) |
| 91 |
self.browser_type = get_browser_type(browser) |
| 92 |
else: |
| 93 |
raise ValueError(f"Could not infer BROWSER from {browser}") |
| 94 |
|
| 95 |
self.extension_path = os.path.abspath( |
| 96 |
os.path.join(os.path.dirname(__file__), '..', '..', 'src')) |
| 97 |
|
| 98 |
if self.browser_type in ('chrome', 'edge'): |
| 99 |
# this extension ID and the "key" property in manifest.json |
| 100 |
# must both be derived from the same private key |
| 101 |
self.info = { |
| 102 |
'extension_id': 'mcgekeccgjgcmhnhbabplanchdogjcnh' |
| 103 |
} |
| 104 |
self.base_url = f"chrome-extension://{self.info['extension_id']}/" |
| 105 |
if self.browser_type == 'chrome': |
| 106 |
self.manager = self.chrome_manager |
| 107 |
else: |
| 108 |
self.manager = self.edge_manager |
| 109 |
|
| 110 |
# make extension ID constant across runs |
| 111 |
self.fix_chrome_extension_id() |
| 112 |
|
| 113 |
elif self.browser_type == 'firefox': |
| 114 |
self.info = { |
| 115 |
'extension_id': 'jid1-MnnxcxisBPnSXQ@jetpack', |
| 116 |
'uuid': 'd56a5b99-51b6-4e83-ab23-796216679614' |
| 117 |
} |
| 118 |
self.manager = self.firefox_manager |
| 119 |
self.base_url = f"moz-extension://{self.info['uuid']}/" |
| 120 |
|
| 121 |
print(f"\nUsing browser path: {self.browser_path}\n" |
| 122 |
f"with browser type: {self.browser_type}\n" |
| 123 |
f"and extension path: {self.extension_path}\n") |
| 124 |
|
| 125 |
def fix_chrome_extension_id(self): |
| 126 |
# create temp directory |
| 127 |
# pylint: disable-next=consider-using-with |
| 128 |
self.tmp_dir = tempfile.TemporaryDirectory() |
| 129 |
new_extension_path = os.path.join(self.tmp_dir.name, "src") |
| 130 |
|
| 131 |
# copy extension sources there |
| 132 |
copytree(self.extension_path, new_extension_path) |
| 133 |
|
| 134 |
# update manifest.json |
| 135 |
manifest_path = os.path.join(new_extension_path, "manifest.json") |
| 136 |
with open(manifest_path, "r", encoding="utf-8") as f: |
| 137 |
manifest = json.load(f) |
| 138 |
# this key and the extension ID must both be derived from the same private key |
| 139 |
manifest['key'] = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArMdgFkGsm7nOBr/9qkx8XEcmYSu1VkIXXK94oXLz1VKGB0o2MN+mXL/Dsllgkh61LZgK/gVuFFk89e/d6Vlsp9IpKLANuHgyS98FKx1+3sUoMujue+hyxulEGxXXJKXhk0kGxWdE0IDOamFYpF7Yk0K8Myd/JW1U2XOoOqJRZ7HR6is1W6iO/4IIL2/j3MUioVqu5ClT78+fE/Fn9b/DfzdX7RxMNza9UTiY+JCtkRTmm4ci4wtU1lxHuVmWiaS45xLbHphQr3fpemDlyTmaVoE59qG5SZZzvl6rwDah06dH01YGSzUF1ezM2IvY9ee1nMSHEadQRQ2sNduNZWC9gwIDAQAB" # noqa:E501 pylint:disable=line-too-long |
| 140 |
with open(manifest_path, "w", encoding="utf-8") as f: |
| 141 |
json.dump(manifest, f) |
| 142 |
|
| 143 |
# update self.extension_path |
| 144 |
self.extension_path = new_extension_path |
| 145 |
|
| 146 |
@property |
| 147 |
def wants_xvfb(self): |
| 148 |
if self.on_github_actions or bool(int(os.environ.get('ENABLE_XVFB', 0))): |
| 149 |
try: |
| 150 |
Xvfb |
| 151 |
except NameError: |
| 152 |
print("\nHeadless mode not supported: install xvfbwrapper first") |
| 153 |
return False |
| 154 |
return True |
| 155 |
return False |
| 156 |
|
| 157 |
@property |
| 158 |
def on_github_actions(self): |
| 159 |
if "GITHUB_ACTIONS" in os.environ: |
| 160 |
return True |
| 161 |
return False |
| 162 |
|
| 163 |
@contextmanager |
| 164 |
def chrome_manager(self): |
| 165 |
opts = ChromeOptions() |
| 166 |
|
| 167 |
# support tests in Chrome with MV2 PB while this flag remains available |
| 168 |
opts.add_argument("--enable-features=AllowLegacyMV2Extensions") |
| 169 |
|
| 170 |
opts.binary_location = self.browser_path |
| 171 |
opts.enable_bidi = True |
| 172 |
opts.enable_webextensions = True |
| 173 |
|
| 174 |
# work around https://issues.chromium.org/issues/409441960 |
| 175 |
opts.add_experimental_option('enableExtensionTargets', True) |
| 176 |
|
| 177 |
# https://github.com/GoogleChromeLabs/chromium-bidi/issues/3281 |
| 178 |
opts.set_capability("unhandledPromptBehavior", "ignore"); |
| 179 |
|
| 180 |
# TODO not yet in Firefox (w/o hacks anyway): |
| 181 |
# https://github.com/mozilla/geckodriver/issues/284#issuecomment-456073771 |
| 182 |
opts.set_capability("goog:loggingPrefs", {'browser': 'ALL'}) |
| 183 |
|
| 184 |
for i in range(5): |
| 185 |
try: |
| 186 |
driver = webdriver.Chrome(options=opts) |
| 187 |
driver.webextension.install(self.extension_path) |
| 188 |
except WebDriverException as ex: |
| 189 |
if i == 0: print("") |
| 190 |
print(f"ChromeDriver initialization failed: {ex}") |
| 191 |
else: |
| 192 |
break |
| 193 |
|
| 194 |
try: |
| 195 |
yield driver |
| 196 |
finally: |
| 197 |
driver.quit() |
| 198 |
|
| 199 |
@contextmanager |
| 200 |
def edge_manager(self): |
| 201 |
opts = EdgeOptions() |
| 202 |
|
| 203 |
opts.binary_location = self.browser_path |
| 204 |
opts.enable_bidi = True |
| 205 |
opts.enable_webextensions = True |
| 206 |
|
| 207 |
# work around https://issues.chromium.org/issues/409441960 |
| 208 |
opts.add_experimental_option('enableExtensionTargets', True) |
| 209 |
|
| 210 |
# https://github.com/GoogleChromeLabs/chromium-bidi/issues/3281 |
| 211 |
opts.set_capability("unhandledPromptBehavior", "ignore"); |
| 212 |
|
| 213 |
for i in range(5): |
| 214 |
try: |
| 215 |
driver = webdriver.Edge(options=opts) |
| 216 |
driver.webextension.install(self.extension_path) |
| 217 |
except WebDriverException as ex: |
| 218 |
if i == 0: print("") |
| 219 |
print(f"EdgeDriver initialization failed ({i+1}/5): {ex}", end='') |
| 220 |
else: |
| 221 |
break |
| 222 |
|
| 223 |
try: |
| 224 |
yield driver |
| 225 |
finally: |
| 226 |
driver.quit() |
| 227 |
|
| 228 |
@contextmanager |
| 229 |
def firefox_manager(self): |
| 230 |
opts = FirefoxOptions() |
| 231 |
|
| 232 |
opts.binary_location = self.browser_path |
| 233 |
opts.enable_bidi = True |
| 234 |
opts.enable_webextensions = True |
| 235 |
|
| 236 |
# https://github.com/mozilla/geckodriver/issues/2241#issuecomment-3984861843 |
| 237 |
opts.set_capability("unhandledPromptBehavior", "ignore"); |
| 238 |
|
| 239 |
# make extension ID constant across runs |
| 240 |
opts.set_preference('extensions.webextensions.uuids', '{"%s": "%s"}' % ( |
| 241 |
self.info['extension_id'], self.info['uuid'])) |
| 242 |
|
| 243 |
# needed for test_referrer_header() |
| 244 |
# https://bugzilla.mozilla.org/show_bug.cgi?id=1720294 |
| 245 |
opts.set_preference('network.http.referer.disallowCrossSiteRelaxingDefault', False) |
| 246 |
|
| 247 |
# disable tracker cookie blocking as it breaks cookie tests |
| 248 |
# that use trackersimulator.org, a "known tracker", |
| 249 |
# and disable cookie site isolation, as it breaks the cookie |
| 250 |
# tracking detection test |
| 251 |
opts.set_preference("network.cookie.cookieBehavior", 0) |
| 252 |
|
| 253 |
# disable JSON viewer as it breaks parsing JSON pages |
| 254 |
opts.set_preference("devtools.jsonview.enabled", False) |
| 255 |
|
| 256 |
# to produce a trace-level geckodriver.log, |
| 257 |
# set the log_output argument to FirefoxService() to "./geckodriver.log" |
| 258 |
# and uncomment the line below |
| 259 |
#opts.log.level = "trace" |
| 260 |
|
| 261 |
for i in range(5): |
| 262 |
try: |
| 263 |
service = FirefoxService(log_output=os.path.devnull, |
| 264 |
# https://bugzilla.mozilla.org/show_bug.cgi?id=2048451 |
| 265 |
service_args=['--allow-system-access']) |
| 266 |
driver = webdriver.Firefox(options=opts, service=service) |
| 267 |
driver.webextension.install(self.extension_path) |
| 268 |
except WebDriverException as ex: |
| 269 |
if i == 0: print("") |
| 270 |
print(f"Firefox WebDriver initialization failed ({i+1}/5): {ex}", end='') |
| 271 |
else: |
| 272 |
break |
| 273 |
|
| 274 |
try: |
| 275 |
yield driver |
| 276 |
finally: |
| 277 |
driver.quit() |
| 278 |
|
| 279 |
|
| 280 |
shim = Shim() # create the browser shim |
| 281 |
|
| 282 |
|
| 283 |
def retry_until(fun, tester=None, times=3, msg=None): |
| 284 |
""" |
| 285 |
Execute function `fun` until either its return is truthy |
| 286 |
(or if `tester` is set, until the result of calling `tester` with `fun`'s return is truthy), |
| 287 |
or it gets executed X times, where X = `times` + 1. |
| 288 |
""" |
| 289 |
for i in range(times): |
| 290 |
result = fun() |
| 291 |
|
| 292 |
if tester is not None: |
| 293 |
if tester(result): |
| 294 |
break |
| 295 |
elif result: |
| 296 |
break |
| 297 |
|
| 298 |
if msg: |
| 299 |
if i == 0: |
| 300 |
print("") |
| 301 |
print(msg) |
| 302 |
|
| 303 |
time.sleep(2 ** i) |
| 304 |
|
| 305 |
return result |
| 306 |
|
| 307 |
|
| 308 |
class PBSeleniumTest(unittest.TestCase): |
| 309 |
@classmethod |
| 310 |
def setUpClass(cls): |
| 311 |
cls.manager = shim.manager |
| 312 |
cls.base_url = shim.base_url |
| 313 |
cls.wants_xvfb = shim.wants_xvfb |
| 314 |
if cls.wants_xvfb: |
| 315 |
cls.vdisplay = Xvfb(width=1280, height=720) |
| 316 |
cls.vdisplay.start() |
| 317 |
|
| 318 |
@classmethod |
| 319 |
def tearDownClass(cls): |
| 320 |
if cls.wants_xvfb: |
| 321 |
cls.vdisplay.stop() |
| 322 |
|
| 323 |
def init(self, driver): |
| 324 |
self.driver = driver |
| 325 |
self.js = self.driver.execute_script |
| 326 |
self.options_url = self.base_url + "skin/options.html" |
| 327 |
self.popup_url = self.base_url + "skin/popup.html" |
| 328 |
self.first_run_url = self.base_url + "skin/firstRun.html" |
| 329 |
self.test_url = self.base_url + "tests/index.html" |
| 330 |
|
| 331 |
def run(self, result=None): |
| 332 |
# disable Selenium sending your IP to a third-party company |
| 333 |
# https://www.selenium.dev/documentation/selenium_manager/#data-collection |
| 334 |
os.environ['SE_AVOID_STATS'] = "true" |
| 335 |
|
| 336 |
with self.manager() as driver: |
| 337 |
self.init(driver) |
| 338 |
|
| 339 |
# wait for Badger's storage, listeners, ... |
| 340 |
self.load_url(self.options_url) |
| 341 |
self.wait_for_script( |
| 342 |
"let done = arguments[arguments.length - 1];" |
| 343 |
"chrome.runtime.sendMessage({" |
| 344 |
" type: 'isBadgerInitialized'" |
| 345 |
"}, r => done(r));", execute_async=True) |
| 346 |
|
| 347 |
# also disable the welcome page |
| 348 |
self.driver.execute_async_script( |
| 349 |
"let done = arguments[arguments.length - 1];" |
| 350 |
"chrome.runtime.sendMessage({" |
| 351 |
" type: 'updateSettings'," |
| 352 |
" data: { showIntroPage: false }" |
| 353 |
"}, () => {" |
| 354 |
" chrome.tabs.query({}, (res) => {" |
| 355 |
" let welcome_tab = res && res.find(" |
| 356 |
" tab => tab.url == chrome.runtime.getURL('skin/firstRun.html'));" |
| 357 |
" if (!welcome_tab) {" |
| 358 |
" return done();" |
| 359 |
" }" |
| 360 |
" chrome.tabs.remove(welcome_tab.id, done);" |
| 361 |
" });" |
| 362 |
"});") |
| 363 |
|
| 364 |
super().run(result) |
| 365 |
|
| 366 |
def is_firefox_nightly(self): |
| 367 |
caps = self.driver.capabilities |
| 368 |
if caps['browserName'] == "firefox": |
| 369 |
version = caps['browserVersion'] |
| 370 |
return re.search('a[0-9]+$', version) is not None |
| 371 |
return False |
| 372 |
|
| 373 |
def open_window(self): |
| 374 |
try: |
| 375 |
self.driver.switch_to.new_window('tab') |
| 376 |
except NoSuchWindowException: |
| 377 |
time.sleep(1) |
| 378 |
self.driver.switch_to.new_window('tab') |
| 379 |
|
| 380 |
def load_url(self, url, wait_for_body_text=False, retries=5): |
| 381 |
"""Load a URL and wait before returning.""" |
| 382 |
for i in range(retries): |
| 383 |
try: |
| 384 |
self.driver.get(url) |
| 385 |
break |
| 386 |
except TimeoutException as e: |
| 387 |
if i < retries - 1: |
| 388 |
time.sleep(2 ** i) |
| 389 |
continue |
| 390 |
raise e |
| 391 |
# work around geckodriver/marionette/Firefox timeout handling, |
| 392 |
# for example: https://travis-ci.org/EFForg/privacybadger/jobs/389429089 |
| 393 |
except WebDriverException as e: |
| 394 |
if str(e).startswith("Reached error page") and i < retries - 1: |
| 395 |
time.sleep(2 ** i) |
| 396 |
continue |
| 397 |
raise e |
| 398 |
|
| 399 |
if wait_for_body_text: |
| 400 |
# wait for document.body.textContent to become truthy |
| 401 |
retry_until( |
| 402 |
lambda: self.driver.find_element(By.TAG_NAME, 'body').text) |
| 403 |
|
| 404 |
def txt_by_css(self, css_selector, timeout=SEL_DEFAULT_WAIT_TIMEOUT): |
| 405 |
"""Find an element by CSS selector and return its text.""" |
| 406 |
return self.find_el_by_css( |
| 407 |
css_selector, visible_only=False, timeout=timeout).text |
| 408 |
|
| 409 |
def find_el_by_css(self, css_selector, visible_only=True, timeout=SEL_DEFAULT_WAIT_TIMEOUT): |
| 410 |
condition = ( |
| 411 |
EC.visibility_of_element_located if visible_only |
| 412 |
else EC.presence_of_element_located |
| 413 |
) |
| 414 |
return WebDriverWait(self.driver, timeout).until( |
| 415 |
condition((By.CSS_SELECTOR, css_selector))) |
| 416 |
|
| 417 |
def find_el_by_xpath(self, xpath, timeout=SEL_DEFAULT_WAIT_TIMEOUT): |
| 418 |
return WebDriverWait(self.driver, timeout).until( |
| 419 |
EC.visibility_of_element_located((By.XPATH, xpath))) |
| 420 |
|
| 421 |
@contextmanager |
| 422 |
def wait_for_reload(self, timeout=SEL_DEFAULT_WAIT_TIMEOUT): |
| 423 |
"""Context manager that waits for the page to reload, |
| 424 |
to be used with actions that reload the page.""" |
| 425 |
page = self.driver.find_element(By.TAG_NAME, 'html') |
| 426 |
yield |
| 427 |
try: |
| 428 |
WebDriverWait(self.driver, timeout).until(EC.staleness_of(page)) |
| 429 |
except WebDriverException as e: |
| 430 |
# work around Firefox nonsense |
| 431 |
if str(e).startswith("Message: TypeError: can't access dead object"): |
| 432 |
pass |
| 433 |
else: |
| 434 |
raise e |
| 435 |
|
| 436 |
def wait_for_script(self, script, *script_args, |
| 437 |
timeout=SEL_DEFAULT_WAIT_TIMEOUT, execute_async=False, |
| 438 |
message="Timed out waiting for execute_script to eval to True"): |
| 439 |
|
| 440 |
"""Variant of execute_script/execute_async_script |
| 441 |
that keeps rerunning the script until it returns True.""" |
| 442 |
|
| 443 |
def execute_script(dr): |
| 444 |
if execute_async: return dr.execute_async_script(script, *script_args) |
| 445 |
return dr.execute_script(script, *script_args) |
| 446 |
|
| 447 |
return WebDriverWait(self.driver, timeout).until(execute_script, message) |
| 448 |
|
| 449 |
def wait_for_any_text(self, selector, timeout=SEL_DEFAULT_WAIT_TIMEOUT): |
| 450 |
return WebDriverWait(self.driver, timeout).until( |
| 451 |
lambda d: d.find_element(By.CSS_SELECTOR, selector).text.strip()) |
| 452 |
|
| 453 |
def wait_for_text(self, selector, text, timeout=SEL_DEFAULT_WAIT_TIMEOUT): |
| 454 |
return WebDriverWait(self.driver, timeout).until( |
| 455 |
EC.text_to_be_present_in_element( |
| 456 |
(By.CSS_SELECTOR, selector), text)) |
| 457 |
|
| 458 |
def wait_for_and_switch_to_frame(self, selector, timeout=SEL_DEFAULT_WAIT_TIMEOUT): |
| 459 |
return WebDriverWait(self.driver, timeout).until( |
| 460 |
EC.frame_to_be_available_and_switch_to_it( |
| 461 |
(By.CSS_SELECTOR, selector))) |
| 462 |
|
| 463 |
def switch_to_window_with_url(self, url, max_tries=5): |
| 464 |
"""Point the driver to the first window that matches this url.""" |
| 465 |
|
| 466 |
for _ in range(max_tries): |
| 467 |
for w in self.driver.window_handles: |
| 468 |
try: |
| 469 |
self.driver.switch_to.window(w) |
| 470 |
if self.driver.current_url != url: |
| 471 |
continue |
| 472 |
except (InvalidArgumentException, NoSuchWindowException): |
| 473 |
pass |
| 474 |
except WebDriverException as e: |
| 475 |
if "cannot determine loading status" in str(e): |
| 476 |
pass |
| 477 |
else: |
| 478 |
raise e |
| 479 |
else: |
| 480 |
return |
| 481 |
|
| 482 |
time.sleep(1) |
| 483 |
|
| 484 |
raise WindowNotFoundException("Failed to find window for " + url) |
| 485 |
|
| 486 |
|
| 487 |
def close_window_with_url(self, url, max_tries=5): |
| 488 |
self.switch_to_window_with_url(url, max_tries) |
| 489 |
|
| 490 |
if len(self.driver.window_handles) == 1: |
| 491 |
# open another window to avoid implicit session deletion |
| 492 |
self.open_window() |
| 493 |
self.switch_to_window_with_url(url, max_tries) |
| 494 |
|
| 495 |
self.driver.close() |
| 496 |
self.driver.switch_to.window(self.driver.window_handles[0]) |
| 497 |
self.open_window() |
| 498 |
|
| 499 |
def set_dnt(self, domain): |
| 500 |
self.load_url(self.options_url) |
| 501 |
self.driver.execute_async_script( |
| 502 |
"let done = arguments[arguments.length - 1];" |
| 503 |
"chrome.runtime.sendMessage({" |
| 504 |
" type: 'setDnt'," |
| 505 |
" domain: arguments[0]" |
| 506 |
"}, done);", domain) |
| 507 |
|
| 508 |
def check_dnt(self, domain): |
| 509 |
self.load_url(self.options_url) |
| 510 |
return self.driver.execute_async_script( |
| 511 |
"let done = arguments[arguments.length - 1];" |
| 512 |
"chrome.runtime.sendMessage({" |
| 513 |
" type: 'checkForDntPolicy'," |
| 514 |
" domain: arguments[0]" |
| 515 |
"}, done);", domain) |
| 516 |
|
| 517 |
def set_user_action(self, domain, action): |
| 518 |
"""Adds or modifies the action_map entry for `domain`, |
| 519 |
setting userAction to "user_" + `action`.""" |
| 520 |
self.load_url(self.options_url) |
| 521 |
self.driver.execute_async_script( |
| 522 |
"let done = arguments[arguments.length - 1];" |
| 523 |
"chrome.runtime.sendMessage({" |
| 524 |
" type: 'saveOptionsToggle'," |
| 525 |
" domain: arguments[0]," |
| 526 |
" action: arguments[1]" |
| 527 |
"}, done);", domain, action) |
| 528 |
|
| 529 |
def add_domain(self, domain, action): |
| 530 |
"""Adds or modifies the action_map entry for `domain`, |
| 531 |
setting heuristicAction to `action`.""" |
| 532 |
self.load_url(self.options_url) |
| 533 |
self.driver.execute_async_script( |
| 534 |
"let done = arguments[arguments.length - 1]," |
| 535 |
" domain = arguments[0]," |
| 536 |
" action = arguments[1];" |
| 537 |
"chrome.runtime.sendMessage({" |
| 538 |
" type: 'setAction', domain, action" |
| 539 |
"}, done);", domain, action) |
| 540 |
|
| 541 |
def block_domain(self, domain): |
| 542 |
self.add_domain(domain, "block") |
| 543 |
|
| 544 |
def cookieblock_domain(self, domain): |
| 545 |
self.add_domain(domain, "cookieblock") |
| 546 |
|
| 547 |
def add_site_override(self, domain, site_domain): |
| 548 |
self.load_url(self.options_url) |
| 549 |
self.driver.execute_async_script( |
| 550 |
"let done = arguments[arguments.length - 1];" |
| 551 |
"chrome.runtime.sendMessage({" |
| 552 |
" type: 'addSiteOverride'," |
| 553 |
" domain: arguments[0]," |
| 554 |
" site_domain: arguments[1]" |
| 555 |
"}, done);", domain, site_domain) |
| 556 |
|
| 557 |
def disable_badger_on_site(self, url): |
| 558 |
self.load_url(self.options_url) |
| 559 |
self.wait_for_script("return window.OPTIONS_INITIALIZED") |
| 560 |
self.driver.find_element(By.ID, 'new-disabled-site-input').send_keys(url) |
| 561 |
self.driver.find_element(By.CSS_SELECTOR, '#add-disabled-site').click() |
| 562 |
|
| 563 |
def reenable_badger_on_site(self, domain): |
| 564 |
self.load_url(self.options_url) |
| 565 |
self.driver.execute_async_script( |
| 566 |
"let done = arguments[arguments.length - 1];" |
| 567 |
"chrome.runtime.sendMessage({" |
| 568 |
" type: 'reenableOnSites'," |
| 569 |
" domains: [arguments[0]]" |
| 570 |
"}, done);", domain) |
| 571 |
|
| 572 |
def get_domain_slider_state(self, domain): |
| 573 |
label = self.driver.find_element( |
| 574 |
By.CSS_SELECTOR, f'input[name="{domain}"][checked]') |
| 575 |
return label.get_property('value') |
| 576 |
|
| 577 |
def clear_tracker_data(self): |
| 578 |
self.load_url(self.options_url) |
| 579 |
self.driver.execute_async_script( |
| 580 |
"let done = arguments[arguments.length - 1];" |
| 581 |
"chrome.runtime.sendMessage({" |
| 582 |
" type: 'removeAllData'" |
| 583 |
"}, done);") |
| 584 |
|
| 585 |
def get_badger_storage(self, store_name): |
| 586 |
self.load_url(self.options_url) |
| 587 |
return self.driver.execute_async_script(( |
| 588 |
"let done = arguments[arguments.length - 1]," |
| 589 |
" store_name = arguments[0];" |
| 590 |
"chrome.runtime.sendMessage({" |
| 591 |
" type: 'syncStorage'," |
| 592 |
" storeName: store_name" |
| 593 |
"}, function () {" |
| 594 |
" chrome.storage.local.get([store_name], function (res) {" |
| 595 |
" done(res[store_name]);" |
| 596 |
" });" |
| 597 |
"});" |
| 598 |
), store_name) |
| 599 |
|
| 600 |
def open_popup(self, target_url=None, show_reminder=False): |
| 601 |
"""Show the PB popup as a new tab. |
| 602 |
|
| 603 |
If Selenium would let us just programmatically launch an extension from its icon, |
| 604 |
we wouldn't need this method. Alas it will not. |
| 605 |
|
| 606 |
But! We can open a new tab and set the url to the extension's popup html page and |
| 607 |
test away. That's how most devs test extensions. But**2!! PB's popup code uses |
| 608 |
the current tab's url to report the current tracker status. And since we changed |
| 609 |
the current tab's url when we loaded the popup as a tab, the popup loses all the |
| 610 |
blocker status information from the original tab. |
| 611 |
|
| 612 |
The workaround is to execute a new convenience function in the popup codebase that |
| 613 |
looks for a given url in the tabs and, if it finds a match, refreshes the popup |
| 614 |
with the associated tabid. Then the correct status information will be displayed |
| 615 |
in the popup.""" |
| 616 |
|
| 617 |
for _ in range(5): |
| 618 |
self.open_window() |
| 619 |
self.load_url(self.popup_url) |
| 620 |
self.wait_for_script("return window.POPUP_INITIALIZED") |
| 621 |
|
| 622 |
# get the popup populated with status information for the correct url |
| 623 |
self.js(""" |
| 624 |
/** |
| 625 |
* @param {String} [url] |
| 626 |
* @param {Boolean} [show_reminder] |
| 627 |
*/ |
| 628 |
(function (url, show_reminder) { |
| 629 |
let queryOpts = { currentWindow: true }; |
| 630 |
if (url) { |
| 631 |
queryOpts = { url }; |
| 632 |
} |
| 633 |
chrome.tabs.query(queryOpts, function (tabs) { |
| 634 |
if (!tabs || !tabs.length) { |
| 635 |
return; |
| 636 |
} |
| 637 |
chrome.runtime.sendMessage({ |
| 638 |
type: "getPopupData", |
| 639 |
tabId: tabs[0].id, |
| 640 |
tabUrl: tabs[0].url |
| 641 |
}, (response) => { |
| 642 |
response.settings.seenComic = !show_reminder; |
| 643 |
setPopupData(response); |
| 644 |
refreshPopup(); |
| 645 |
showNagMaybe(); // not init() because init() already ran and should only run once |
| 646 |
window.DONE_REFRESHING = true; |
| 647 |
}); |
| 648 |
}); |
| 649 |
}(arguments[0], arguments[1]));""", target_url, show_reminder) |
| 650 |
|
| 651 |
try: |
| 652 |
# wait for popup to be ready |
| 653 |
self.wait_for_script("return window.DONE_REFRESHING && window.SLIDERS_DONE") |
| 654 |
except TimeoutException: |
| 655 |
continue |
| 656 |
else: |
| 657 |
break |
| 658 |
|
| 659 |
def get_tracker_state(self): |
| 660 |
"""Parse the UI to group all third party domains into their respective action states.""" |
| 661 |
|
| 662 |
notYetBlocked = {} |
| 663 |
cookieBlocked = {} |
| 664 |
blocked = {} |
| 665 |
|
| 666 |
domain_divs = self.driver.find_elements(By.CSS_SELECTOR, |
| 667 |
"#blockedResourcesInner > div.clicker[data-origin]") |
| 668 |
for div in domain_divs: |
| 669 |
domain = div.get_dom_attribute('data-origin') |
| 670 |
|
| 671 |
# assert that this domain is never duplicated in the UI |
| 672 |
self.assertNotIn(domain, notYetBlocked) |
| 673 |
self.assertNotIn(domain, cookieBlocked) |
| 674 |
self.assertNotIn(domain, blocked) |
| 675 |
|
| 676 |
# get slider state for given domain |
| 677 |
action_type = self.get_domain_slider_state(domain) |
| 678 |
|
| 679 |
# non-tracking domains are hidden by default |
| 680 |
# so if we see a slider set to "allow", |
| 681 |
# it must be in the tracking-but-not-yet-blocked section |
| 682 |
if action_type == 'allow': |
| 683 |
notYetBlocked[domain] = True |
| 684 |
elif action_type == 'cookieblock': |
| 685 |
cookieBlocked[domain] = True |
| 686 |
elif action_type == 'block': |
| 687 |
blocked[domain] = True |
| 688 |
else: |
| 689 |
self.fail(f"what is this?!? {action_type}") |
| 690 |
|
| 691 |
return { |
| 692 |
'notYetBlocked': notYetBlocked, |
| 693 |
'cookieBlocked': cookieBlocked, |
| 694 |
'blocked': blocked |
| 695 |
} |
| 696 |
|
| 697 |
@property |
| 698 |
def logs(self): |
| 699 |
# TODO not yet in Firefox |
| 700 |
return [log.get('message') for log in self.driver.get_log('browser')] |