This package provides useful helper methods for testing in Plone.

Browser testing

The hexagonit.testing.browser module provides an enhanced zope.testbrowser test browser.

The main purpose is to make functional browser tests read easier by reducing the amount of confusing setup code.

To use the enhanced test browser simple instantiate it in place of plone.testing.z2.Browser in your tests, e.g.

from hexagonit.testing.browser import Browser
browser = Browser()

# Set the base URL for easier browsing.

# Access objects using readable URLs.

# Login in to the portal
browser.login('r00t', 's3kr3t')

# Inspect the current URL in a real browser.

# POST data directly without a <form>, including a file upload.
browser.post('/web-api', {
    'foo': 'bar',
    'my_file_field': {
        'filename': 'my-document.pdf',
        'content-type': 'application/pdf',
        'data' : open('/tmp/my-document.pdf'),

Date manipulation

The hexagonit.testing.date module provides helpers to mock out the dynamic constructors datetime.date.today() and datetime.datetime.now() by providing class generators which produce instances of these classes that return static values.

This is particularly useful when used in conjunction with the mock package during unit testing, e.g.

from hexagonit.testing.date import static_date
from hexagonit.testing.date import static_datetime
import datetime
import mock
import unittest

class MyTestCase(unittest.TestCase):

    @mock.patch('my.module.datetime', static_datetime(datetime.datetime(2011, 12, 14, 12, 45)))
    @mock.patch('my.module.date', static_date(datetime.date(2011, 12, 14)))
    def test_something_that_uses_datetime(self):
        # Within the test your application code calling `datetime.now()` or `date.today()`
        # will always return the static values.


class hexagonit.testing.browser.Browser(app, url=None)[source]

Enhanced test browser.


Sets a base URL for all subsequent requests.

Usually the base URL is set to portal_url at the beginning of the test with main benefit that subsequent calls to browser.open are easier to read. So instead of writing:

>>> browser.open('{0}/path/to/object'.format(self.portal.absolute_url()))
>>> browser.open('{0}/path/to/somewhere/else'.format(self.portal.absolute_url()))

You can write:

>>> browser.setBaseUrl(self.portal.absolute_url())
>>> browser.open('/path/to/object')
>>> browser.open('/path/to/somewhere/else')
Parameters:base_url (str) – Base URL to use in subsequent calls to open.

Dumps the browser contents to the given file.

Parameters:filename (str) – File name.
open(url, data=None)[source]

Opens the given URL in the test browser with additional support for base URLs (set using hexagonit.testing.browser.Browser.setBaseUrl()).

The base URL is used when the given URL is an absolute path.

  • url (str) – Absolute path or full URL.
  • data (dict) – Request Data.
setHeader(name, value)[source]

Sets a request header possibly overwriting an existing header with the same name.

  • name (str) – Header name.
  • value (str) – Header value against the name.
login(username, password, login_url='/login', index=0)[source]

Logs into the portal.

Assumes that the browser has been configured with a base URL pointing to the portal root (see hexagonit.testing.browser.Browser.setBaseUrl()).

  • username (str) – User name.
  • password (str) – Password.
  • login_url (str) – Absolute path for the login URL.
  • index (int) – The index of the log in bottun.
deletePortletManager(portal, name)[source]

Deletes a portlet manager of the given name.

Usually it is u’plone.leftcolumn’ or u’plone.rightcolumn’.

  • portal (object) – Portal object.
  • name (unicode) – Portlet manager name.
openBrowser(browser=None, filename='testbrowser.html')[source]

Dumps self.browser.contents (HTML) to a file and opens it with a normal browser.

If browser is not specified, system default browser will be used.

  • browser (str) – Excutable browser name such as ‘firefox’.
  • filename (str) – HTML file name where the results of html contents will be writen.
post(url, data)[source]

Posts the given data to the given url with a multipart/form-data submission instead of application/x-www-form-urlencoded.

This is particularly useful if you need to test web APIs which expect POST request without having to generate forms just for testing purposes.

>>> browser.post('/web-api', {
...   'foo': 'bar',
...   'bar': 'foo',
... })

To POST a file you can use the following dictionary structure as value for the field:

{ 'filename': 'my-document.pdf',
  'content-type': 'application/pdf',
  'data' : open('/tmp/my-document.pdf'),

The dictionary must contain the above fields where filename and content-type are strings and data is a file-like object. To perform a file upload you could make a following kind of call:

>>> browser.post('/web-api', {
...   'my_file_field': {
...       'filename': 'my-document.pdf',
...       'content-type': 'application/pdf',
...       'data' : open('/tmp/my-document.pdf'),
...       }
... })

If the order of the submitted fields is significant a sequence of (key, value) tuples may be used instead.

>>> browser.post('/web-api', [
...   ('foo', 'bar'),
...   ('bar', 'foo'),
... ])
  • url (str) – where data will be posted.
  • data (iterable or dict) – Submission data either as an iterable of (key, value) tuples or a dictionary.

Grepping contents with given regex.


Searching contents with given regex.

bycss(cssselector, numbered=True)

Showing only parts specified by given CSS selector


Generates a modified datetime.date class with a static .today() method which returns the given date, which must be an instance of datetime.date.

Parameters:date (datetime.date) – The static date that the today() will return.
Return type:Modified datetime.date

Generates a modified datetime.datetime class with a static .now() method which returns the given date which must be an instance of datetime.datetime.

Parameters:dt (datetime.datetime) – The static datetime that the now() method will return.
Return type:Modified datetime.datetime


Indices and tables

Table Of Contents

Next topic


This Page