The Artima Developer Community
Sponsored Link

Java Community News
How Do You Test JavaScript?

5 replies on 1 page. Most recent reply: Apr 21, 2006 2:27 PM by Max Lybbert

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

How Do You Test JavaScript? Posted: Apr 20, 2006 12:11 PM
Reply to this message Reply
Summary
In a recent EWeek.com interview, James Gosling pointed to the difficulty of testing AJAX components to ensure they work as expected in different browsers.
Advertisement
In the interview, Gosling describes the problem with JavaScript testing:

The place where there's sort of black magic right now that we're trying to figure out what to do in, is around how you create AJAX components. We can make it pretty easy to use AJAX components. There are a bunch of folks out there that just need to use them. But there's a dramatically smaller community of people that create them.

Creating them is extremely hard. Not because programming JavaScript is hard, but because all these flavors of JavaScript are ever so slightly different. You have to build your components so that they're adaptable to all the different browsers that you care about. And you have to figure out how to test them. None of the browsers has decent debugging hooks. We could build little things for people where they could test these components.

The problem is that it wouldn't be exactly the same environment as being inside Internet Explorer or being inside Spark or being inside Firefox. And those environments are pretty limited. Pretty much all you can do is include a bunch of printf's—capture the log input as printf's—[with] no ability to look at variables or segue points or single-step things. There's no ability to do cross-platform QA; you've just got to do them one by one. Right now it looks pretty hopeless to make AJAX development easier.

Testing JavaScript seems to be something many people need to do more of these days, and it would be useful to share information on how it can best be done in practice. If you have developed and deployed JavaScript on web pages, to what extent have you developed automated tests for that JavaScript, and to what extent have you tested in multiple browser environments? How did you accomplish that? What were the issues? How did you solve them?


Leo Lipelis

Posts: 111
Nickname: aeoo
Registered: Apr, 2006

Re: How Do You Test JavaScript? Posted: Apr 20, 2006 5:18 PM
Reply to this message Reply
Use Firefox, and get http://www.joehewitt.com/software/firebug/ and be happy.

Just my .02c

David Phipps

Posts: 1
Nickname: dphipps
Registered: Apr, 2006

Re: How Do You Test JavaScript? Posted: Apr 20, 2006 6:20 PM
Reply to this message Reply
Automated testing involves something like a JUnit test, in most developers' minds, hence projects such as JSUnit (http://www.edwardh.com/jsunit/), in which your unit test is written in Javascript (JSUnit also has very nice features like managing multiple testing boxes and distributing tests among machines to run against multiple browsers).

However, they all suffer from one core flaw most developers don't immediately realize: JavaScript has only one thread. This means you can't wait for one event to occur without preventing that one event from occurring.

The JavaScript object XMLHttpRequest supports making HTTP requests from Javascript in either synchronous or asynchronous mode, which allows you to bridge Java and JavaScript; technologies that use this include Direct Web Remoting (DWR) (more at http://getahead.ltd.uk/dwr/) and JSON-RPC (more at http://oss.metaparadigm.com/jsonrpc/).

However, leveraging this to make it easy to write unit tests is complicated, because most developers think in terms of serial, sequentical execution with thread-based waiting, which won't even with this approach; you need to generate instructions for Javascript to follow, which includes calling back to Java and asking for the next task to execute, all done in a way that allows your Ajax to run.

Here at BEA's Interaction Division (was: Plumtree), we use Selenium (http://www.openqa.org/selenium/). We use it in what is called "driven mode": a Java-centric mode that drives steps that Javascript executes, much like a standard unit test. Selenium has a handful of bugs in its driven mode, but luckily we have reasonably good developers that have been contributing back to the project. We've developed a dozen or so highly flexible JSF components in-house, and they are all unit tested using Selenium suites.

The best approach for unit testing on smaller components is to ensure that each smaller component builds its own WAR file that includes a page that renders the component, which Selenium tests can be run against. This does require that the build machine deploy and run a small WAR, but since there's no configuration necessary, it's not that much work.

One other approach that does *not* work is to use HttpUnit, a mock object testing library that has implemented an incredibly poor simulation of a javascript library. We've found we need to run against real-world browsers, and we need to run against more than one (right now, Firefox, IE, Safari, and Mozilla).

We've been exploring other mock-object JSF control wrapping testing systems, but even the most advanced one - the Shale test framework - suffers from that critical blocker: a bad or inaccurate Javascript implementation. The point behind an Ajax unit test is that it has to determine if your UI works on all browsers.

Titus Brown

Posts: 23
Nickname: titus
Registered: Nov, 2004

Re: How Do You Test JavaScript? Posted: Apr 20, 2006 7:23 PM
Reply to this message Reply
There are several ways to test JavaScript and AJAX-y Web sites. See:

Selenium, http://www.openqa.org/selenium/.

Watir, http://wtr.rubyforge.org/ (IE-specific, but with a relatively new Firefox bridge)

Sahi, http://sahi.sourceforge.net/.

It's entirely possible to automate Selenium tests with VNC and Firefox; see agile.idyll.org.

Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: How Do You Test JavaScript? Posted: Apr 20, 2006 10:46 PM
Reply to this message Reply
> Here at BEA's Interaction Division (was: Plumtree), we use
> Selenium (http://www.openqa.org/selenium/). We use it in
> what is called "driven mode": a Java-centric mode that
> drives steps that Javascript executes, much like a
> standard unit test. Selenium has a handful of bugs in its
> driven mode, but luckily we have reasonably good
> developers that have been contributing back to the
> project. We've developed a dozen or so highly flexible
> JSF components in-house, and they are all unit tested
> using Selenium suites.
>
Thanks for the very helpful information.

How have you dealt with idiosyncracies and incompatibilities between JavaScript implementations in different browsers? To what extent has that been a problem in developing the JSF components?

Max Lybbert

Posts: 314
Nickname: mlybbert
Registered: Apr, 2005

Hmm, could Perl help? Posted: Apr 21, 2006 2:27 PM
Reply to this message Reply
/* However, [JS-only Unit Test suites] all suffer from one core flaw most developers don't immediately realize: JavaScript has only one thread. This means you can't wait for one event to occur without preventing that one event from occurring.
*/

I haven't done real web design in three or four years, as I've been busy with C++ and Perl work. However, I can't help but wonder if perhaps Perl GUI Test modules could help here ( http://www.perl.com/lpt/a/2006/02/02/x11_gui_testing.html and http://search.cpan.org/search?query=guitest&mode=all ). Your driver is no longer JS-only, but you should be able to get your events to occur.

Flat View: This topic has 5 replies on 1 page
Topic: How Do You Test JavaScript? Previous Topic   Next Topic Topic: Sun Releases Nine New JSF AJAX Components

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use