test_network_check.py (2547B)
1 from marionette_driver import By, Wait, errors 2 from marionette_driver.localization import L10n 3 from marionette_harness import MarionetteTestCase 4 5 NETWORK_CHECK_URL = "https://check.torproject.org/" 6 TOR_BOOTSTRAP_TIMEOUT = 30 # 30s 7 8 STRINGS_LOCATION = "chrome://torbutton/locale/torConnect.properties" 9 10 11 class TestNetworkCheck(MarionetteTestCase): 12 def setUp(self): 13 MarionetteTestCase.setUp(self) 14 15 self.l10n = L10n(self.marionette) 16 17 def tearDown(self): 18 self.marionette.restart(in_app=False, clean=True) 19 super().tearDown() 20 21 def attemptConnection(self, tries=1): 22 if tries > 3: 23 self.assertTrue(False, "Failed to connect to Tor after 3 attempts") 24 25 connectBtn = self.marionette.find_element(By.ID, "connectButton") 26 Wait(self.marionette, timeout=10).until( 27 lambda _: connectBtn.is_displayed(), 28 message="Timed out waiting for tor connect button to show up.", 29 ) 30 connectBtn.click() 31 32 try: 33 34 def check(m): 35 if not m.get_url().startswith("about:torconnect"): 36 # We have finished connecting and have been redirected. 37 return True 38 39 try: 40 heading = self.marionette.find_element(By.ID, "tor-connect-heading") 41 except errors.NoSuchElementException: 42 # Page is probably redirecting. 43 return False 44 45 if heading.text not in [ 46 self.l10n.localize_property( 47 [STRINGS_LOCATION], "torConnect.torConnecting" 48 ), 49 self.l10n.localize_property( 50 [STRINGS_LOCATION], "torConnect.torConnected" 51 ), 52 ]: 53 raise ValueError("Tor connect page is not connecting or connected") 54 55 return False 56 57 Wait(self.marionette, timeout=TOR_BOOTSTRAP_TIMEOUT).until(check) 58 except (errors.TimeoutException, ValueError): 59 cancelBtn = self.marionette.find_element(By.ID, "cancelButton") 60 if cancelBtn.is_displayed(): 61 cancelBtn.click() 62 63 self.attemptConnection(tries + 1) 64 65 def test_network_check(self): 66 self.attemptConnection() 67 self.marionette.navigate(NETWORK_CHECK_URL) 68 self.assertRegex( 69 self.marionette.title, 70 r"^Congratulations\.", 71 f"{NETWORK_CHECK_URL} should have the expected title.", 72 )