使用zombie.js进行快速功能javascript测试
collective.zombiedoctesting的Python项目详细描述
Zombie.js是一个具有所有javascript支持的快速无头浏览器Node.js 提供。这个包提供了一个函数decorator,它允许python 使用类似python的coffeescript进行doctest风格的功能javascript测试。
免责声明:这主要是一个实验。这不会取代你的 现有的硒堆栈,因为僵尸JS,甚至当它工作时,只是一个 另一个有自己怪癖的浏览器。
要求
当您可以在 控制台不打印任何内容(返回任何错误):
$ echo "require 'zombie'; require 'async'"|coffee -s
使用示例(使用plone.app.testing)
首先用zserver定义一个功能测试夹具(它将运行 默认情况下,您在本地主机上的plone:55001):
from plone.app.testing import PLONE_FIXTURE from plone.app.testing import FunctionalTesting from plone.testing import z2 FUNCTIONAL_TESTING = FunctionalTesting( bases=(PLONE_FIXTURE, z2.ZSERVER_FIXTURE), name="PloneFixture:ZServer")
然后将功能javascript测试作为testmethod的doctest编写 使用coffeescript而不是python。通过定义测试的上下文(url) 使用来自collective.zombiedoctesting的装饰符(@browser)作为 如下所示。您可以使用上下文提供的所有javascript 代表zombie.js浏览器的全局browser:
import unittest from plone.app.testing import TEST_USER_NAME from plone.app.testing import TEST_USER_PASSWORD from collective.zombiedoctesting import browser constants = { "TEST_USER_NAME": TEST_USER_NAME, "TEST_USER_PASSWORD": TEST_USER_PASSWORD } class LoginOverlayTest(unittest.TestCase): layer = FUNCTIONAL_TESTING @browser("http://localhost:55001/plone/", mapping=constants) def test_login(self): """ Let's start by looking up the login link using the jQuery available on our site: >>> console.log $("#personaltools-login").text() Log in Clicking that link should not redirect us anywhere, but give us an AJAX overlay with a login form. >>> $("#personaltools-login").click() >>> console.log window.location.href ... console.log $(".pb-ajax #login-form").text() http://localhost:55001/plone/ Login Name Password Let's store that form as global (to be available between different doctest examples) and fill it... >>> global.form = $(".pb-ajax #login-form") ... form.find("#__ac_name").val("%(TEST_USER_NAME)s") ... form.find("#__ac_password").val("%(TEST_USER_PASSWORD)s") ... console.log form.find("#__ac_name").val() ... console.log form.find("#__ac_password").val() %(TEST_USER_NAME)s %(TEST_USER_PASSWORD)s ... and click the button to log in. >>> form.find("input[type='submit']").click() >>> console.log window.location.href ... console.log browser.text(".documentFirstHeading") http://localhost:55001/plone/login_form You are now logged in Uh oh, we were properly logged in, but we were redirected also, so zombie is not a perfect browser yet. Also, notice, that we couldn't use jQuery in testing the document first heading (we used zombie's custom browser API), because the context after the click is an AJAX-response without jQuery or any other javascript. """
注意,每个解析的doctest示例(以>;>>开头的行)都会执行 单独使用,但是可以使用node.js'global使变量可用 在最博学的例子之间。
如果您希望看到生成的完整javascript与zombie一起运行, 您可以将debug=True添加到@browser-decorator调用中。
更改日志
1.1.0–2011年10月30日
- 第一次(希望)不间断释放。