These are chat archives for django/django

11th
Jul 2015
Charles Javelona
@charlesjavelona
Jul 11 2015 20:24
I think im going mental. Im using django-behave and keep getting this error
ile "user/features/steps/register.py", line 9, in impl
          client = context.browser.get('http://localhost:8000/')
        File "/Users/charlesjavelona/.virtualenvs/student_job/lib/python3.4/site-packages/behave/runner.py", line 214, in __getattr__
          raise AttributeError(msg)
      AttributeError: 'Context' object has no attribute 'browser'
@when('I click the Register button')
    7 def impl(context):
    8     client = context.browser.get('http://localhost:8000/')
    9     client.context.browser.find_element_by_link_text('Register').click
Above is an example code inside steps folder. What am I doing wrong. I have been searching but django-behave does not have much examples
Mir Nazim
@mnazim
Jul 11 2015 20:27
@charlesakio what’s the output of print(dir(context))
Charles Javelona
@charlesjavelona
Jul 11 2015 20:28
I did that earlier, hold on let me copy it
Captured stdout:
BEHAVE
      USER
      __class__
      __contains__
      __delattr__
      __dict__
      __dir__
      __doc__
      __eq__
      __format__
      __ge__
      __getattr__
      __getattribute__
      __gt__
      __hash__
      __init__
      __le__
      __lt__
      __module__
      __ne__
      __new__
      __reduce__
      __reduce_ex__
      __repr__
      __setattr__
      __sizeof__
      __str__
      __subclasshook__
      __weakref__
      _config
      _dump
      _emit_warning
      _mode
      _origin
      _pop
      _push
      _record
      _root
      _runner
      _set_root_attribute
      _stack
      execute_steps
      user_mode
Mir Nazim
@mnazim
Jul 11 2015 20:37
So when you are doing context.browser.get there is no browser. Is this the correct type of object that you need? There oly two public methods on it. execute_steps and user_mode.
… says you need to explictly initialize context.browser
from splinter.browser import Browser

def before_all(context):
    context.browser = Browser()

def after_all(context):
    context.browser.quit()
    context.browser = None
Looks like it wont be available by default.
Charles Javelona
@charlesjavelona
Jul 11 2015 20:40
@mnazim im fairly new at bdd and django-behave. I understand that part and here is what I did in my the file they want to explicitly initialize it.
import os
+   2 from splinter.browser import Browser
    3 # This is necessary for all installed apps to be recognized, for some reason.
    4 os.environ['DJANGO_SETTINGS_MODULE'] = 'student_job.settings'
    5
    6
    7 def before_all(context):
    8     # Even though DJANGO_SETTINGS_MODULE is set, this may still be
    9     # necessary. Or it may be simple CYA insurance.                                                    10     from django.core.management import setup_environ
   11     from myproject import settings
   12     setup_environ(settings)                                                                            13
   14     ### Take a TestRunner hostage.
   15     from django.test.simple import DjangoTestSuiteRunner
   16     # We'll use thise later to frog-march Django through the motions                                   17     # of setting up and tearing down the test environment, including
   18     # test databases.
   19     context.runner = DjangoTestSuiteRunner()
   20
   21     ## If you use South for migrations, uncomment this to monkeypatch                                  22     ## syncdb to get migrations to run.
   23     # from south.management.commands import patch_for_test_db_setup                                    24     # patch_for_test_db_setup()                                                                        25
   26     ### Set up the WSGI intercept "port".                                                              27     import wsgi_intercept
   28     from django.core.handlers.wsgi import WSGIHandler                                                  29     host = context.host = 'localhost'
   30     port = context.port = getattr(settings, 'TESTING_MECHANIZE_INTERCEPT_PORT', 17681)
   31     # NOTE: Nothing is actually listening on this port. wsgi_intercept                                 32     # monkeypatches the networking internals to use a fake socket when
   33     # connecting to this port.
   34     wsgi_intercept.add_wsgi_intercept(host, port, WSGIHandler)
   35
+  36     context.browser = Browser()
+  37
+  38     def after_all(context):
+  39         context.browser.quit()
+  40         context.browser = None
The only thing that is new in this file is from what you mentioned
Il try deleting everything except the one they mentioned.
Mir Nazim
@mnazim
Jul 11 2015 20:44
I haven’t done any BDD either. I was just going by the standard python code on the docs.
  • Django settings should always be imported as from django.conf import settings.
  • Inject a few print statements in there to make sure before_all is actually getting called.
also after_all is indented inside before_all. Is the intentional?
Charles Javelona
@charlesjavelona
Jul 11 2015 20:54
No it's not.
Here is the original file after downloading it
```python
```python
import os
# This is necessary for all installed apps to be recognized, for some reason.
os.environ['DJANGO_SETTINGS_MODULE'] = 'student_job.settings'


def before_all(context):
    # Even though DJANGO_SETTINGS_MODULE is set, this may still be
    # necessary. Or it may be simple CYA insurance.
    from django.core.management import setup_environ
    from myproject import settings
    setup_environ(settings)

    ### Take a TestRunner hostage.
    from django.test.simple import DjangoTestSuiteRunner
    # We'll use thise later to frog-march Django through the motions
    # of setting up and tearing down the test environment, including
    # test databases.
    context.runner = DjangoTestSuiteRunner()

    ## If you use South for migrations, uncomment this to monkeypatch
    ## syncdb to get migrations to run.
    # from south.management.commands import patch_for_test_db_setup
    # patch_for_test_db_setup()

    ### Set up the WSGI intercept "port".
    import wsgi_intercept
    from django.core.handlers.wsgi import WSGIHandler
    host = context.host = 'localhost'
    port = context.port = getattr(settings, 'TESTING_MECHANIZE_INTERCEPT_PORT', 17681)
    # NOTE: Nothing is actually listening on this port. wsgi_intercept
    # monkeypatches the networking internals to use a fake socket when
    # connecting to this port.
    wsgi_intercept.add_wsgi_intercept(host, port, WSGIHandler)

    def browser_url(url):
        """Create a URL for the virtual WSGI server.
        e.g context.browser_url('/'), context.browser_url(reverse('my_view'))
        """
        return urlparse.urljoin('http://%s:%d/' % (host, port), url)

    context.browser_url = browser_url

    ### BeautifulSoup is handy to have nearby. (Substitute lxml or html5lib as you see fit)
    from BeautifulSoup import BeautifulSoup
    def parse_soup():
        """Use BeautifulSoup to parse the current response and return the DOM tree.
        """
        r = context.browser.response()
        html = r.read()
        r.seek(0)
        return BeautifulSoup(html)

    context.parse_soup = parse_soup


def before_scenario(context, scenario):
    # Set up the scenario test environment
    context.runner.setup_test_environment()
    # We must set up and tear down the entire database between
    # scenarios. We can't just use db transactions, as Django's
    # TestClient does, if we're doing full-stack tests with Mechanize,
    # because Django closes the db connection after finishing the HTTP
    # response.
    context.old_db_config = context.runner.setup_databases()

    ### Set up the Mechanize browser.
    from wsgi_intercept import mechanize_intercept
    # MAGIC: All requests made by this monkeypatched browser to the magic
    # host and port will be intercepted by wsgi_intercept via a
    # fake socket and routed to Django's WSGI interface.
    browser = context.browser = mechanize_intercept.Browser()
    browser.set_handle_robots(False)


def after_scenario(context, scenario):
    # Tear down the scenario test environment.
    context.runner.teardown_databases(context.old_db_config)
    context.runner.teardown_test_environment()
    # Bob's your uncle.
Mir Nazim
@mnazim
Jul 11 2015 21:03
I am sure these need to be connected somewhere. you might be missing some setting. Dig into docs.
Charles Javelona
@charlesjavelona
Jul 11 2015 21:06
Will do
Thanks