django - Python selenium test gets stuck in urlopen -


my app relies on:

  • python 3
  • django 1.8
  • weasyprint
  • selenium

it runs flawlessly on dev , production environment, not while testing selenium.

using weasyprint, create pdf html, library uses urllib download css (e.g. http://localhost:8081/static/lib/bootstrap/css/bootstrap.min.css), hangs (no errors, stuck) while opening these.

if enter url directly in browser while hanged, css displayed.

command used:

./manage.py test tests.test_account.homenewvisitortest 

relevant part of test:

from selenium import webdriver  class homenewvisitortest(staticliveservertestcase):     def setup(self):         if test_env_firefox:             self.driver = webdriver.firefox()         else:             self.driver = webdriver.phantomjs()         self.driver.set_window_size(1440, 900)      def teardown(self):         try:             path = 'worksites/' + self.worksite_name.lower()             os.rmdir(settings.media_root + path)         except filenotfounderror:             pass         super().teardown()      def test(self):         d = self.driver         d.get(self.get_full_url('home'))         d.find_element_by_css_selector('.btn-success[type=submit]').click() 

in view:

    # generate pdf contact directory     template = get_template("pdf/annuaire.html")     context = {"worksite": worksite}     html = template.render(requestcontext(self.request, context))     base_url = self.request.build_absolute_uri("/")     pdf = weasyprint.html(string=html, base_url=base_url)     pdf.write_pdf(directory + '/annuaire.pdf') 

here thread dump when stuck:

fatal python error: aborted  thread 0x0000000106f92000 (most recent call first):   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/socket.py", line 374 in readinto   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/http/client.py", line 313 in _read_status   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/http/client.py", line 351 in begin   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/http/client.py", line 1171 in getresponse   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/urllib/request.py", line 1185 in do_open   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/urllib/request.py", line 1210 in http_open   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/urllib/request.py", line 441 in _call_chain   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/urllib/request.py", line 481 in _open   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/urllib/request.py", line 463 in open   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/urllib/request.py", line 161 in urlopen   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/weasyprint/urls.py", line 276 in default_url_fetcher   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/weasyprint/urls.py", line 311 in fetch   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/contextlib.py", line 59 in __enter__   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/weasyprint/__init__.py", line 297 in _select_source   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/contextlib.py", line 59 in __enter__   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/weasyprint/__init__.py", line 223 in __init__   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/weasyprint/css/__init__.py", line 198 in find_stylesheets   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/weasyprint/css/__init__.py", line 448 in get_all_computed_styles   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/weasyprint/document.py", line 312 in _render   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/weasyprint/__init__.py", line 132 in render   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/weasyprint/__init__.py", line 164 in write_pdf   file "/users/sebcorbin/sites/planexo/worksite/views.py", line 111 in done   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/formtools/wizard/views.py", line 357 in render_done   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/formtools/wizard/views.py", line 730 in render_done   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/formtools/wizard/views.py", line 300 in post   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/formtools/wizard/views.py", line 686 in post   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/views/generic/base.py", line 89 in dispatch   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/formtools/wizard/views.py", line 237 in dispatch   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/views/generic/base.py", line 71 in view   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/contrib/auth/decorators.py", line 22 in _wrapped_view   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/core/handlers/base.py", line 132 in get_response   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/core/handlers/wsgi.py", line 189 in __call__   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/test/testcases.py", line 1099 in __call__   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/contrib/staticfiles/handlers.py", line 63 in __call__   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/wsgiref/handlers.py", line 137 in run   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/core/servers/basehttp.py", line 182 in handle   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/socketserver.py", line 673 in __init__   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/core/servers/basehttp.py", line 102 in __init__   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/socketserver.py", line 344 in finish_request   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/socketserver.py", line 331 in process_request   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/socketserver.py", line 305 in _handle_request_noblock   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/socketserver.py", line 238 in serve_forever   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/test/testcases.py", line 1182 in run   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/threading.py", line 920 in _bootstrap_inner   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/threading.py", line 888 in _bootstrap  current thread 0x00007fff7996a300 (most recent call first):   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/socket.py", line 374 in readinto   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/http/client.py", line 313 in _read_status   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/http/client.py", line 351 in begin   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/http/client.py", line 1171 in getresponse   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/urllib/request.py", line 1185 in do_open   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/urllib/request.py", line 1210 in http_open   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/urllib/request.py", line 441 in _call_chain   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/urllib/request.py", line 481 in _open   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/urllib/request.py", line 463 in open   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/selenium/webdriver/remote/remote_connection.py", line 457 in _request   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/selenium/webdriver/remote/remote_connection.py", line 389 in execute   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 191 in execute   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/selenium/webdriver/remote/webelement.py", line 447 in _execute   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/selenium/webdriver/remote/webelement.py", line 68 in click   file "/users/sebcorbin/sites/planexo/tests/test_account.py", line 203 in _test_worksite_form   file "/users/sebcorbin/sites/planexo/tests/test_account.py", line 36 in test   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/unittest/case.py", line 577 in run   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/unittest/case.py", line 625 in __call__   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/test/testcases.py", line 186 in __call__   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/unittest/suite.py", line 122 in run   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/unittest/suite.py", line 84 in __call__   file "/usr/local/cellar/python3/3.4.3/frameworks/python.framework/versions/3.4/lib/python3.4/unittest/runner.py", line 168 in run   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/test/runner.py", line 178 in run_suite   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/test/runner.py", line 211 in run_tests   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/core/management/commands/test.py", line 90 in handle   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/core/management/base.py", line 441 in execute   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/core/management/commands/test.py", line 74 in execute   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/core/management/base.py", line 390 in run_from_argv   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/core/management/commands/test.py", line 30 in run_from_argv   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/core/management/__init__.py", line 330 in execute   file "/users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/core/management/__init__.py", line 338 in execute_from_command_line   file "./manage.py", line 10 in <module> 

thanks pinpointing problem, took inspiration article http://nedbatchelder.com/blog/201103/quick_and_dirty_multithreaded_django_dev_server.html , modified manage.py file:

#!/usr/bin/env python http import server import os socketserver import threadingmixin import sys   def monkey_patch_test_server():     # monkey-patches httpserver create base httpserver class     # supports multithreading     originalhttpserver = server.httpserver      class threadedhttpserver(threadingmixin, originalhttpserver):         def __init__(self, server_address, requesthandlerclass=none):             originalhttpserver.__init__(self, server_address,                                         requesthandlerclass)      server.httpserver = threadedhttpserver   if __name__ == "__main__":     os.environ.setdefault("django_settings_module", "planexo.settings")      django.core.management import execute_from_command_line      if sys.argv[1] == 'test':         monkey_patch_test_server()      execute_from_command_line(sys.argv) 

now works!


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -