一个django模板标记,用于在html元素上启用testhook属性。
django-testhook的Python项目详细描述
一个django模板标记,用于在html元素上启用testhook属性。
关于django testhook
django testhook提供了一个模板标记来生成 data-testhook-id用于HTML模板。
这对于自动化测试非常有用(例如: Webdriver.IO)来维护一个固定的条目 点,而不是通过重命名来中断自动测试 需要重新计算 xpath选择器。
要求
Django 1.8.x或更高版本,Python 2.7或更高版本
安装
使用pip安装django testhook:
$ pip install django-testhook
配置
配置django testhook
将以下内容添加到您的设置文件中:
INSTALLED_APPS+=('testhook',)
可用设置
默认情况下,将启用testhook数据属性的呈现。如果你 在特定环境中,您要禁用它 只需将其配置为False。
TESTHOOK_ENABLED=False
用法
基本用法
在html模板中,必须加载testhook标记才能使用 它testhook标记只需要一个参数就可以返回 一个结果。
{% load testhook %} <divclass="my-elem"{%testhook"test-elem"%}> I want to test this </div>
这将输出到以下位置:
<divclass="my-elem"data-testhook-id="test-elem"> I want to test this </div>
对象用法
对于动态元素,还可以选择传递参数。例如: 假设我在购物篮里有一个产品,上面有一个主键和一个 我可以这样使用它:
<divclass="item"{%testhook"basket"product.idproduct.slug%}> {{ product.title }} </div>
它将输出到:
<divclass="item"data-testhook-id="basket-1-product-slug"> A product title </div>