cciw-website / cciw / cciwmain / tests / twillhelpers.py

from StringIO import StringIO

import twill
from twill import commands as tc
from django.core.servers.basehttp import AdminMediaHandler
from django.core.handlers.wsgi import WSGIHandler
from django.core.urlresolvers import reverse
from twill.shell import TwillCommandLoop

def make_twill_url(url):
    url = url.replace("http://www.cciw.co.uk/", "http://127.0.0.1:8080/")
    return url.replace("https://www.cciw.co.uk/", "http://127.0.0.1:8080/")

BASE = "https://www.cciw.co.uk"
def make_django_url(view, *args, **kwargs):
    return make_twill_url(BASE + reverse(view, args=args, kwargs=kwargs))

def twill_setup():
    app = AdminMediaHandler(WSGIHandler())
    twill.add_wsgi_intercept("127.0.0.1", 8080, lambda: app)
    b = twill.get_browser()
    b._browser._factory.is_html = True # make it handle XHTML
    twill.browser = b

def twill_teardown():
    twill.remove_wsgi_intercept('127.0.0.1', 8080)

class TwillMixin(object):
    twill_quiet = True

    def setUp(self):
        twill_setup()
        if self.twill_quiet:
            twill.set_output(StringIO())

    def _twill_login(self, creds):
        tc.go(make_django_url("cciw.officers.views.index"))
        tc.fv(1, 'id_username', creds[0])
        tc.fv(1, 'id_password', creds[1])
        tc.submit()

    def continue_twill(self):
        cmd = TwillCommandLoop()
        cmd.cmdloop()

    def tearDown(self):
        twill_teardown()

# Twill snippets
# To interactively continue, finish test with this:
#   self.continue_twill()
# And add
#   twill_quiet = False
# to the class
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.