Skip to content

Commit 11802db

Browse files
committed
Add ad_block functionality
1 parent 87172e2 commit 11802db

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def test_anything(self):
3636
from bs4 import BeautifulSoup
3737
from pyvirtualdisplay import Display
3838
from seleniumbase.common import decorators
39+
from seleniumbase.config import ad_block_list
3940
from seleniumbase.config import settings
4041
from seleniumbase.core.application_manager import ApplicationManager
4142
from seleniumbase.core.s3_manager import S3LoggingBucket
@@ -75,13 +76,15 @@ def __init__(self, *args, **kwargs):
7576
self.driver = None
7677
self.environment = None
7778
self._last_url_of_delayed_assert = "data:,"
79+
self._last_page_load_url = "data:,"
7880
self._page_check_count = 0
7981
self._page_check_failures = []
8082
self._html_report_extra = []
8183
self._default_driver = None
8284
self._drivers_list = []
8385

8486
def open(self, url):
87+
self._last_page_load_url = None
8588
self.driver.get(url)
8689
if settings.WAIT_FOR_RSC_ON_PAGE_LOADS:
8790
self.wait_for_ready_state_complete()
@@ -411,11 +414,13 @@ def get_attribute(self, selector, attribute, by=By.CSS_SELECTOR,
411414
selector, attribute))
412415

413416
def refresh_page(self):
417+
self._last_page_load_url = None
414418
self.driver.refresh()
419+
self.wait_for_ready_state_complete()
415420

416421
def refresh(self):
417422
""" The shorter version of self.refresh_page() """
418-
self.driver.refresh()
423+
self.refresh_page()
419424

420425
def get_current_url(self):
421426
return self.driver.current_url
@@ -431,10 +436,14 @@ def get_title(self):
431436
return self.driver.title
432437

433438
def go_back(self):
439+
self._last_page_load_url = None
434440
self.driver.back()
441+
self.wait_for_ready_state_complete()
435442

436443
def go_forward(self):
444+
self._last_page_load_url = None
437445
self.driver.forward()
446+
self.wait_for_ready_state_complete()
438447

439448
def get_image_url(self, selector, by=By.CSS_SELECTOR,
440449
timeout=settings.SMALL_TIMEOUT):
@@ -978,6 +987,19 @@ def remove_elements(self, selector, by=By.CSS_SELECTOR):
978987
remove_script = """jQuery('%s').remove()""" % selector
979988
self.safe_execute_script(remove_script)
980989

990+
def ad_block(self):
991+
for css_selector in ad_block_list.AD_BLOCK_LIST:
992+
css_selector = re.escape(css_selector)
993+
script = ("""var $elements = document.querySelectorAll('%s');
994+
var index = 0, length = $elements.length;
995+
for(; index < length; index++){
996+
$elements[index].remove();}"""
997+
% css_selector)
998+
try:
999+
self.execute_script(script)
1000+
except Exception:
1001+
pass # Don't fail test if ad_blocking fails
1002+
9811003
def jq_format(self, code):
9821004
# DEPRECATED - Use re.escape() instead, which does the action you want.
9831005
return page_utils._jq_format(code)
@@ -1077,6 +1099,8 @@ def set_value(self, selector, new_value, by=By.CSS_SELECTOR,
10771099
element = self.wait_for_element_present(
10781100
orginal_selector, by=by, timeout=timeout)
10791101
element.send_keys(Keys.RETURN)
1102+
if settings.WAIT_FOR_RSC_ON_PAGE_LOADS:
1103+
self.wait_for_ready_state_complete()
10801104
self._demo_mode_pause_if_active()
10811105

10821106
def jquery_update_text_value(self, selector, new_value, by=By.CSS_SELECTOR,
@@ -1441,6 +1465,17 @@ def wait_for_ready_state_complete(self, timeout=settings.EXTREME_TIMEOUT):
14411465
is_ready = page_actions.wait_for_ready_state_complete(self.driver,
14421466
timeout)
14431467
self.wait_for_angularjs(timeout=settings.MINI_TIMEOUT)
1468+
if self.ad_block_on:
1469+
# If the ad_block feature is enabled, then block ads for new URLs
1470+
current_url = self.get_current_url()
1471+
if not current_url == self._last_page_load_url:
1472+
time.sleep(0.02)
1473+
self.ad_block()
1474+
time.sleep(0.01)
1475+
if self.is_element_present("iframe"):
1476+
time.sleep(0.07) # iframe ads take slightly longer to load
1477+
self.ad_block() # Do ad_block on slower-loading iframes
1478+
self._last_page_load_url = current_url
14441479
return is_ready
14451480

14461481
def wait_for_angularjs(self, timeout=settings.LARGE_TIMEOUT, **kwargs):

0 commit comments

Comments
 (0)