def test_options_ui_open_in_tab(self): # open options page manually, keeping the new user intro page self.open_window() self.load_options_page() # switch to new user intro page switch_to_window_with_url(self.driver, self.first_run_url) # save open windows handles_before = set(self.driver.window_handles) # open options page using dedicated chrome API self.js("chrome.runtime.openOptionsPage();") # if we switched to the previously manually opened options page, all good # if we haven't, this must mean options_ui's open_in_tab no longer works new_handles = set( self.driver.window_handles).difference(handles_before) num_newly_opened_windows = len(new_handles) if num_newly_opened_windows: self.driver.switch_to.window(new_handles.pop()) self.assertEqual( num_newly_opened_windows, 0, "Expected to switch to existing options page, " "opened a new page ({}) instead: {}".format( self.driver.title, self.driver.current_url))
def test_canvas_fingerprinting_detection(self): PAGE_URL = ( "https://gitcdn.link/cdn/ghostwords/" "ff6347b93ec126d4f73a9ddfd8b09919/raw/6b215b3f052115d36831ecfca758081ca7da7e37/" "privacy_badger_fingerprint_test_fixture.html" ) FINGERPRINTING_DOMAIN = "cdn.jsdelivr.net" # open Badger's background page self.load_url(self.bg_url) # need to keep Badger's background page open for tabData to persist # so, open and switch to a new window self.open_window() # visit the page self.load_url(PAGE_URL) # switch back to Badger's background page switch_to_window_with_url(self.driver, self.bg_url) # check that we detected the fingerprinting domain as a tracker self.assertTrue( retry_until(partial(self.detected_tracking, FINGERPRINTING_DOMAIN, PAGE_URL)), "Canvas fingerprinting resource was detected as a tracker.") # check that we detected canvas fingerprinting self.assertTrue( self.detected_fingerprinting(FINGERPRINTING_DOMAIN), "Canvas fingerprinting resources was detected as a fingerprinter." )
def test_canvas_fingerprinting_detection(self): PAGE_URL = ( "https://cdn.rawgit.com/ghostwords" "/ff6347b93ec126d4f73a9ddfd8b09919/raw/2332f82d3982bd4a84cd2380aed90228955d1f2a" "/privacy_badger_fingerprint_test_fixture.html" ) FINGERPRINTING_DOMAIN = "cdn.jsdelivr.net" # open Badger's background page self.load_url(self.bg_url) # need to keep Badger's background page open for tabData to persist # so, either open and switch to a new window, # or just reuse the already-open new user welcome window switch_to_window_with_url(self.driver, self.first_run_url) # visit the page self.load_url(PAGE_URL) # switch back to Badger's background page switch_to_window_with_url(self.driver, self.bg_url) # check that we detected the fingerprinting domain as a tracker self.assertTrue( retry_until(partial(self.detected_tracking, FINGERPRINTING_DOMAIN, PAGE_URL)), "Canvas fingerprinting resource was detected as a tracker.") # check that we detected canvas fingerprinting self.assertTrue( self.detected_fingerprinting(FINGERPRINTING_DOMAIN), "Canvas fingerprinting resources was detected as a fingerprinter." )
def test_options_ui_open_in_tab(self): # open options page manually, keeping the new user intro page self.open_window() self.load_options_page() # switch to new user intro page switch_to_window_with_url(self.driver, self.first_run_url) # save open windows handles_before = set(self.driver.window_handles) # open options page using dedicated chrome API self.js("chrome.runtime.openOptionsPage();") # if we switched to the previously manually opened options page, all good # if we haven't, this must mean options_ui's open_in_tab no longer works new_handles = set(self.driver.window_handles).difference(handles_before) num_newly_opened_windows = len(new_handles) if num_newly_opened_windows: self.driver.switch_to.window(new_handles.pop()) self.assertEqual(num_newly_opened_windows, 0, "Expected to switch to existing options page, " "opened a new page ({}) instead: {}".format( self.driver.title, self.driver.current_url))
def test_canvas_fingerprinting_detection(self): PAGE_URL = ( "https://gitcdn.link/cdn/ghostwords/" "ff6347b93ec126d4f73a9ddfd8b09919/raw/6b215b3f052115d36831ecfca758081ca7da7e37/" "privacy_badger_fingerprint_test_fixture.html") FINGERPRINTING_DOMAIN = "cdn.jsdelivr.net" # open Badger's background page self.load_url(self.bg_url) # need to keep Badger's background page open for tabData to persist # so, open and switch to a new window self.open_window() # visit the page self.load_url(PAGE_URL) # switch back to Badger's background page switch_to_window_with_url(self.driver, self.bg_url) # check that we detected the fingerprinting domain as a tracker self.assertTrue( retry_until( partial(self.detected_tracking, FINGERPRINTING_DOMAIN, PAGE_URL)), "Canvas fingerprinting resource was detected as a tracker.") # check that we detected canvas fingerprinting self.assertTrue( self.detected_fingerprinting(FINGERPRINTING_DOMAIN), "Canvas fingerprinting resources was detected as a fingerprinter.")
def test_canvas_fingerprinting_detection(self): PAGE_URL = "https://www.eff.org/files/badger_test_fixtures/fingerprinting.html" FINGERPRINTING_DOMAIN = "cdn.jsdelivr.net" # open Badger's background page self.load_url(self.bg_url) # need to keep Badger's background page open for tabData to persist # so, open and switch to a new window self.open_window() # visit the page self.load_url(PAGE_URL) # switch back to Badger's background page switch_to_window_with_url(self.driver, self.bg_url) # check that we detected the fingerprinting domain as a tracker self.assertTrue( retry_until( partial(self.detected_tracking, FINGERPRINTING_DOMAIN, PAGE_URL)), "Canvas fingerprinting resource was detected as a tracker.") # check that we detected canvas fingerprinting self.assertTrue( self.detected_fingerprinting(FINGERPRINTING_DOMAIN), "Canvas fingerprinting resources was detected as a fingerprinter.")
def test_should_not_record_nontracking_domains(self): TEST_URL = ( "https://cdn.rawgit.com/ghostwords/" "eef2c982fc3151e60a78136ca263294d/raw/13ed3d1e701994640b8d8065b835f8a9684ece92/" "privacy_badger_recording_nontracking_domains_fixture.html" ) TRACKING_DOMAIN = "dnt-request-cookies-test.trackersimulator.org" NON_TRACKING_DOMAIN = "dnt-test.trackersimulator.org" # open Badger's background page self.load_url(self.bg_url, wait_on_site=1) # need to keep Badger's background page open to record what's happening # so, open and switch to a new window self.open_window() # visit a page containing two third-party resources, # one from a cookie-tracking domain # and one from a non-tracking domain self.load_url(TEST_URL) # switch back to Badger's background page switch_to_window_with_url(self.driver, self.bg_url) # verify that the cookie-tracking domain was recorded self.assertTrue( self.domain_was_recorded(TRACKING_DOMAIN), "Tracking domain should have gotten recorded" ) # verify that the non-tracking domain was not recorded self.assertFalse( self.domain_was_recorded(NON_TRACKING_DOMAIN), "Non-tracking domain should not have gotten recorded" )
def test_donate_button(self): """Ensure donate button opens EFF website.""" EFF_URL = "https://supporters.eff.org/donate/support-privacy-badger" self.open_popup() try: donate_button = self.driver.find_element_by_id("donate") except NoSuchElementException: self.fail("Unable to find donate button on popup") donate_button.click() # Make sure EFF website not opened in same window. if self.driver.current_url != self.popup_url: self.fail("EFF website not opened in new window") # Look for EFF website and return if found. switch_to_window_with_url(self.driver, EFF_URL) self.wait_for_page_to_start_loading(EFF_URL) self.assertEqual( self.driver.current_url, EFF_URL, "EFF website should open after clicking donate button on popup")
def test_welcome_page_reminder_overlay(self): """Ensure overlay links to first run comic.""" self.open_popup(show_nag=True) self.driver.find_element_by_id("intro-reminder-btn").click() # Look for first run page and return if found. switch_to_window_with_url(self.driver, self.first_run_url)
def test_dnt_check_should_happen_for_blocked_domains(self): PAGE_URL = ( "https://gitcdn.link/cdn/ghostwords/" "74585c942a918509b20bf2db5659646e/raw/2401659e678442de6309339882f19fbb21dbc959/" "privacy_badger_dnt_test_fixture.html" ) DNT_DOMAIN = "www.eff.org" BLOCK_DOMAIN_JS = ( "(function () {" "chrome.extension.getBackgroundPage()." "badger.storage.setupHeuristicAction(" " arguments[0]," " chrome.extension.getBackgroundPage().constants.BLOCK" ");" "}());" ) # mark a DNT-compliant domain for blocking self.load_url(self.options_url) self.js(BLOCK_DOMAIN_JS, DNT_DOMAIN) # visit a page that loads a resource from that DNT-compliant domain self.open_window() self.load_url(PAGE_URL) # switch back to Badger's options page switch_to_window_with_url(self.driver, self.options_url) # verify that the domain is blocked self.assertTrue(self.domain_was_detected(DNT_DOMAIN), msg="Domain should have been detected.") self.assertTrue(self.domain_was_blocked(DNT_DOMAIN), msg="DNT-compliant resource should have been blocked at first.") def reload_and_see_if_unblocked(): # switch back to the page with the DNT-compliant resource switch_to_window_with_url(self.driver, PAGE_URL) # reload it self.load_url(PAGE_URL) # switch back to Badger's options page switch_to_window_with_url(self.driver, self.options_url) return ( self.domain_was_detected(DNT_DOMAIN) and self.domain_was_blocked(DNT_DOMAIN) ) # verify that the domain is allowed was_blocked = retry_until( reload_and_see_if_unblocked, tester=lambda x: not x, msg="Waiting a bit for DNT check to complete and retrying ...") self.assertFalse(was_blocked, msg="DNT-compliant resource should have gotten unblocked.")
def test_dnt_check_should_happen_for_blocked_domains(self): PAGE_URL = ( "https://gistcdn.githack.com/ghostwords/" "74585c942a918509b20bf2db5659646e/raw/2401659e678442de6309339882f19fbb21dbc959/" "privacy_badger_dnt_test_fixture.html") DNT_DOMAIN = "www.eff.org" BLOCK_DOMAIN_JS = ( "(function () {" "chrome.extension.getBackgroundPage()." "badger.storage.setupHeuristicAction(" " arguments[0]," " chrome.extension.getBackgroundPage().constants.BLOCK" ");" "}());") # mark a DNT-compliant domain for blocking self.load_url(self.options_url) self.js(BLOCK_DOMAIN_JS, DNT_DOMAIN) # visit a page that loads a resource from that DNT-compliant domain self.open_window() self.load_url(PAGE_URL) # switch back to Badger's options page switch_to_window_with_url(self.driver, self.options_url) # verify that the domain is blocked self.assertTrue(self.domain_was_detected(DNT_DOMAIN), msg="Domain should have been detected.") self.assertTrue( self.domain_was_blocked(DNT_DOMAIN), msg="DNT-compliant resource should have been blocked at first.") def reload_and_see_if_unblocked(): # switch back to the page with the DNT-compliant resource switch_to_window_with_url(self.driver, PAGE_URL) # reload it self.load_url(PAGE_URL) # switch back to Badger's options page switch_to_window_with_url(self.driver, self.options_url) return (self.domain_was_detected(DNT_DOMAIN) and self.domain_was_blocked(DNT_DOMAIN)) # verify that the domain is allowed was_blocked = retry_until( reload_and_see_if_unblocked, tester=lambda x: not x, msg="Waiting a bit for DNT check to complete and retrying ...") self.assertFalse( was_blocked, msg="DNT-compliant resource should have gotten unblocked.")
def reload_and_see_if_unblocked(): # switch back to the page with the DNT-compliant resource switch_to_window_with_url(self.driver, PAGE_URL) # reload it self.load_url(PAGE_URL) # switch back to Badger's background page switch_to_window_with_url(self.driver, self.bg_url) return self.domain_was_blocked(DNT_DOMAIN)
def reload_and_see_if_unblocked(): # switch back to the page with the DNT-compliant resource switch_to_window_with_url(self.driver, PAGE_URL) # reload it self.load_url(PAGE_URL) # switch back to Badger's options page switch_to_window_with_url(self.driver, self.options_url) return (self.domain_was_detected(DNT_DOMAIN) and self.domain_was_blocked(DNT_DOMAIN))
def load_pb_ui(self, target_url): """Show the PB popup as a new tab. If Selenium would let us just programmatically launch an extension from its icon, we wouldn't need this method. Alas it will not. But! We can open a new tab and set the url to the extension's popup html page and test away. That's how most devs test extensions. But**2!! PB's popup code uses the current tab's url to report the current tracker status. And since we changed the current tab's url when we loaded the popup as a tab, the popup loses all the blocker status information from the original tab. The workaround is to execute a new convenience function in the popup codebase that looks for a given url in the tabs and, if it finds a match, refreshes the popup with the associated tabid. Then the correct status information will be displayed in the popup.""" self.open_window() self.load_url(self.popup_url) # get the popup populated with status information for the correct url window_utils.switch_to_window_with_url(self.driver, self.popup_url) self.js( """ /** * if the query url pattern matches a tab, switch the module's tab object to that tab */ (function (url) { chrome.tabs.query({url}, function (tabs) { if (!tabs || !tabs.length) { return; } chrome.runtime.sendMessage({ type: "getPopupData", tabId: tabs[0].id, tabUrl: tabs[0].url }, (response) => { setPopupData(response); refreshPopup(); window.DONE_REFRESHING = true; }); }); }(arguments[0]));""", target_url) # wait for popup to be ready self.wait_for_script( "return typeof window.DONE_REFRESHING != 'undefined' &&" "window.POPUP_INITIALIZED &&" "window.SLIDERS_DONE") self.get_tracker_state()
def test_dnt_check_should_happen_for_blocked_domains(self): PAGE_URL = ( "https://cdn.rawgit.com/ghostwords/" "74585c942a918509b20bf2db5659646e/raw/f42d25717e5b4f735c7affa527a2e0b62286c005/" "privacy_badger_dnt_test_fixture.html") DNT_DOMAIN = "www.eff.org" BLOCK_DOMAIN_JS = """(function () {{ badger.storage.setupHeuristicAction('{}', constants.BLOCK); }}());""".format(DNT_DOMAIN) # mark a DNT-compliant domain for blocking self.load_url(self.bg_url) self.js(BLOCK_DOMAIN_JS) # need to keep Badger's background page open for our changes to persist # so, either open and switch to a new window, # or just reuse the already-open new user welcome window switch_to_window_with_url(self.driver, self.first_run_url) # visit a page that loads a resource from that DNT-compliant domain self.load_url(PAGE_URL) # switch back to Badger's background page switch_to_window_with_url(self.driver, self.bg_url) # verify that the domain is blocked self.assertTrue( self.domain_was_blocked(DNT_DOMAIN), msg="DNT-compliant resource should have been blocked at first.") def reload_and_see_if_unblocked(): # switch back to the page with the DNT-compliant resource switch_to_window_with_url(self.driver, PAGE_URL) # reload it self.load_url(PAGE_URL) # switch back to Badger's background page switch_to_window_with_url(self.driver, self.bg_url) return self.domain_was_blocked(DNT_DOMAIN) # verify that the domain is allowed was_blocked = retry_until( reload_and_see_if_unblocked, tester=lambda x: not x, msg="Waiting a bit for DNT check to complete and retrying ...") self.assertFalse( was_blocked, msg="DNT-compliant resource should have gotten unblocked.")
def load_pb_ui(self, target_scheme_and_host): """Show the PB popup as a new tab. If Selenium would let us just programmatically launch an extension from its icon, we wouldn't need this method. Alas it will not. But! We can open a new tab and set the url to the extension's popup html page and test away. That's how most devs test extensions. But**2!! PB's popup code uses the current tab's url to report the current tracker status. And since we changed the current tab's url when we loaded the popup as a tab, the popup loses all the blocker status information from the original tab. The workaround is to execute a new convenience function in the popup codebase that looks for a given url in the tabs and, if it finds a match, refreshes the popup with the associated tabid. Then the correct status information will be displayed in the popup.""" self.open_window() self.load_url(self.popup_url) # get the popup populated with status information for the correct url window_utils.switch_to_window_with_url(self.driver, self.popup_url) target_url = target_scheme_and_host + "/*" javascript_src = """/** * if the query url pattern matches a tab, switch the module's tab object to that tab */ function setTabToUrl(query_url) { chrome.tabs.query({url: query_url}, function (tabs) { if (!tabs || !tabs.length) { return; } chrome.runtime.sendMessage({ type: "getPopupData", tabId: tabs[0].id, tabUrl: tabs[0].url }, (response) => { setPopupData(response); refreshPopup(); window.DONE_REFRESHING = true; }); }); }""" self.js(javascript_src + "setTabToUrl('{}');".format(target_url)) # wait for popup to be ready self.wait_for_script( "return typeof window.DONE_REFRESHING != 'undefined' &&" "window.POPUP_INITIALIZED &&" "window.SLIDERS_DONE" )
def reload_and_see_if_unblocked(): # switch back to the page with the DNT-compliant resource switch_to_window_with_url(self.driver, PAGE_URL) # reload it self.load_url(PAGE_URL) # switch back to Badger's options page switch_to_window_with_url(self.driver, self.options_url) return ( self.domain_was_detected(DNT_DOMAIN) and self.domain_was_blocked(DNT_DOMAIN) )
def test_help_button(self): """Ensure FAQ website is opened when help button is clicked.""" FAQ_URL = "https://privacybadger.org/#faq" try: switch_to_window_with_url(self.driver, FAQ_URL, max_tries=1) except WindowNotFoundException: pass else: self.fail("FAQ should not already be open") self.open_popup() self.driver.find_element_by_id("help").click() switch_to_window_with_url(self.driver, FAQ_URL)
def test_dnt_check_should_happen_for_blocked_domains(self): PAGE_URL = ( "https://efforg.github.io/privacybadger-test-fixtures/html/" "dnt.html") DNT_DOMAIN = "www.eff.org" # mark a DNT-compliant domain for blocking self.block_domain(DNT_DOMAIN) # visit a page that loads a resource from that DNT-compliant domain self.open_window() self.load_url(PAGE_URL) # switch back to Badger's options page switch_to_window_with_url(self.driver, self.options_url) # verify that the domain is blocked self.assertTrue(self.domain_was_detected(DNT_DOMAIN), msg="Domain should have been detected.") self.assertTrue( self.domain_was_blocked(DNT_DOMAIN), msg="DNT-compliant resource should have been blocked at first.") def reload_and_see_if_unblocked(): # switch back to the page with the DNT-compliant resource switch_to_window_with_url(self.driver, PAGE_URL) # reload it self.load_url(PAGE_URL) # switch back to Badger's options page switch_to_window_with_url(self.driver, self.options_url) return (self.domain_was_detected(DNT_DOMAIN) and self.domain_was_blocked(DNT_DOMAIN)) # verify that the domain is allowed was_blocked = retry_until( reload_and_see_if_unblocked, tester=lambda x: not x, msg="Waiting a bit for DNT check to complete and retrying ...") self.assertFalse( was_blocked, msg="DNT-compliant resource should have gotten unblocked.")
def test_trackers_link(self): """Ensure trackers link opens EFF website.""" EFF_URL = "https://www.eff.org/privacybadger/faq#What-is-a-third-party-tracker" self.open_popup() # Get all possible tracker links (none, one, multiple) trackers_links = self.driver.find_elements_by_css_selector( "#pbInstructions a") if not trackers_links: self.fail("Unable to find trackers link on popup") # Get the one that's displayed on the page that this test is using for link in trackers_links: if link.is_displayed(): trackers_link = link trackers_link.click() # Make sure EFF website not opened in same window. if self.driver.current_url != self.popup_url: self.fail("EFF website not opened in new window") # Look for EFF website and return if found. switch_to_window_with_url(self.driver, EFF_URL) self.wait_for_page_to_start_loading(EFF_URL) self.assertEqual( self.driver.current_url, EFF_URL, "EFF website should open after clicking trackers link on popup") # Verify EFF website contains the linked anchor element. faq_selector = 'a[href="{}"]'.format(EFF_URL[EFF_URL.index('#'):]) try: WebDriverWait(self.driver, pbtest.SEL_DEFAULT_WAIT_TIMEOUT).until( expected_conditions.presence_of_element_located( (By.CSS_SELECTOR, faq_selector))) except TimeoutException: self.fail( "Unable to find expected element ({}) on EFF website".format( faq_selector))
def load_pb_ui(self, target_scheme_and_host): """Show the PB popup as a new tab. If Selenium would let us just programmatically launch an extension from its icon, we wouldn't need this method. Alas it will not. But! We can open a new tab and set the url to the extension's popup html page and test away. That's how most devs test extensions. But**2!! PB's popup code uses the current tab's url to report the current tracker status. And since we changed the current tab's url when we loaded the popup as a tab, the popup loses all the blocker status information from the original tab. The workaround is to execute a new convenience function in the popup codebase that looks for a given url in the tabs and, if it finds a match, refreshes the popup with the associated tabid. Then the correct status information will be displayed in the popup.""" self.open_window() self.load_url(self.popup_url) # get the popup populated with status information for the correct url window_utils.switch_to_window_with_url(self.driver, self.popup_url) target_url = target_scheme_and_host + "/*" javascript_src = """/** * if the query url pattern matches a tab, switch the module's tab object to that tab */ function setTabToUrl(query_url) { chrome.tabs.query({url: query_url}, function (tabs) { if (!tabs || !tabs.length) { return; } chrome.runtime.sendMessage({ type: "getPopupData", tabId: tabs[0].id, tabUrl: tabs[0].url }, (response) => { setPopupData(response); refreshPopup(); }); }); }""" self.js(javascript_src + "setTabToUrl('{}');".format(target_url))
def load_pb_ui(self, target_scheme_and_host ): """Show the PB popup as a new tab. If Selenium would let us just programmatically launch an extension from its icon, we wouldn't need this method. Alas it will not. But! We can open a new tab and set the url to the extension's popup html page and test away. That's how most devs test extensions. But**2!! PB's popup code uses the current tab's url to report the current tracker status. And since we changed the current tab's url when we loaded the popup as a tab, the popup loses all the blocker status information from the original tab. The workaround is to execute a new convenience function in the popup codebase that looks for a given url in the tabs and, if it finds a match, refreshes the popup with the associated tabid. Then the correct status information will be displayed in the popup.""" # open new tab with the PB UI # note: there's a selenium + chromedriver + mac bug where you can't use the command key to # open a new tab. And if you inject javascript to open a window and you have the # popup blocker turned on, it won't work. Workaround: embed a button on the target page # that opens a new window when clicked. window_utils.switch_to_window_with_url( self.driver, target_scheme_and_host ) button = self.driver.find_element_by_id("newwindowbutton") button.click() window_utils.switch_to_window_with_url( self.driver, "about:blank" ) self.load_url(PB_CHROME_PU_URL) # use the new convenience function to get the popup populated with status information for the correct url window_utils.switch_to_window_with_url( self.driver, PB_CHROME_PU_URL ) target_url = target_scheme_and_host + "/*" javascript_src = "setTabToUrl('" + target_url + "');" self.js( javascript_src )
def test_trackers_link(self): """Ensure trackers link opens EFF website.""" EFF_URL = "https://www.eff.org/privacybadger/faq#What-is-a-third-party-tracker" self.open_popup() # Get all possible tracker links (none, one, multiple) trackers_links = self.driver.find_elements_by_css_selector("#pbInstructions a") if not trackers_links: self.fail("Unable to find trackers link on popup") # Get the one that's displayed on the page that this test is using for link in trackers_links: if link.is_displayed(): trackers_link = link trackers_link.click() # Make sure EFF website not opened in same window. if self.driver.current_url != self.popup_url: self.fail("EFF website not opened in new window") # Look for EFF website and return if found. switch_to_window_with_url(self.driver, EFF_URL) self.wait_for_page_to_start_loading(EFF_URL) self.assertEqual(self.driver.current_url, EFF_URL, "EFF website should open after clicking trackers link on popup") # Verify EFF website contains the linked anchor element. faq_selector = 'a[href="{}"]'.format(EFF_URL[EFF_URL.index('#'):]) try: WebDriverWait(self.driver, pbtest.SEL_DEFAULT_WAIT_TIMEOUT).until( expected_conditions.presence_of_element_located( (By.CSS_SELECTOR, faq_selector))) except TimeoutException: self.fail("Unable to find expected element ({}) on EFF website".format(faq_selector))
def test_donate_button(self): """Ensure donate button opens EFF website.""" EFF_URL = "https://supporters.eff.org/donate/support-privacy-badger" self.open_popup() donate_button = self.driver.find_element_by_id("donate") donate_button.click() # Make sure EFF website not opened in same window. if self.driver.current_url != self.popup_url: self.fail("EFF website not opened in new window") # Look for EFF website and return if found. switch_to_window_with_url(self.driver, EFF_URL) self.wait_for_page_to_start_loading(EFF_URL) self.assertEqual(self.driver.current_url, EFF_URL, "EFF website should open after clicking donate button on popup")
def test_ga_js_surrogate(self): # verify site loads self.assertTrue(self.load_ga_js_test_page()) # block ga.js (known to break the site) self.load_url(pbtest.PB_CHROME_BG_URL, wait_on_site=1) ga_backup = self.js( "badger.saveAction('block', 'www.google-analytics.com');" "const sdb = require('surrogatedb');" "return JSON.stringify(sdb.hostnames['www.google-analytics.com']);" ) # and disable surrogate self.js( "const sdb = require('surrogatedb');" "delete sdb.hostnames['www.google-analytics.com'];" ) # need to keep PB's background page open for our changes to persist ... # so, open and switch to a new window self.open_window() # verify site breaks self.assertFalse(self.load_ga_js_test_page()) # switch back to PB's background page switch_to_window_with_url(self.driver, pbtest.PB_CHROME_BG_URL) # re-enable surrogate self.js( "const sdb = require('surrogatedb');" "sdb.hostnames['www.google-analytics.com'] = JSON.parse('%s');" % ga_backup ) # still need to keep PB's bg page open ... self.open_window() # verify site loads again self.assertTrue(self.load_ga_js_test_page())
def test_canvas_fingerprinting_detection(self): PAGE_URL = ( "https://cdn.rawgit.com/ghostwords" "/ff6347b93ec126d4f73a9ddfd8b09919/raw/2332f82d3982bd4a84cd2380aed90228955d1f2a" "/privacy_badger_fingerprint_test_fixture.html" ) FINGERPRINTING_DOMAIN = "cdn.jsdelivr.net" # open Badger's background page self.load_url(self.bg_url, wait_on_site=1) # need to keep Badger's background page open for tabData to persist # so, open and switch to a new window self.open_window() # visit the page self.load_url(PAGE_URL) # switch back to Badger's background page switch_to_window_with_url(self.driver, self.bg_url) # check that we detected the fingerprinting domain as a tracker has_tracking = self.detected_tracking(FINGERPRINTING_DOMAIN, PAGE_URL) # work around sporadic failures on some versions of Firefox if pbtest.shim.browser_type == 'firefox' and not has_tracking: print("\nRetrying canvas fingerprinting detection check ...") sleep(2) has_tracking = self.detected_tracking(FINGERPRINTING_DOMAIN, PAGE_URL) self.assertTrue(has_tracking, "Canvas fingerprinting resource was detected as a tracker.") # check that we detected canvas fingerprinting self.assertTrue( self.detected_fingerprinting(FINGERPRINTING_DOMAIN), "Canvas fingerprinting resources was detected as a fingerprinter." )
def test_ga_js_surrogate(self): # verify site loads self.assertTrue(self.load_ga_js_test_page()) # block ga.js (known to break the site) self.load_url(self.bg_url, wait_on_site=1) ga_backup = self.js( "badger.saveAction('block', 'www.google-analytics.com');" "const sdb = require('surrogatedb');" "return JSON.stringify(sdb.hostnames['www.google-analytics.com']);" ) # and disable surrogate self.js("const sdb = require('surrogatedb');" "delete sdb.hostnames['www.google-analytics.com'];") # need to keep PB's background page open for our changes to persist ... # so, open and switch to a new window self.open_window() # verify site breaks self.assertFalse(self.load_ga_js_test_page()) # switch back to PB's background page switch_to_window_with_url(self.driver, self.bg_url) # re-enable surrogate self.js( "const sdb = require('surrogatedb');" "sdb.hostnames['www.google-analytics.com'] = JSON.parse('%s');" % ga_backup) # still need to keep PB's bg page open ... self.open_window() # verify site loads again self.assertTrue(self.load_ga_js_test_page())
def test_dnt_check_should_happen_for_blocked_domains(self): PAGE_URL = ( """https://cdn.rawgit.com/ghostwords/74585c942a918509b20bf2db5659646e/raw/f42d25717e5b4f735c7affa527a2e0b62286c005/privacy_badger_dnt_test_fixture.html""" ) DNT_DOMAIN = "www.eff.org" BLOCK_DOMAIN_JS = """(function () {{ badger.storage.setupHeuristicAction('{}', constants.BLOCK); }}());""".format(DNT_DOMAIN) # mark a DNT-compliant domain for blocking self.load_url(self.bg_url, wait_on_site=1) self.js(BLOCK_DOMAIN_JS) # need to keep Badger's background page open for our changes to persist # so, open and switch to a new window self.open_window() # visit a page that loads a resource from that DNT-compliant domain self.load_url(PAGE_URL) # switch back to Badger's background page switch_to_window_with_url(self.driver, self.bg_url) # verify that the domain is blocked self.assertTrue( self.domain_was_blocked(DNT_DOMAIN), msg="DNT-compliant resource should have been blocked at first.") # switch back to the page with the DNT-compliant resource switch_to_window_with_url(self.driver, PAGE_URL) # reload it self.load_url(PAGE_URL) # switch back to Badger's background page switch_to_window_with_url(self.driver, self.bg_url) # verify that the domain is allowed for i in range(5): was_blocked = self.domain_was_blocked(DNT_DOMAIN) if not was_blocked: # success break print("\nWaiting a bit for DNT check to complete and retrying ...") sleep(2**i) self.assertFalse( was_blocked, msg="DNT-compliant resource should have gotten unblocked.")
def load_pb_ui(self, target_scheme_and_host): """Show the PB popup as a new tab. If Selenium would let us just programmatically launch an extension from its icon, we wouldn't need this method. Alas it will not. But! We can open a new tab and set the url to the extension's popup html page and test away. That's how most devs test extensions. But**2!! PB's popup code uses the current tab's url to report the current tracker status. And since we changed the current tab's url when we loaded the popup as a tab, the popup loses all the blocker status information from the original tab. The workaround is to execute a new convenience function in the popup codebase that looks for a given url in the tabs and, if it finds a match, refreshes the popup with the associated tabid. Then the correct status information will be displayed in the popup.""" # open new tab with the PB UI # note: there's a selenium + chromedriver + mac bug where you can't use the command key to # open a new tab. And if you inject javascript to open a window and you have the # popup blocker turned on, it won't work. Workaround: embed a button on the target page # that opens a new window when clicked. window_utils.switch_to_window_with_url(self.driver, target_scheme_and_host) button = self.driver.find_element_by_id("newwindowbutton") button.click() window_utils.switch_to_window_with_url(self.driver, "about:blank") self.load_url(self.popup_url) # get the popup populated with status information for the correct url window_utils.switch_to_window_with_url(self.driver, self.popup_url) target_url = target_scheme_and_host + "/*" javascript_src = """/** * if the query url pattern matches a tab, switch the module's tab object to that tab * Convenience function for the test harness * Chrome URL pattern docs: https://developer.chrome.com/extensions/match_patterns */ function setTabToUrl(query_url) { chrome.tabs.query( {url: query_url}, function(ta) { if (typeof ta == "undefined") { return; } if (ta.length === 0) { return; } var tabid = ta[0].id; refreshPopup(tabid); }); }""" self.js(javascript_src + "setTabToUrl('{}');".format(target_url))
def test_options_button(self): """Ensure options page is opened when button is clicked.""" self.open_popup() self.driver.find_element_by_id("options").click() switch_to_window_with_url(self.driver, self.options_url)
def test_ga_js_surrogate(self): # open the background page self.load_url(self.bg_url) # verify the surrogate is present self.assertTrue( self. js("const sdb = require('surrogatedb');" "return sdb.hostnames.hasOwnProperty('www.google-analytics.com');" ), "Surrogate is missing but should be present.") # verify site loads self.assertTrue(self.load_ga_js_test_page(), "Page failed to load even before we did anything.") # block ga.js (known to break the site) self.load_url(self.bg_url) # also back up the surrogate definition before removing it ga_backup = self.js( "badger.heuristicBlocking.blacklistOrigin('www.google-analytics.com', 'google-analytics.com');" "const sdb = require('surrogatedb');" "return JSON.stringify(sdb.hostnames['www.google-analytics.com']);" ) # now remove the surrogate self.js("const sdb = require('surrogatedb');" "delete sdb.hostnames['www.google-analytics.com'];") # wait until this happens self.wait_for_script( "const sdb = require('surrogatedb');" "return !sdb.hostnames.hasOwnProperty('www.google-analytics.com');", timeout=5, message="Timed out waiting for surrogate to get removed.") # need to keep PB's background page open for our changes to persist ... # so, open and switch to a new window self.open_window() # verify site breaks self.assertFalse( self.load_ga_js_test_page(), "Page loaded successfully when it should have failed.") # switch back to PB's background page switch_to_window_with_url(self.driver, self.bg_url) # re-enable surrogate self.js( "const sdb = require('surrogatedb');" "sdb.hostnames['www.google-analytics.com'] = JSON.parse('%s');" % ga_backup) # wait until this happens self.wait_for_script( "const sdb = require('surrogatedb');" "return sdb.hostnames.hasOwnProperty('www.google-analytics.com');", timeout=5, message="Timed out waiting for surrogate to get readded.") # still need to keep PB's bg page open ... self.open_window() # verify site loads again self.assertTrue(retry_until(self.load_ga_js_test_page), "Page failed to load after surrogation.")