How JSSh works
FireWatir uses JSSh (TCP/IP JavaScript Shell Server for Mozilla) to drive the FireFox browser. JSSh allows other programs (like telnet) to establish JavaScript shell connections to a running Mozilla process. Once that connection is made, we can send JavaScript commands over the connection, which are executed against the DOM of the page loaded in the browser. JSSh listens on port 9997 by default.
A Sample JSSh session
$telnet localhost 9997
This will connect to the JavaScript Shell server listening on port 9997 (the default port) on the local machine (i.e. you are telnet-ing from the same machine on which the FireFox instance you want to drive using JSSh is running. JSSh can be also be used to drive a FireFox instance on a remote machine. If this is what you want to do, replace localhost with the hostname of the remote machine). Once a connection to the JSSh server inside the FireFox instance has been successfully made, you should see the following message:
Welcome to the Mozilla JavaScript Shell\!
Now you can send JavaScript commands that will run inside the browser and which can access the DOM for pages in the browser. The JS shell prompt character is '>'
var w0 = getWindows()\[0\];
getWindows() returns an array of currently opened windows. Get the first opened window in variable 'w0'.
var browser = w0.getBrowser();
getBrowser() returns the instance of browser inside window. Store that in variable 'browser'.
browser.loadURI("http://www.croczilla.com/svg/");
This will load url 'http://www.croczilla.com/svg/' in the browser.
var doc = browser.contentDocument;
Get the document object inside the browser. Now you can use any method that is supported for HTMLDocumentElement. Note that there are some methods that are supported by IE but not by FireFox.
To quit, just type:
exit() Goodbye\!
FireWatir uses this underlying mechanism to drive FireFox. FireWatir creates a socket and connects to the JSSh port on which the target FireFox browser instance's JSSh server is listening. Note that the FireFox instance must have the JSSh extension loaded, and the browser must have been started with the -jssh option (That's one hyphen before the jssh, not two). Right now this needs to be done manually.
FireWatir translates code into a JavaScript equivalent, which is then transmitted to the JSSh server and executed against the DOM of the page loaded in the browser. The top level interface - i.e. the WATiR methods that a tester or programmer can use to write a test script thus stay the same, although the target browser and the mechanism by which it is driven are very different.