hexagonit.testing

This package provides useful helper methods for browser testing in Plone by enhancing the zope.testbrowser test browser.

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

Usage

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.
browser.setBaseUrl(self.portal.absolute_url())

# Access objects using readable URLs.
browser.open('/my/cool-page')
browser.open('/some/other/place')

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

# Inspect the current URL in a real browser.
browser.openBrowser()

# 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'),
        }
    })

API

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

Enhanced test browser.

setBaseUrl(base_url)[source]

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.
dump(filename)[source]

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.

Parameters:
  • 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.

Parameters:
  • name (str) – Header name.
  • value (str) – Header value against the name.
login(username, password, login_url='/login_form')[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()).

Parameters:
  • username (str) – User name.
  • password (str) – Password.
  • login_url (str) – Absolute path for the login URL.
deletePortletManager(portal, name)[source]

Deletes a portlet manager of the given name.

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

Parameters:
  • 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.

Parameters:
  • 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'),
... ])
Parameters:
  • url (str) – where data will be posted.
  • data (iterable or dict) – Submission data either as an iterable of (key, value) tuples or a dictionary.

Contents:

Indices and tables

Table Of Contents

Next topic

Installation

This Page