Selenium tests fail for all URLs with seemingly random errors

I am using Ubuntu 16.04. I installed Django 1.9.7 and selenium 2.53.5 in a virtual environment.

I follow Harry J. W. Percival book Test Driven Development with Python, and I am currently in the first tutorial (Using Selenium to Test User Interaction) for functional tests in chapter 4. The code is as follows

from selenium import webdriver from selenium.webdriver.common.keys import Keys import unittest class NewVisitorTest(unittest.TestCase): def setUp(self): self.browser = webdriver.Firefox() self.browser.implicitly_wait(3) def tearDown(self): self.browser.quit() def test_can_start_a_list_and_retrieve_it_later(self): # Edith has heard about a cool new online to-do app. She goes to check out its homepage self.browser.get('http://localhost:8000') # She notices the page title and header mention to-do lists self.assertIn('To-Do', self.browser.title) header_text = self.browser.find_element_by_tag_name('h1').text self.assertIn('To-Do', header_text) # She is invited to enter a to-do item straight away inputbox = self.browser.find_element_by_id('id_new_item') self.assertEqual( inputbox.get_attribute('placeholder'), 'Enter a to-do item' ) # She types "Buy peacock feathers" into a text box (Edith hobby is tying fly-fishing lures) inputbox.send_keys('Buy peacock feathers') # When she hits enter, the page updates, and now the page lists "1: Buy peacock feathers" as an item in a to-do list inputbox.send_keys(Keys.ENTER) table = self.browser.find_element_by_id('id_list_table') rows = table.find_elements_by_tag_name('tr') self.assertTrue( any(row.text == '1: Buy peacock feathers' for row in rows) ) # There is still a text box inviting her to add another item. She enters "Use peacock feathers to make a fly" (Edith is very methodical) self.fail('Finish the test!') # The page updates again, and now shows both items on her list # Edith wonders whether the site will remember her list. Then she sees that the site has generated a unique URL for her -- there is some explanatory text to that effect. # She visits that URL - her to-do list is still there. # Satisfied, she goes back to sleep if __name__ == '__main__': unittest.main(warnings='ignore') 

Now, after starting the Django server, if I run the selenium script with python3 functional_tests.py , I get seemingly random error reports with a bunch of traces, three of which are given below

 E ====================================================================== ERROR: test_can_start_a_list_and_retrieve_it_later (__main__.NewVisitorTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "functional_tests.py", line 8, in setUp self.browser = webdriver.Firefox() File "/home/adipanda/.virtualenvs/goat/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py", line 81, in __init__ self.binary, timeout) File "/home/adipanda/.virtualenvs/goat/lib/python3.5/site-packages/selenium/webdriver/firefox/extension_connection.py", line 51, in __init__ self.binary.launch_browser(self.profile, timeout=timeout) File "/home/adipanda/.virtualenvs/goat/lib/python3.5/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 68, in launch_browser self._wait_until_connectable(timeout=timeout) File "/home/adipanda/.virtualenvs/goat/lib/python3.5/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 98, in _wait_until_connectable raise WebDriverException("The browser appears to have exited " selenium.common.exceptions.WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details. ---------------------------------------------------------------------- Ran 1 test in 3.081s FAILED (errors=1) 

or

 E ====================================================================== ERROR: test_can_start_a_list_and_retrieve_it_later (__main__.NewVisitorTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "functional_tests.py", line 9, in setUp self.browser.implicitly_wait(3) File "/home/adipanda/.virtualenvs/goat/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 689, in implicitly_wait self.execute(Command.IMPLICIT_WAIT, {'ms': float(time_to_wait) * 1000}) File "/home/adipanda/.virtualenvs/goat/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 234, in execute response = self.command_executor.execute(driver_command, params) File "/home/adipanda/.virtualenvs/goat/lib/python3.5/site-packages/selenium/webdriver/remote/remote_connection.py", line 401, in execute return self._request(command_info[0], url, body=data) File "/home/adipanda/.virtualenvs/goat/lib/python3.5/site-packages/selenium/webdriver/remote/remote_connection.py", line 432, in _request resp = self._conn.getresponse() File "/usr/lib/python3.5/http/client.py", line 1197, in getresponse response.begin() File "/usr/lib/python3.5/http/client.py", line 297, in begin version, status, reason = self._read_status() File "/usr/lib/python3.5/http/client.py", line 266, in _read_status raise RemoteDisconnected("Remote end closed connection without" http.client.RemoteDisconnected: Remote end closed connection without response ---------------------------------------------------------------------- Ran 1 test in 2.437s FAILED (errors=1) 

or

 E ====================================================================== ERROR: test_can_start_a_list_and_retrieve_it_later (__main__.NewVisitorTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "functional_tests.py", line 20, in test_can_start_a_list_and_retrieve_it_later header_text = self.browser.find_element_by_tag_name('h1').text File "/home/adipanda/.virtualenvs/goat/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 389, in find_element_by_tag_name return self.find_element(by=By.TAG_NAME, value=name) File "/home/adipanda/.virtualenvs/goat/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 745, in find_element {'using': by, 'value': value})['value'] File "/home/adipanda/.virtualenvs/goat/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 234, in execute response = self.command_executor.execute(driver_command, params) File "/home/adipanda/.virtualenvs/goat/lib/python3.5/site-packages/selenium/webdriver/remote/remote_connection.py", line 401, in execute return self._request(command_info[0], url, body=data) File "/home/adipanda/.virtualenvs/goat/lib/python3.5/site-packages/selenium/webdriver/remote/remote_connection.py", line 432, in _request resp = self._conn.getresponse() File "/usr/lib/python3.5/http/client.py", line 1197, in getresponse response.begin() File "/usr/lib/python3.5/http/client.py", line 297, in begin version, status, reason = self._read_status() File "/usr/lib/python3.5/http/client.py", line 266, in _read_status raise RemoteDisconnected("Remote end closed connection without" http.client.RemoteDisconnected: Remote end closed connection without response ---------------------------------------------------------------------- Ran 1 test in 3.693s 

Even before these errors, selenium will randomly show Remote end closed connection , and then work without errors if the command was executed again, without changing the code.

Can someone explain what is happening?

+5
source share
1 answer

The simple answer is either switching to Firefox up to 46, or installing the Marionette Web driver. I had similar problems.

Please see the most appropriate answer to the question below.

Unable to open browser with Selenium after updating Firefox

+4
source

All Articles