Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium tests fail for all URLs with seemingly random errors

Tags:

I'm using Ubuntu 16.04. I've set up Django 1.9.7 and selenium 2.53.5 in a virtual environment.

I'm following Harry J.W. Percival's book "Test Driven Development with Python", and i'm currently on the first tutorial ("Using Selenium to Test User Interactions") for functional tests in chapter 4. The code goes 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's 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 tracebacks, 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 prior to these errors, selenium would randomly show the Remote end closed connection and then work error free if the command was executed again, with no changes to the code.

Can somebody explain what's going on?

like image 348
Aditya Srivastava Avatar asked Jun 14 '16 14:06

Aditya Srivastava


1 Answers

The simple answer is you'll either have to downgrade Firefox to 46 or install the Marionette Web Driver. I've had similar problems.

Please see the top rated answer on the question below.

Can't open browser with Selenium after Firefox update

like image 89
jharrison12 Avatar answered Oct 14 '22 01:10

jharrison12