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.