class ShutdownTest(ChromeDriverTest): def setUp(self): super(ShutdownTest, self).setUp() self._custom_server = ChromeDriverLauncher( self.GetDriverPath()).Launch() self._custom_factory = ChromeDriverFactory(self._custom_server, self.GetChromePath()) def tearDown(self): self._custom_server.Kill() super(ShutdownTest, self).tearDown() def testShutdownWithSession(self): driver = self._custom_factory.GetNewDriver() driver.get(self._custom_server.GetUrl() + '/status') driver.find_element_by_tag_name('body') self._custom_server.Kill() def testShutdownWithBusySession(self): def _Hang(driver): """Waits for the process to quit and then notifies.""" try: driver.get(self._custom_server.GetUrl() + '/hang') except httplib.BadStatusLine: pass driver = self._custom_factory.GetNewDriver() wait_thread = threading.Thread(target=_Hang, args=(driver, )) wait_thread.start() wait_thread.join(5) self.assertTrue(wait_thread.isAlive()) self._custom_server.Kill() wait_thread.join(10) self.assertFalse(wait_thread.isAlive())
class UrlBaseTest(unittest.TestCase): """Tests that the server can be configured for a different URL base.""" def setUp(self): self._server = ChromeDriverLauncher(test_paths.CHROMEDRIVER_EXE, url_base='/wd/hub').Launch() def tearDown(self): self._server.Kill() def testCreatingSessionShouldRedirectToCorrectURL(self): request_url = self._server.GetUrl() + '/session' response = SendRequest(request_url, method='POST', data='{"desiredCapabilities":{}}') self.assertEquals(200, response.code) self.session_url = response.geturl() # TODO(jleyba): verify this URL? data = json.loads(response.read()) self.assertTrue(isinstance(data, dict)) self.assertEquals(0, data['status']) url_parts = urlparse.urlparse(self.session_url)[2].split('/') self.assertEquals(5, len(url_parts)) self.assertEquals('', url_parts[0]) self.assertEquals('wd', url_parts[1]) self.assertEquals('hub', url_parts[2]) self.assertEquals('session', url_parts[3]) self.assertEquals(data['sessionId'], url_parts[4])
def testShouldNotServeFilesByDefault(self): server = ChromeDriverLauncher(test_paths.CHROMEDRIVER_EXE).Launch() try: SendRequest(server.GetUrl(), method='GET') self.fail('Should have raised a urllib.HTTPError for returned 403') except urllib2.HTTPError, expected: self.assertEquals(403, expected.code)
def _Run(self): """Run the tests.""" # TODO(kkania): Remove this hack. self._FakePytestHack() test_names = self._GetTestNames(self._args) # The tests expect to run with preset 'driver' and 'webserver' class # properties. server = ChromeDriverLauncher( self._options.driver_exe or test_paths.CHROMEDRIVER_EXE, test_paths.WEBDRIVER_TEST_DATA).Launch() driver = WebDriver(server.GetUrl(), {}) # The tests expect a webserver. Since ChromeDriver also operates as one, # just pass this dummy class with the right info. class DummyWebserver: pass webserver = DummyWebserver() webserver.port = server.GetPort() for test in test_names: Main._SetTestClassAttributes(test, 'driver', driver) Main._SetTestClassAttributes(test, 'webserver', webserver) # Load and run the tests. logging.debug('Loading tests from %s', test_names) loaded_tests = unittest.defaultTestLoader.loadTestsFromNames(test_names) test_suite = unittest.TestSuite() test_suite.addTests(loaded_tests) verbosity = 1 if self._options.verbose: verbosity = 2 result = GTestTextTestRunner(verbosity=verbosity).run(test_suite) server.Kill() sys.exit(not result.wasSuccessful())
class BasicTest(ChromeDriverTest): """Basic ChromeDriver tests.""" def setUp(self): self._server2 = ChromeDriverLauncher(self.GetDriverPath()).Launch() def tearDown(self): self._server2.Kill() def testShouldReturn403WhenSentAnUnknownCommandURL(self): request_url = self._server2.GetUrl() + '/foo' try: SendRequest(request_url, method='GET') self.fail('Should have raised a urllib.HTTPError for returned 403') except urllib2.HTTPError, expected: self.assertEquals(403, expected.code)
def _Run(self): """Run the tests.""" # TODO(kkania): Remove this hack. self._FakePytestHack() # In the webdriver tree, the python 'test' module is moved under the root # 'selenium' one for testing. Here we mimic that by setting the 'selenium' # module's 'test' attribute and adding 'selenium.test' to the system # modules. import selenium import test selenium.test = test sys.modules['selenium.test'] = test # Load and decide which tests to run. test_names = self._GetTestNamesFrom( os.path.join(os.path.dirname(__file__), self.TESTS_FILENAME)) all_tests_suite = unittest.defaultTestLoader.loadTestsFromNames( test_names) filtered_suite = unittest_util.FilterTestSuite(all_tests_suite, self._options.filter) if self._options.list is True: print '\n'.join( unittest_util.GetTestNamesFromSuite(filtered_suite)) sys.exit(0) # The tests expect to run with preset 'driver' and 'webserver' class # properties. driver_exe = self._options.driver_exe or test_paths.CHROMEDRIVER_EXE chrome_exe = self._options.chrome_exe or test_paths.CHROME_EXE if driver_exe is None or not os.path.exists( os.path.expanduser(driver_exe)): raise RuntimeError('ChromeDriver could not be found') if chrome_exe is None or not os.path.exists( os.path.expanduser(chrome_exe)): raise RuntimeError('Chrome could not be found') driver_exe = os.path.expanduser(driver_exe) chrome_exe = os.path.expanduser(chrome_exe) # Increase number of http client threads to 10 to prevent hangs. # The hang seems to occur because Chrome keeps too many multiple # simultaneous connections open to our webserver. server = ChromeDriverLauncher(os.path.expanduser(driver_exe), test_paths.WEBDRIVER_TEST_DATA, http_threads=10).Launch() driver = WebDriver(server.GetUrl(), {'chrome.binary': os.path.expanduser(chrome_exe)}) # The tests expect a webserver. Since ChromeDriver also operates as one, # just pass this dummy class with the right info. class DummyWebserver: pass webserver = DummyWebserver() webserver.port = server.GetPort() for test in unittest_util.GetTestsFromSuite(filtered_suite): test.__class__.driver = driver test.__class__.webserver = webserver verbosity = 1 if self._options.verbose: verbosity = 2 result = unittest_util.TextTestRunner( verbosity=verbosity).run(filtered_suite) server.Kill() sys.exit(not result.wasSuccessful())