我正处于使用Django开发webapp的开始阶段。 我在Vagrant中使用无头硒设置。
访问页面工作正常但在下面的测试中它只是挂起。 我不确定这是否是由于selenium和我的firefox版本之间的兼容性。
我的测试环境的pip3冻结:
coverage==4.0.3
defusedxml==0.4.1
Django==1.9.1
django-allauth==0.24.1
oauthlib==1.0.3
psycopg2==2.6.1
python3-openid==3.0.9
requests==2.9.1
requests-oauthlib==0.6.0
selenium==2.49.0
wheel==0.24.0
xvfbwrapper==0.2.7
firefox -v
Mozilla Firefox 43.0.4
我将从我编写的用于测试allauth(Google登录)的类开始,而不是挂起的特定函数。
# -*- coding: utf-8 -*-
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from django.core.urlresolvers import reverse
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from xvfbwrapper import Xvfb
class TestGoogleLogin(StaticLiveServerTestCase):
def setUp(self):
self.vdisplay = Xvfb()
self.vdisplay.start()
self.browser = webdriver.Firefox()
self.browser.maximize_window()
self.browser.implicitly_wait(3000)
self.browser.wait = WebDriverWait(self.browser, 5)
def tearDown(self):
self.browser.quit()
self.vdisplay.stop()
def get_element_by_id(self, element_id):
return self.browser.wait.until(EC.presence_of_element_located(
(By.ID, element_id)))
def get_button_by_id(self, element_id):
return self.browser.wait.until(EC.element_to_be_clickable(
(By.ID, element_id)))
def get_full_url(self, namespace):
return self.live_server_url + reverse(namespace)
以下测试挂起。
def test_google_login(self):
self.browser.get(self.get_full_url("home"))
google_login = self.get_element_by_id("google_login")
with self.assertRaises(TimeoutException):
self.get_element_by_id("logout")
self.assertEqual(
google_login.get_attribute("href"),
self.live_server_url + "/accounts/google/login")
google_login.click()
with self.assertRaises(TimeoutException):
self.get_element_by_id("google_login")
google_logout = self.get_element_by_id("logout")
google_logout.click()
google_login = self.get_element_by_id("google_login")
奇怪的是,如果我把所有东西拿走,只运行它传递的前两句话。 但是,当我插入with语句时,它会挂起!
如果我运行两个第一个句子并且断言等于href属性(因此没有with语句和其他所有内容)它会通过。当我还添加google_login.click时,它会再次挂起。
当我按CTRL-C测试时,我得到以下回溯:
======================================================================
ERROR: test_google_login (functional_tests.test_allauth.TestGoogleLogin)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/vagrant/project/functional_tests/test_allauth.py", line 40, in test_google_login
self.get_element_by_id("logout")
File "/home/vagrant/project/functional_tests/test_allauth.py", line 27, in get_element_by_id
(By.ID, element_id)))
File "/home/vagrant/.virtualenvs/test/lib/python3.4/site-packages/selenium/webdriver/support/wait.py", line 71, in until
value = method(self._driver)
File "/home/vagrant/.virtualenvs/test/lib/python3.4/site-packages/selenium/webdriver/support/expected_conditions.py", line 59, in __call__
return _find_element(driver, self.locator)
File "/home/vagrant/.virtualenvs/test/lib/python3.4/site-packages/selenium/webdriver/support/expected_conditions.py", line 274, in _find_element
return driver.find_element(*by)
File "/home/vagrant/.virtualenvs/test/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 712, in find_element
{'using': by, 'value': value})['value']
File "/home/vagrant/.virtualenvs/test/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 199, in execute
response = self.command_executor.execute(driver_command, params)
File "/home/vagrant/.virtualenvs/test/lib/python3.4/site-packages/selenium/webdriver/remote/remote_connection.py", line 395, in execute
return self._request(command_info[0], url, body=data)
File "/home/vagrant/.virtualenvs/test/lib/python3.4/site-packages/selenium/webdriver/remote/remote_connection.py", line 426, in _request
resp = self._conn.getresponse()
File "/usr/lib/python3.4/http/client.py", line 1171, in getresponse
response.begin()
File "/usr/lib/python3.4/http/client.py", line 351, in begin
version, status, reason = self._read_status()
File "/usr/lib/python3.4/http/client.py", line 321, in _read_status
raise BadStatusLine(line)
http.client.BadStatusLine: ''
希望有人能够帮助我解决这个问题。 在此先感谢!!
答案 0 :(得分:-1)
我的firefox webdriver也有问题。尝试将你的firefox降级到版本36.这是最后一个对我有用的。