python - Django headless Selenium 挂起

标签 python django selenium

我正处于使用 Django 开发网络应用程序的开始阶段。 我在 Vagrant 中使用 headless Selenium 设置。

访问页面工作正常,但在下面的测试中它只是挂起。 我不确定这是否是由于 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

火狐浏览器 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 语句时它挂起!

如果我运行前两个句子并 assertEqual 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: ''

希望有人能帮我解决这个问题。 提前致谢!!

最佳答案

我也遇到了 firefox webdriver 的问题。尝试将您的 Firefox 降级到版本 36。这是对我有用的最后一个版本。

关于python - Django headless Selenium 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34840949/

相关文章:

python - 无法安装和使用 python-docx?

python - 更新 ConfigParser 中的部分(或替代方案)

python - 两个列表给出相同值的列表的逻辑错误

python - Elixir 的 Pylons

python - 无法在 CentOS v8 上通过 yum 安装 mod_wsgi

java - 如何将字符串转换为按类型

javascript - 在 Python Selenium 中检测用户键/鼠标

python - 在Django中立即运行页面并返回给用户的正确方法是什么?

testing - 我是使用 Selenium 进行自动化测试的新手。运行自动化测试需要什么?

selenium - Xvfb 和 Chromedriver 有什么区别以及何时使用它们