我正在使用Ubuntu 16.04。我在虚拟环境中设置了Django 1.9.7和selenium 2.53.5。
我正在关注Harry J.W. Percival的书“使用Python进行测试驱动开发”,我目前正在第4章中进行功能测试的第一个教程(“使用Selenium测试用户交互”)。代码如下:
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')
现在,在启动Django服务器之后,如果我使用python3 functional_tests.py
运行selenium脚本,我会看到带有一堆追溯的看似随机的错误报告,其中三个在下面给出
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)
或
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)
或
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
即使在这些错误发生之前,selenium会随机显示Remote end closed connection
,然后再次执行该命令时无错误工作,而不会更改代码。
有人可以解释发生了什么吗?
答案 0 :(得分:4)
简单的答案是你要么必须将Firefox降级到46或者安装Marionette Web Driver。我有类似的问题。
请在下面的问题中查看最受好评的答案。