One of the side projects I've been working on this week is to try using JsUnit as a runner for Selenium tests, instead of HTML-table Selenese. This would make it easier to write complicated tests with conditionals, loops, etc..

(I can't be the first person to have thought of this...? Didn't Selenium at one time depend on JsUnit?)

Anyway, it turns out to be pretty easy to use Selenium in a JsUnit context. JsUnit's TestRunner.html defines a handy invisible iframe that's pretty analogous to SeleniumRunner.html's "imyframe". You access it like this:

   function testFoo() {
      inform("in testfoo");
      var docLoader = top.testManager.documentLoader;
      var frame = docLoader.document.getElementById('documentBuffer');
      var selenium = Selenium.createForFrame(frame);
      selenium.assertTextPresent("Hello World");
      inform("done!");
   }

It's similarly pretty easy to tweak selenium-logging.js to use JsUnit's logger [debug(), inform(), warn(), error()] and to make Assert.fail() throw a JsUnitException instead of Selenium's AssertionFailedError.

The main tricky part turns out to be waiting for events. JavaScript has no native sleep() function; if you use the (asynchronous) window.setTimeout() method, you may accidentally return control to JsUnit, as if your test was "passing". (The setTimeout callback will get run eventually, but not *by* JsUnit, so the runner won't notice or care.)

Many people have remarked about this as a weakness in JsUnit for testing asynchronous events. I've found a reasonable kludge for this, which I've posted to the JsUnit mailing list (free subscription required): http://groups.yahoo.com/group/jsunit/message/460

Putting the two together, you can use Selenium and the BrowserBot very effectively in pure JsUnit tests to write your tests in pure JavaScript.

Is this helpful to anyone? Am I the only one who prefers writing tests in JavaScript rather than HTML-table Selenese?

-Dan
_______________________________________________
Selenium-users mailing list
Selenium-users@lists.public.thoughtworks.org
http://lists.public.thoughtworks.org/mailman/listinfo/selenium-users

Reply via email to