JavaScript 測試工具庫:Atomus

jopen 9年前發布 | 18K 次閱讀 Atomus JavaScript開發

Atomus 是一個在 JSDOM的基礎上進行封裝簡單的工具庫,用來在Node.js環境下測試客戶端代碼。當使用終端模擬瀏覽器的時候,Atomus在單元測試和功能性測試中非常有用。這就是Atomus名字的來源。Atomus會和應用程序的單元和諧工作。你只需要包含需要測試框架和模塊,然后創建一個實例,然后開始使用DOM和模塊的API即可。

JavaScript 測試工具庫:Atomus

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 receives errors and window 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.$ - jQuery
  • browser.window - the usual Window object
  • browser.clicked([jQuery object or DOM element]) - fires click event.
  • browser.changed([jQuery object or DOM element]) - fires change event
  • browser.focused([jQuery object or DOM element]) - fires focus event
  • browser.blurred([jQuery object or DOM element]) - fires blur event
  • browser.selected([jQuery object or DOM element]) - fires click 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.

項目主頁:http://www.baiduhome.net/lib/view/home/1418266664073

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!