使用Zombie.js进行快速功能JavaScript测试
项目描述
Zombie.js是一个快速的无头浏览器,它提供了所有Node.js提供的JavaScript支持。此包提供了一个函数装饰器,允许使用类似Python的CoffeeScript进行Python doctest样式的功能JavaScript测试。
免责声明: 这主要是一个实验。这永远不会取代你现有的Selenium栈,因为即使Zombie.js工作,它也只是另一个带有自己怪癖的浏览器。
要求
当您可以在控制台中运行以下命令而没有任何打印(返回任何错误)时,应满足要求
$ echo "require 'zombie'; require 'async'"|coffee -s
使用示例(使用plone.app.testing)
从定义一个使用ZServer的功能测试配置文件开始(它将默认在localhost:55001上运行您的Plone)
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")
然后使用CoffeeScript而不是Python编写您的功能JavaScript测试作为testmethod的doctest。使用装饰器(如以下所示)定义测试的上下文(URL)(@browser来自collective.zombiedoctesting)。您可以使用上下文提供的所有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("https://127.0.0.1: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() https://127.0.0.1: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") https://127.0.0.1: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在doctest示例之间使变量可用。
如果您想查看与zombie一起运行的完整JavaScript,可以在@browser-decorator调用中添加debug=True。
变更日志
1.1.0 – 2011-10-30
第一个(希望)没有问题的版本。
项目详情
下载文件
下载适用于您的平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源分发
collective.zombiedoctesting-1.1.0.zip (17.0 kB 查看哈希)
关闭
collective.zombiedoctesting-1.1.0.zip的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 36b19aa70754ab6538484382d77a1f7bfd66d2e38fae83c266c80b8eea564fde |
|
MD5 | d4981757f1054ef427cb1e65b30cffeb |
|
BLAKE2b-256 | b1828d0d397c7ae62ca02b7daf48a36db9bed350803d889a5f69206f154aaa0c |