A Tale of Two JsUnits
There are actually two projects called "JsUnit":
One JsUnit, written by Jörg Schaible
, has limited support for in-browser testing, and is best used in the context of a command-line JavaScript shell (like Mozilla Rhino
, Microsoft's cscript.exe
, or kjsembed
.
In these non-browser contexts, there is no DOM: there's no "window" object or "document" object, (except if you define your own,) and no access to events, timers/timeouts, elements, etc. It's good for testing pure JavaScript libraries with a lot of complicated branches whose logic needs to be tested in isolation.
The other JsUnit, written by Edward Hieatt
, is explicitly designed for use within a browser and doesn't work in non-browser command-line JavaScript execution contexts.
Hieatt's JsUnit has a lot in common with Selenium: specifically, it provides a Java-based server that knows how to launch browsers and gather their results, much like the Selenese mode
of Selenium RC. Both are great tools for rigorous platform testing.
Selenium and Hieatt's JsUnit
The most important difference between Selenium and Hieatt's JsUnit is their purpose. Hieatt's JsUnit is designed to unit test JavaScript, not to run an end-to-end application-level test. Specifically:
- JsUnit provides no explicit support for automating/simulating end-user tasks (it doesn't provide commands like "click" or "dragAndDrop" or "type"), though you can certainly roll your own code for doing that (or just copy the code from Selenium's browserbot).
- JsUnit runs synchronously, meaning that your test cannot (in general) pause to wait for events to occur in the background.
- JsUnit has no recorder tool (it wouldn't really make sense).
With that said, we on the Selenium team use JsUnit ourselves; we use it for what it's designed for: unit testing. Hieatt's JsUnit is great for:
- testing your client-side JS code with mock objects
- writing complex code-based assertion commands
- testing error handling code that is hard to reproduce in a "real" application setting
Writing Selenium RC tests in JavaScript with JsUnit
Selenium RC also allows you to write your automated tests in JavaScript using Rhino
. When running under Rhino, it's up to you to provide a testing framework. (Selenium RC provides only commands like "open", "click", and "getText"... it doesn't provide testing commands like "assert" or "fail", nor does it have any "runTest" command to run a suite of tests and gather results.) For that, you may want to use Schiable's JsUnit.