JavaScript 测试工具库:Atomus
Atomus 是一个在 JSDOM的基础上进行封装简单的工具库,用来在Node.js环境下测试客户端代码。当使用终端模拟浏览器的时候,Atomus在单元测试和功能性测试中非常有用。这就是Atomus名字的来源。Atomus会和应用程序的单元和谐工作。你只需要包含需要测试框架和模块,然后创建一个实例,然后开始使用DOM和模块的API即可。
Simple usage
All you have to do is to require the module, call the ready
method:
var htmlStr = '<body><h1>Atomus</h1></body>'; var atomus = require('atomus'); var browser = atomus().html(htmlStr).ready(function(errors, window) { ... });
The window
that is passed to our callback is the good old Window object that we have in every browser. Thankfully to jsdom we have a JavaScript implementation of the WHATWG DOM and HTML standards. So we may call window.document.querySelector
or element.dispatchEvent
. In practice we may interact with the page as we are in a real browser.
API
browser.ready([callback])
- call this method when you are ready with configuring your browser. The callback receiveserrors
andwindow
object.browser.external([filepath])
- add an absolute path to JavaScript file that you want to be injected into the page. This may be a framework or custom bundled JavaScript for example.browser.html([string])
- the initial HTML markup of the page
Once the ready
method is called we have a few other methods and objects available.
browser.$
- jQuerybrowser.window
- the usual Window objectbrowser.clicked([jQuery object or DOM element])
- firesclick
event.browser.changed([jQuery object or DOM element])
- fireschange
eventbrowser.focused([jQuery object or DOM element])
- firesfocus
eventbrowser.blurred([jQuery object or DOM element])
- firesblur
eventbrowser.selected([jQuery object or DOM element])
- firesclick
event. Use this while you operate with radio or checkboxes.
JSDom has some problems with radio and checkboxes selecting. That's why we introduced API methods for triggering events. For sure you may use $('#link').trigger('click')
but that's not working properly in some cases. So, we recommend using browser
API for dispatching DOM events.