2012年7月19日星期四

Web 测试与浏览自动化

(1) JavaScript

所有主流浏览器对嵌入 JavaScript 的支持比较一致,对 JavaScript 暴露了 DOM 访问 API 以及浏览器操作 API。所以"JavaScript"这条路比较好走。但由于 JavaScript 不能访问本地文件(安全性考虑)等限制,我无法做"登录某站点后下载某文件"之类的操作。JavaScript 相关的 Web 自动化工具有:

  • zope.testrecorder:一个跨浏览器 (IE, Firefox, Safari) JavaScript 程序,记录浏览器事件 (点击和输入文字等) 以及测试断言 (这个文本框应包含这个文字,那个复选框应不应被勾选等)。必须安装 Zope 这个 Web Server,再访问 zope.testrocoder 这个组件,然后输入任意 URL 开始记录测试。但是安装和配置 Zope 太麻烦了。需要配合Selenium(一个开源的 Web 测试框架)或 zope.testbrowser(见下文)使用。
  • JsUnit:设计为测试静态或生成的 HTML 文档,不能与实际的 Web Server 交互。

(2) 控制浏览器

IE 和 Firefox 都提供了编程接口以便外部程序控制其行为。

  • MSDN 给出了 IE 的 COM 接口以及 VB 控制 IE 的例子。pywin32 以 Python 包装了 COM 等机制,所以用 Python 操作 IE 也是可行的PAMIE 对 pywin32 进一步包装,控制 IE 更加方便。关于 Python 的 COM 编程,请参考《Python Programming on Win32》一书。另外该书的 Chapter 21. Active Scripting 还描述了怎样把 Python 嵌入 IE(受限于安全性,嵌入 Python 不允许导入任何模块,所以并不能提供比 JavaScript 更多的功能)。另外注意: IE 7 及以上版本默认不允许 COM 控制,这样打开:Tools --> Internet Options --> Security --> Security for this zone 修改为"Medium"。
  • XPCOM 是访问 Gecko 库、嵌入或扩展 Gecko 的方法。PyXPCOM 包装了 XPCOM 机制。FireFox 等以 Gecko 作为 Web 排版引擎,所以可以通过 XPCOM 来控制它们。这方面资料较少,我至今还没有在 Google 上发现通过 PyXPCOM 控制 Firefox 的例子。

(3) 模拟浏览器

浏览器的重定向、表单和 Cookie 等行为比较好模拟,但 Frame、DOM 访问、JavaScript 和 JavaApplet 等就越来越难模拟了。以下按照模拟能力降序排列:

  • HttpUnit(Java 实现,配合 JUnit):支持基本 HTTP 认证、简单的 JavaScript。
  • zope.testbrowser 是一个 Zope 组件但可脱离 Zope 单独使用。支持简单 DOM (含表单)、Cookie、定制 HTTP 请求。
  • WebUnit(Python 实现,配合 PyUnit):DOM 方面仅支持表单,其他能力与 zope.testbrowser 相当。
  • twill(Python 实现):模拟能力与 WebUnit 相当。
  • Python 3 标准模块 urllib(基于更底层的 http.client 模块)和 http.cookiejar 等配合起来支持基本的定制 HTTP 请求、Cookie。标准模块 html.parser 解析 HTML,可能需要 python-utidylib 做 HTML 整理。(《Python Cookbook》第 14 章 Web Programming 还提到控制 IE、使用 HTTPS 等技巧。)

(4) 宏记录鼠标点击坐标和时间然后重放

这个方法受 Web 页变化影响很大,且测试逻辑不明确,所以这方式的测试用例几乎无法维护。许多商业的 Web 测试软件就是这一类。

http://linuxtoy.org/archives/web-test-and-browser-automation.html

Examples of browser simulators:

Examples of browser drivers:
  • Pamie (Python), which is based on Samie (Perl): IE automation via COM
  • Watir (Ruby): IE automation via COM
  • JSSh (Mozilla C++ extension module): Mozilla automation via JavaScript shell connections

Here are a number of tools written in or using python for testing:

unittest – this is a typical xUnit framework, and will probably be the base of most tests.  There are the expected methods setUp() and tearDown(), as well as asserts.  Tests extend TestCase,  are grouped into TestSuites, and executed withTestRunners the default being TextTestRunner.    Nothing surprising here for xUnit users, and a clear, concise way to get started for those new to writing code tests.  A couple niceties for running unit tests in Python that show maturity (over JUnit, for example) are the TestLoader and TestResult classes, for organizing tests into suites and gathering results, respectively.

Selenium – Selenium RC is written in Java, but has a python driver. A good way to ease into Python test development is through the Selenium IDE which can convert recorded tests into Python.  Not only will it help you learn the Selenium API and Python syntax, but it will also introduce you to unittest.

Windmill – Windmill is very similar to Selenium, but written in Python.  It uses a web proxy, is javascript driven, and includes a test recorder that works with Internet Explorer.  This seems to be a popular feature and is helping Windmill gain converts.  For me, however, the interactive shell is much more interesting and more useful than the Selenium interactive experience.  If you prefer, you can write your tests in Javascript as well as Python.

twill – twill is "a simple scripting language for Web browsing".  Think of it as a selenese-style interface for the grandmother of all web testing techniques, the venerable (and painful) telnet port 80.  Another analogy would be using htmlunit from a command line.  It looks very interesting for non-browser-based testing.

Pamie – Pamie is a port of Samie, a Perl framework for automating IE via COM.  Watir (Watij, Watin) is a similar framework written in Ruby which is probably more mature.  I don't know if Pamie is still active.

Mechanize - There is also a port of Perl's WWW::Mechanize to Python for those who like it.

WebUnit – WebUnit is an extension of unittest that provides additional features for web testing.  A WebTestCase drives an HTTP Request and has additional helpers.   It looks fairly simple and straightforward, and would fall into the same category of non-browser driven tests with Mechanize, Twill, HTMLUnit, and Webrat, though probably not as featureful.  (But sometimes you don't need all the features.)

Fitnesse – Fitnesse, like Selenium is written in Java, but fixtures can also be written in Python via Jython.  There is also a port of Fit called PyFit, but it may be outdated.  Personally, I like the idea of wiki documentation tied to user-editable test data and developer written test fixtures as code.  I'd like to see updated Fit style testing frameworks with a better wiki than Fitnesse.

webtest – Not to be confused with Canoo Web Test, webtest "is an extraction of paste.fixture.TestApp" In a nutshell, it's for testing at the WSGI application layer.

Saving the best for last…

doctest - Write literate tests using docstrings.  Ability to execute tests from the Python shell.  It's like fitnesse in your source code.

nose -   "nose extends unittest to make testing easier."  It's the way the cool kids are writing their tests in Python these days.  I don't know much about it.

py.test – py.test is an alternate testing framework, that is also compatible with unittest (and nose).  It's quite different, emphasising simplicity (in code) with "no boilerplate test functions".  I haven't quite wrapped my head around it, but the website says it is a "mature command line testing tool" that "automatically collects and executes tests" and "supports many testing practises and methods".  Perhaps it's Python's answer to Rspec, but going in a completely different direction.  One that looks much more interesting to me.

See also the following:

http://pycheesecake.org/wiki/AgileTestingArticlesAndTutorials

http://pycheesecake.org/wiki/PythonTestingToolsTaxonomy

http://www.simplistix.co.uk/presentations/testing04/testing04.pdf

http://www.opensourcetesting.org/unit_python.php

http://us.pycon.org/2009/conference/schedule/event/88/

没有评论: