def test_unwrapping(self): self.perform_google_search() def _check_results(): # select all anchor elements with non-empty href attributes SELECTOR = "a[href]:not([href=''])" search_results = self.driver.find_elements_by_css_selector( SELECTOR) # remove "About this result" links as they do not get cleaned # (they don't match any of the `trap_link` selectors) # and so they fail the rel check below search_results = [ a for a in search_results if (a.text or a.get_attribute('textContent') != self.SEARCH_RESULT_URL or a.get_attribute('innerHTML') != self.SEARCH_RESULT_URL) ] # verify these appear to be actual search results hrefs = [link.get_attribute('href') for link in search_results] self.assertIn(self.SEARCH_RESULT_URL, hrefs, "At least one search result points to our homepage") # verify that tracking attributes are missing for link in search_results: # only check links that point to our homepage # as there is a mix of search result links and other links # and not all links get cleaned # and it's not clear how to select search result links only href = link.get_attribute('href') if self.SEARCH_RESULT_URL not in href: continue self.assertFalse(link.get_attribute('ping'), "Tracking attribute should be missing") self.assertFalse(link.get_attribute('onmousedown'), "Tracking attribute should be missing") self.assertEqual(link.get_attribute('rel'), "noreferrer noopener") return True time.sleep(1) self.assertTrue( pbtest.retry_until( pbtest.convert_exceptions_to_false(_check_results)), "Search results still fail our checks after several attempts")
def test_no_unwrapping_when_disabled(self): """Tests that Google search result links still match our selectors.""" # use the browser-appropriate selector SELECTOR = "a[ping]" if pbtest.shim.browser_type == "firefox": SELECTOR = "a[onmousedown^='return rwt(this,']" # turn off link unwrapping on Google # so that we can test our selectors self.disable_badger_on_site(self.GOOGLE_SEARCH_DOMAIN) def _perform_search_and_check_results(): self.perform_google_search() search_results = self.driver.find_elements_by_css_selector( SELECTOR) # remove "About this result" links as they do not get cleaned # (they don't match any of the `trap_link` selectors) # and so they fail the rel check below search_results = [ a for a in search_results if (a.text or a.get_attribute('textContent') != self.SEARCH_RESULT_URL or a.get_attribute('innerHTML') != self.SEARCH_RESULT_URL) ] # check the results hrefs = [link.get_attribute('href') for link in search_results] self.assertIn(self.SEARCH_RESULT_URL, hrefs, "At least one search result points to our homepage") return True self.assertTrue( pbtest.retry_until( pbtest.convert_exceptions_to_false( _perform_search_and_check_results)), "Search results still fail our checks after several attempts")