Greasemonkey YUI Test Example (YUI 3)

By Maarten Last update Dec 24, 2009 — Installed 263 times.


Script Summary: v1.0: Shows how to set up YUI Test (YUI 3) for use with Greasemonkey scripts.

Version: 1.0

This script is written using YUI 3; a YUI 2 version is also available.
Note that for unit testing you can use either one.

This script shows how to set up YUI Test (YUI 3) for use with Greasemonkey scripts. It allows you to test any page, even those you don't own. It was created so that you can write unit tests for Greasemonkey scripts.

The bulk of this script came from: http://wiki.greasespot.net/Code_snippets#YUI_Li...
All I did was adapt it for use with YUI Test, and provide instructions for setting up your scripts so they can be tested.

The script as it is provided here can be used to test any web page in the world. To use it to test a specific Greasemonkey script, just one change is needed (add a @require tag); see the test instructions below.

Test Instructions:
To test any Greasemonkey script, follow these instructions
  1. Uninstall or disable the script you want to test
  2. Include the script you want to test via @require http://url/to/script.user.js in the test script (just like it requires the YUI libraries)
  3. Important: to avoid inflating the #installs counter on userscripts.org, host a copy of the script you want to test elsewhere, and require that one instead of the one on userscripts.org
  4. Install the test script (this will fetch and install the @required code too)
  5. Reload the page; you should see the script you want to test as well as the test script running side by side
  6. All public functions defined in the script you want to test can now be called by your test script (just like it calls out to the YUI libraries)
  7. The previous point means that you might have to change the script you want to test to make things testable.
  8. Important: if you make changes to the script you want to test, just re-install the test script (even if the test script hasn't changed), so that the @required code is re-installed

Instructions for using YUI Test are available at: http://developer.yahoo.com/yui/3/yuitest/

History:
  • 2009-12-24: v1.0: initial version; brings up YUI Logger console and shows sample test result.