skip to navigation
skip to content

django-testhook 1.0.2

A Django template tag to enable testhook attributes on HTML elements.

A Django template tag to enable testhook attributes on HTML elements.

About django-testhook

The django-testhook provides a template tag to generate data-testhook-id for HTML templates.

This can be useful for automated testing (for eg: Webdriver.IO) to maintain a fixed entry point, rather than having a automated test that breaks by just renaming a CSS class or an element which can require you to re-evaluate an XPath-selector.


Django 1.8.x or greater, Python 2.7 or greater.


Install django-testhook with pip:

$ pip install django-testhook


Configuring django-testhook

Add the following to your settings file:


Available settings

By default the rendering of testhook data attributes is enabled. If you are in the scenario you want to disable it for a certain environment just configure it to False.



Basic usage

Within your HTML template you must load the testhook tag in order to use it. The testhook tag only requires a single argument in order to return a result.

{% load testhook %}

<div class="my-elem" {% testhook "test-elem" %}>
    I want to test this

This will output to the following:

<div class="my-elem" data-testhook-id="test-elem">
    I want to test this

Object Usage

For dynamic elements there is also the option to pass arguments. For eg: given I have a product in a shopping basket with a primary key and a slug I could use it like this:

<div class="item" {% testhook "basket" product.slug %}>
    {{ product.title }}

It will output to:

<div class="item" data-testhook-id="basket-1-product-slug">
    A product title
File Type Py Version Uploaded on Size
django-testhook-1.0.2.tar.gz (md5) Source 2017-05-17 7KB