Silicon Forks Home
Blog
JSCoverage
NRuler
Documentation
Forum

Silicon Forks

Archive for May, 2010

Using JSCoverage with Selenium

Saturday, May 22nd, 2010

Many people have asked questions about using JSCoverage with a Selenium test suite. There are a number of different approaches to doing this (these will likely be discussed in detail in a future article on this blog). One method is described in “JSCoverage, cobertura and selenium,” on the Mercenary Code blog. This article demonstrates how to access JSCoverage’s coverage data directly from the browser using a Selenium command, as well as how to create a custom report (in Cobertura‘s XML format) from that data.

Monitor “out of range” when installing Ubuntu Lucid Lynx

Friday, May 7th, 2010

On some computers, it seems that running the Ubuntu 10.04 (Lucid Lynx) installer (the standard desktop installer) results in a blank screen and the error message “out of range.”

(At least, that was the error message generated on one monitor. The message was generated by the on-screen display of the monitor itself, so results may vary. You may observe a different error message, smoke coming out the back of the monitor, etc.)

You can read about it in this forum thread; to summarize, the workaround is as follows:

  1. When running the installer, just before the error occurs, you will see this cryptic screen:

    Ubuntu Lucid Lynx installer

    That cryptic little keyboard next to that cryptic little man apparently means “press any key.” If you do that, a menu will pop up. First, select a language, then press F6 (“Other Options”). A new menu will pop up; use the arrow keys to move to “nomodeset”, and press Enter to select it. Press Esc to dismiss the menu. Then you can proceed with the installation as normal.

  2. Of course, this only works for the installer. When you first try to boot your newly installed system, you will get the same error as before. To boot, you will need to add nomodeset to the kernel command line. When you boot the machine, wait for the GRUB menu to appear. (If the menu does not appear, you may need to hold down the Shift key when booting.)

    GRUB menu

    Press e to edit the kernel command line:

    Editing the kernel command line

    Add nomodeset right after quiet splash:

    Adding "nomodeset" to the kernel command line

    Press Ctrl+x to boot.

  3. Of course, editing the kernel command line only affects the current boot process; the next time you boot the machine, you will have to manually add nomodeset again. To fix the problem permanently:

    1. If you have an NVIDIA video card, installing the proprietary NVIDIA driver may make the problem go away.
    2. Alternatively, you can edit the file /etc/default/grub and change the line
      GRUB_CMDLINE_LINUX=""
      

      to

      GRUB_CMDLINE_LINUX="nomodeset"
      

      Then run

      sudo update-grub
      

      See the release notes for details.

Using JSCoverage with the JsUnit test suite

Sunday, May 2nd, 2010

This is the seventh and final (for now) article in a series on using the JSCoverage tool with various JavaScript testing frameworks:

  1. Using JSCoverage with the script.aculo.us test suite
  2. Using JSCoverage with the MochiKit test suite
  3. Using JSCoverage with the jQuery test suite
  4. Using JSCoverage with the MooTools test suite
  5. Using JSCoverage with the YUI test suite
  6. Using JSCoverage with the Dojo test suite
  7. Using JSCoverage with the JsUnit test suite (this article)

Unlike the previous articles in this series, today we will not examine the test suite for an all-purpose JavaScript library, but instead we will look at JsUnit, dedicated JavaScript test framework. JsUnit comes with its own test suite, used to test the JsUnit code itself. To run this test suite, we will download the latest release (version 2.2 as of this writing). JsUnit is easiest to use running on a web server, so we will install it in the document root of a local Apache web server, running on port 8080. Then we can load the following URL in a web browser to run the test suite:

http://127.0.0.1:8080/jsunit/testRunner.html?testPage=http://127.0.0.1:8080/jsunit/tests/jsUnitTestSuite.html&autoRun=true

The JsUnit test suite

To collect code coverage data for this test suite, we will need to run it in JSCoverage’s “inverted mode,” since it does not run well inside a frame. This means we will have to add a button to the JsUnit user interface to launch the jscoverage.html file in a separate window. Looking at the jsunit/app/main-data.html file, we see that the existing buttons are generated dynamically via script; we will generate our button the same way:

