Greasemonkey YUI Test Example (YUI 2)

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

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



Version: 1.1

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

This script shows how to set up YUI Test (YUI 2) 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, figure out how to get the regular (collapsible) YUI Logger console to appear, and provide instructions for setting up your scripts so they can be tested using YUI Test.

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/yuitest/

Using a simplified (and non-collapsible) YUI Logger Console:
This script uses the regular YUI Logger console by default, but a simplified version is available too which uses less bytes and is not collapsible. To see the simplified (and non-collapsible) YUI Logger console, edit the script to do this:
  1. Use slightly different CSS:
    @resource yuiCss http://yui.yahooapis.com/combo?2.8.0r4/build/logger/assets/logger.css&2.8.0r4/build/yuitest/assets/testlogger.css
  2. It is no longer necessary to add the yui-skin-sam class to the body tag
History:
  • 2009-12-24: v1.1: label this script as the YUI 2 version, default to the collapsible YUI logger console as it's easier when debugging
  • 2009-12-06: v1.0: initial version; brings up YUI Logger console and shows sample test result.