|
| 1 | +django-testhook |
| 2 | +=============== |
| 3 | + |
| 4 | +A Django template tag to enable testhook attributes on HTML elements. |
| 5 | + |
| 6 | +.. image:: https://travis-ci.org/jjanssen/django-testhook.svg?branch=master |
| 7 | + :target: http://travis-ci.org/jjanssen/django-testhook |
| 8 | +.. image:: https://img.shields.io/pypi/v/django-testhook.svg |
| 9 | + :target: https://pypi.python.org/pypi/django-testhook/ |
| 10 | +.. image:: https://img.shields.io/pypi/dm/django-testhook.svg |
| 11 | + :target: https://pypi.python.org/pypi/django-testhook/ |
| 12 | +.. image:: https://img.shields.io/badge/wheel-yes-green.svg |
| 13 | + :target: https://pypi.python.org/pypi/django-testhook/ |
| 14 | +.. image:: https://img.shields.io/pypi/l/django-testhook.svg |
| 15 | + :target: https://pypi.python.org/pypi/django-testhook/ |
| 16 | +.. image:: https://img.shields.io/pypi/pyversions/Django.svg |
| 17 | + :target: https://pypi.python.org/pypi/django-testhook/ |
| 18 | +.. image:: https://coveralls.io/repos/jjanssen/django-testhook/badge.svg?branch=master |
| 19 | + :target: https://coveralls.io/github/jjanssen/django-testhook?branch=master |
| 20 | + |
| 21 | +About django-testhook |
| 22 | +--------------------- |
| 23 | + |
| 24 | +The django-testhook provides a template tag to generate |
| 25 | +``data-testhook-id`` for HTML templates. |
| 26 | + |
| 27 | +This can be useful for automated testing (for eg: |
| 28 | +`Webdriver.IO <http://www.webdriver.io>`__) to maintain a fixed entry |
| 29 | +point, rather than having a automated test that breaks by just renaming |
| 30 | +a CSS class or an element which can require you to re-evaluate an |
| 31 | +XPath-selector. |
| 32 | + |
| 33 | +Requirements |
| 34 | +------------ |
| 35 | + |
| 36 | +Django 1.8.x or greater, Python 2.7 or greater. |
| 37 | + |
| 38 | +Installation |
| 39 | +------------ |
| 40 | + |
| 41 | +Install django-testhook with pip: |
| 42 | + |
| 43 | +:: |
| 44 | + |
| 45 | + $ pip install django-testhook |
| 46 | + |
| 47 | +Configuration |
| 48 | +------------- |
| 49 | + |
| 50 | +Configuring django-testhook |
| 51 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 52 | + |
| 53 | +Add the following to your settings file: |
| 54 | + |
| 55 | +.. code:: python |
| 56 | +
|
| 57 | + INSTALLED_APPS += ( |
| 58 | + 'testhook', |
| 59 | + ) |
| 60 | +
|
| 61 | +Available settings |
| 62 | +~~~~~~~~~~~~~~~~~~ |
| 63 | + |
| 64 | +By default the rendering of testhook data attributes is enabled. If you |
| 65 | +are in the scenario you want to disable it for a certain environment |
| 66 | +just configure it to ``False``. |
| 67 | + |
| 68 | +.. code:: python |
| 69 | +
|
| 70 | + TESTHOOK_ENABLED = False |
| 71 | +
|
| 72 | +Usage |
| 73 | +----- |
| 74 | + |
| 75 | +Basic usage |
| 76 | +~~~~~~~~~~~ |
| 77 | + |
| 78 | +Within your HTML template you must load the testhook tag in order to use |
| 79 | +it. The testhook tag only requires a single argument in order to return |
| 80 | +a result. |
| 81 | + |
| 82 | +.. code:: html |
| 83 | + |
| 84 | + {% load testhook %} |
| 85 | + |
| 86 | + <div class="my-elem" {% testhook "test-elem" %}> |
| 87 | + I want to test this |
| 88 | + </div> |
| 89 | + |
| 90 | +This will output to the following: |
| 91 | + |
| 92 | +.. code:: html |
| 93 | + |
| 94 | + <div class="my-elem" data-testhook-id="test-elem"> |
| 95 | + I want to test this |
| 96 | + </div> |
| 97 | + |
| 98 | +Object Usage |
| 99 | +~~~~~~~~~~~~ |
| 100 | + |
| 101 | +For dynamic elements there is also the option to pass arguments. For eg: |
| 102 | +given I have a product in a shopping basket with a primary key and a |
| 103 | +slug I could use it like this: |
| 104 | + |
| 105 | +.. code:: html |
| 106 | + |
| 107 | + <div class="item" {% testhook "basket" product.id product.slug %}> |
| 108 | + {{ product.title }} |
| 109 | + </div> |
| 110 | + |
| 111 | +It will output to: |
| 112 | + |
| 113 | +.. code:: html |
| 114 | + |
| 115 | + <div class="item" data-testhook-id="basket-1-product-slug"> |
| 116 | + A product title |
| 117 | + </div> |
0 commit comments