document.writeln('<input type="button" name="runButton" value="Run" onclick="top.startTests()" class="' + buttonClass + '">');
document.writeln('<input type="button" name="stopButton" value="Stop" onclick="top.stopTests()" class="' + buttonClass + '">');
document.writeln('<input type="button" name="jscoverageButton" value="Coverage Report" onclick="window.open(\'../jscoverage.html\');" class="' + buttonClass + '">');

Then we can instrument the code using the jscoverage program:

jscoverage --no-instrument=tests jsunit instrumented-jsunit

We can run the instrumented test suite at the following URL:

http://127.0.0.1:8080/instrumented-jsunit/testRunner.html?testPage=http://127.0.0.1:8080/instrumented-jsunit/tests/jsUnitTestSuite.html&autoRun=true

The JsUnit test suite, instrumented using JSCoverage

We can click on the “Coverage Report” button to view code coverage statistics:

The JSCoverage "Summary" tab

Using JSCoverage with the Dojo test suite

Saturday, May 1st, 2010

This is the sixth in a series of articles about using the JSCoverage tool with various JavaScript testing frameworks:

  1. Using JSCoverage with the script.aculo.us test suite
  2. Using JSCoverage with the MochiKit test suite
  3. Using JSCoverage with the jQuery test suite
  4. Using JSCoverage with the MooTools test suite
  5. Using JSCoverage with the YUI test suite
  6. Using JSCoverage with the Dojo test suite (this article)

First, we need to download the Dojo SDK, which includes the Dojo test suite. The latest release is currently version 1.4.2. This release unpacks to a directory named dojo-release-1.4.2-src. Some of the tests in the test suite need to be run under a web server, so we’ll install the distribution in the document root of an Apache web server, running on port 8080. Then, the test suite, which uses a test framework called DOH (the Dojo Objective Harness), can be executed by loading the URL http://127.0.0.1:8080/dojo-release-1.4.2-src/dojo/tests/runTests.html in a web browser:

The Dojo test suite

To obtain code coverage data for this test suite using JSCoverage, we will need to make one modification to the source code. The DOH test framework does not work well running inside a frame, so we will use the JSCoverage “inverted mode” to run it: we will add a button to the file util/doh/runner.html which launches the JSCoverage user interface:

<h3 style="margin: 5px 5px 0px 5px; float: left;">D.O.H.: The Dojo Objective Harness</h3>
<button style="margin-top: 5px; float: left;" onclick="window.open('../../jscoverage.html');">Coverage Report</button>
<img src="small_logo.png" height="40" style="margin: 0px 5px 0px 5px; float: right;">

Then we can run the jscoverage program to instrument the code. We will use the --no-instrument option avoid instrumenting the test suite itself. Some of the files in the distribution contain non-ASCII characters encoded using the UTF-8 character encoding, so we will use the --encoding option to specify this. (Otherwise, we would get “Illegal character” errors.)

jscoverage --encoding=UTF-8 --no-instrument=dojo/tests dojo-release-1.4.2-src instrumented-dojo

Then we can run the instrumented test suite by accessing the URL http://127.0.0.1:8080/instrumented-dojo/dojo/tests/runTests.html:

The Dojo test suite, instrumented using JSCoverage

Note that some tests failed (indicated by the red sections in the bar at the top). Looking at the DOH “Log” tab, we see a lot of errors like this:

Error: Bundle not found: currency in dojo.cldr , locale=en-us

Looking in the (uninstrumented) dojo/cldr directory, we see it contains a lot of .js files; dojo/cldr/nls/en-us/currency.js is typical:

// generated from ldml/main/*.xml, xpath: ldml/numbers/currencies
({
        USD_symbol:"$"
})

The .js files under the dojo/cldr/nls directory contain valid JavaScript code, but they do not contain arbitrary JavaScript code; Dojo expects every one of these files to contain a single JavaScript object literal. When jscoverage adds instrumentation to one of these files, it no longer contains a single JavaScript object literal, and this causes errors when the test suite is run. We need to use the --no-instrument option to ensure that these files are not instrumented:

jscoverage --encoding=UTF-8 --no-instrument=dojo/tests --no-instrument=dojo/cldr/nls dojo-release-1.4.2-src instrumented-dojo

Then we can run the instrumented tests without any errors:

The Dojo test suite, instrumented using JSCoverage

We can click on the “Coverage Report” button to see the code coverage statistics:

The JSCoverage "Summary" tab

Tomorrow we will look at using JSCoverage with the JsUnit test framework.