Zach’s ugly mug (his face)

Zach Leatherman

JavaScript Code Coverage Tool for Firebug

April 18, 2007

Warning

This article is old and may contain information that is outdated, irrelevant, or—dare I say it—no longer accurate. Read with care!

Download FirebugCodeCoverage-0.1.xpi

Recently, I’ve become interested in client side automated testing tools. When I found the Selenium plug-in for Firefox, I was blown away. I can record actions in my browser and play them back! No more manual testing of JavaScript code!

Researching a little bit more about automated testing tools led to the discovery of a metric called ‘Code Coverage’, a statistic that gives you a percentage of how much of your code executed during a certain period of time (usually while your automated test was executing).

Of course, I immediately thought of Joe Hewitt’s Firebug extension for Firefox, which includes a feature for JavaScript Profiling. The Profile feature of Firebug was similar to a Function Entry Code Coverage report, so I decided to modify the feature and release an add-on for Firefox that showed (at a file level) a list of which functions executed and which functions did not.

coverage.gif

So, now, to test my JavaScript code coverage, I will write automated tests using the Selenium IDE plug-in for Firefox, hit the Code Coverage button in Firebug, and try to get my Code Coverage statistics in the upper 90%’s.

Screenshot Example of running the tool at yahoo.com
Code Coverage

Download FirebugCodeCoverage-0.1.xpi.

Requirements (obviously)

  • Mozilla Firefox
  • Firebug Extension for Mozilla Firefox

Limitations:

  • Does not include statistics on anonymous functions.
  • Is limited to function (entry) code coverage, does not include other forms such as exit, statement, condition, or path code coverage. (Description: Wikipedia)
  • Is my first Firefox add-on, so there was a learning curve involved.

Future Improvements:

  • Automated integration with Selenium IDE (one button to run tests and do code coverage)
  • Better display of results, instead of a big kludge of function names
  • Additional types of coverage, depending on what options are available from jsdIDebuggerService

Update: After many requests and much self deliberation, I have decided not to update the plugin to work with newer versions of Firefox. Code Coverage doesn’t belong in the browser, it’s just the wrong place in the tool chain to have something like this. Code Coverage results need to be exportable, and the utility must be executable from the command line to use with unit tests and continuous integration.

Zach’s ugly mug (his face)

Zach is a Web Craftsperson with the award winning Filament Group. He’s currently fixated on web fonts and static site generators. His public speaking résumé includes talks in eight different countries at events like Beyond Tellerrand, Smashing Conference, CSSConf, and The White House. He also helps herd NEJS CONF and the NebraskaJS meetup. Read more about Zach »

18 Replies
    1. Hi!I'm trying to develop something similar to what you're trying to do with Selenium and Firebug Code Coverage.Do have any reference of how to do this integration? How to instantiate an add-on "A" into an add-on "B"?Tks for the attention.Rafael
      1. Zach Leatherman Disqus

        30 May 2007 at 09:09PM
        Sure, I have all the stuff I used as a reference (particularly the link about "Extending Firebug") over at delicious under the tag XULhttp://del.icio.us/zachleat...
        1. This is an absolutely amazing plugin for Firebug! I was looking all over the net for something like this, and it was a PERFECT fit!Of course, I'd love some more features :) Please please please add: 1) Support for nested function - OO-style. If there's a class MyClass and a function MyFunction, it would be awesome if you displayed it like "MyClass.MyFunction"2) Click to navigate to the definition of the function. Firebug already has a great code browser, it would be amazing to be able to click on a function name in your plugin and go directly to the place in code where it's defined.
          1. Hi,I have a requirement (may be strange) as follows:- Execute blackbox test cases using GUI- Hopefully, there is a tool that can record all my actions while I execute blackbox test and play back.- The tool should be able to export the recorded instructions as HTTPUNIT tests.- I need in particular HTTPUNIT or any other tests basically to see the code coverage of JSP/JAVA files.Is there anything for this?Please help
            1. Elephant Disqus

              06 Aug 2007 at 11:38AM
              Cool extension. Why don't you publish it in Mozilla Ads-ons page?
              1. Zach Leatherman Disqus

                06 Aug 2007 at 11:57AM
                It's currently in the sandbox. They won't make it public until it has a certain amount of reviews.
                1. David Burns Disqus

                  09 Aug 2007 at 02:10PM
                  This is really good tool. I really like it as I am trying to automate any and all items that i can.I do have one question though. I ran a selenium test and one of the items that code coverage picked up showed a function in both Function Called and Function Uncalled.Why is this?Thats my only question but its really good!
                  1. Zach Leatherman Disqus

                    09 Aug 2007 at 10:38PM
                    Well, it doesn't report on nested functions inside of objects. So, for example, say you have two identical functions located inside two different objects.YAHOO.util.Dom.getStyle()DAVIDBURNS.getStyle()would unfortunately both be reported as getStyle in the current plugin.
                    1. Mike Cole Disqus

                      20 Sep 2007 at 02:18AM
                      Top-notch! Thanks!
                      1. do you have a place where request features or submit bugs? it will be good if the columns size would be resizable, i have some calls to long uri scripts (.net weresource.axd with a chunk of encoded data on the parameters) and i'm not capable of view the functions called.
                        1. Zach Leatherman Disqus

                          14 Nov 2007 at 11:24PM
                          Well, it uses the default table structure given from Firebug. So, if that feature gets added into Firebug, it'll be usable here. I don't think I want to be the one to add resizeable columns though.You could buy a higher resolution monitor? :P
                          1. Zach Leatherman Disqus

                            30 Mar 2008 at 03:04PM
                            Saw another tool for JavaScript code coverage in a link to my page, and it's got command line, and line reporting!http://siliconforks.com/jsc...Source:http://rplcodeline.blogspot...
                            1. Daniel Colascione Disqus

                              07 Jan 2009 at 07:38PM
                              Check out the hrtimer Firefox extension, which contains a both a line-by-line code coverage tool for Javascript and a call-graph profiler. The package is still in development, but it's useful now.
                              1. Zach Leatherman Disqus

                                07 Jan 2009 at 07:42PM
                                Awesome Daniel!Any plans for integration with Firebug?
                                1. Any hope to update it for firefox 3.0?VELO
                                  1. Aaron T. Grogg Disqus

                                    22 Jun 2009 at 06:32PM
                                    hey, Zach, cool looking add-on, any word on it being compatible with FF 3.0.10 or .11?thanks,Atg
                                    1. hey, cool looking add-on, any word on it being compatible with FF 3.3?thanks,
                                      1. Zach Leatherman Disqus

                                        31 Dec 2009 at 04:02AM
                                        Hey guys, I've updated the article with information about the future of this plugin.
                                        Webmentions