Really advanced FireWatir – XUL!

by Matt 23. December 2009 15:49

This is a belated follow up to my previous couple of posts about taking FireWatir (or rather Jssh) further than simply accessing and manipulating the DOM. They were following the train of thought that since Firefox extensions have very deep access to the internals of the browser; they are written in JavaScript; and given that Jssh is just an extension, then the code you get Jssh to execute is going to be executed in the extensions context, and should allow you to do anything an extension can do.

That lead to getting a hold of Mozilla XPCOM interfaces and doing all sorts of interesting stuff – adding custom headers, changing Firefox preferences, manipulating stored cookies, or whatever else you can think of.

And now we get to take this to its logical conclusion.

Extensions are written in JavaScript and can have user interfaces, so the UI must be scriptable. And that would be XUL (think Ghostbusters), the XML User Interface Language, according to Wikipedia. It’s an XML dialect, very much analogous to HTML, but focussed on the user interface, not the page.

And Firefox exposes a DOM to manipulate it. You can iterate over the open windows using Jssh’s getWindows() function and examine the location. If it happens to start “chrome://” then the document property of that window is an XUL DOM.

Here’s a bit of script to automate the download save dialog, given the window from getWindows():


This finds the “save” radio button element, clicks it, then clicks the accept button. Looks just like scripting the HTML DOM, doesn’t it?

Go to the C:\Program Files\Mozilla Firefox\chrome folder. Make a copy of toolkit.jar and rename it to and extract the contents. Go to content\mozapps\downloads and there you’ll find the downloads.xul file with accompanying scripts and css. This is the content required to display the file download dialog. Open it in an editor to get an idea of what the object model will be like. And if you check for the location of the file download dialog from the getWindows() function, it’s at chrome://mozapps/content/downloads – so it’s fairly easy to find the appropriate xul file for the given location.

Here’s the Ruby code that will automate the download dialog:

def save_file_download
  jssh_command = "var save_dialog=null;"
  jssh_command += "for (i=0;i<getWindows().length;i++)"
  jssh_command += "{"
  jssh_command += "  var window = getWindows()[i];"
  jssh_command += "  if (window.location.toString().substring(0, 35) "
  jssh_command += "    == \"chrome://mozapps/content/downloads/\")"
  jssh_command += "  { save_dialog = window; break; }"
  jssh_command += "}"
  jssh_command += "if (save_dialog != null) {";
  jssh_command += "  save_dialog.document.getElementById(\"save\").click();"
  jssh_command += "  var acceptButton = save_dialog.document."
  jssh_command += "    documentElement.getButton(\"accept\");"
  jssh_command += "  acceptButton.disabled=false;"
  jssh_command += ";"
  jssh_command += "  true;"
  jssh_command += "} else { false; }"
  $jssh_socket.send("#{jssh_command}\n", 0)
  result = read_socket()

One very important point to make here: there is only one carriage return. Jssh prints out a prompt for each carriage return, and you need to do a read_socket() for each. Save it, and just do it once. This took me a while to figure out.

In fact, you could do a lot worse than taking a look at Ethan’s work on JsshObject which is some very nifty Ruby meta-programming that allows you to effectively write the JavaScript directly in the Ruby code. Very cool.

(The very observant among you might notice that I’m enabling the button before I click it. For some reason, the accept button is disabled unless the window has focus. I really can’t think of a good reason for this.

And if you’ve been paying particularly close attention to these posts (barring the two month gap!), I don’t actually use this code. I use the add_string_preference function from last post to add the expected mime type to the list of browser.helperApps.neverAsk.saveToDisk)

But this is a good example of the technique. It shows that browser dialogs can be automated – so hopefully no more frozen tests while displaying a JavaScript alert!

Tags: , , ,

Comments (27) -

5/22/2011 4:56:12 AM #

Recherche thématique sur plusieurs thèmes, c'est, moteur.


5/27/2011 8:32:02 AM #

Liste d'annuaires détaillées pour faciliter le référencement, Créez votre propre article.


best suv
best suv
7/20/2011 10:25:16 PM #

I've recently began a weblog, the data you provide on this site has helped me tremendously. Thanks for all your time &amp; work.


best hybrid vehicles
best hybrid vehicles
7/21/2011 12:48:24 PM #

Per il tuo bambino scegli Moncler. Una scelta di capi, estivi ed invernali, eccezionali. Tuo figlio sarà sempre alla moda e potrà muoversi in totale comodità.


free i pad
free i pad
7/23/2011 1:06:51 PM #

Hi, just required you to know I he added your website to my Google bookmarks due to your layout. But seriously, I consider your net web-site has 1 in the freshest theme I??ve came across. It extremely helps make studying your blogging site significantly easier.


best free ipad apps
best free ipad apps
7/24/2011 4:17:56 AM #

Una pagina sarà dedicata agli accessori, una alle giacche e ai giubbotti. Troverai le indicazioni per lo spaccio o negozio Moncler più vicino a casa tua e tutte le offerte più vantaggiose di questo prestigioso marchio.


DEBORA Laurence
DEBORA Laurence
8/8/2011 6:45:20 PM #

Répondre à vos questions sur la gestion de votre annuaire mais bien plus encore.


DEBORA Laurence
DEBORA Laurence
8/12/2011 11:43:59 PM #

Service Top enchère sur, vendez aux enchères vos objets neuf ou occasions.


Free DVD to DivX Converter
Free DVD to DivX Converter
8/15/2011 12:33:11 AM #

Convert YouTube FLV files to AVI, MPEG, MP4, 3GP and other formats compatible with iPod, iPhone, Apple TV, PSP, PS3, Zune, Blackberry, 3GP mobile phones, etc.


Ena Itzkowitz
Ena Itzkowitz
8/29/2011 1:41:01 PM #

I think this one is a really great post today.


Billiga bilar privatbil leasing
Billiga bilar privatbil leasing
9/2/2011 9:05:42 AM #

Very Interesting. Is there any more information on the topic?


Steven Forrester | CCTV training
Steven Forrester | CCTV training
9/29/2011 2:05:25 PM #

Hey does anyone here know how to set up wireless cctv


Austin Adeipho
Austin Adeipho
11/14/2011 7:27:21 PM #

Great to see a website with first rate content these days.


Rodolfo Thomases
Rodolfo Thomases United States
1/15/2016 4:22:43 AM #

yeah, except the "dolphin saving you" one


Normand Schwarz
Normand Schwarz United States
1/15/2016 4:35:46 AM #

getting laid AWSOME


Rico Petronis
Rico Petronis United States
1/15/2016 5:15:04 AM #

AWESOME!Can't wait for that, will be something to want to accomplish day by day, to do something AWESOME!


Mollie Limbo
Mollie Limbo United States
1/15/2016 6:58:59 AM #

Congrats! So glad to see this site getting the recognition it deserves, the world can always use a little more awesome.


Janean Finkelson
Janean Finkelson United States
1/15/2016 8:20:31 AM #

the style of keeping you connected with so many things of different types is awesome and you find it interesting everytime.


Freddy Stuart
Freddy Stuart United States
1/15/2016 8:29:05 AM #

Hey, congratulations Neil. This site really is something special and it's been a pleasure to share it with my readership too. Looking forward to future awesome things, and I'll send any suggestions that are sufficiently awesome.


Teddy Jargas
Teddy Jargas United States
4/9/2016 1:43:49 AM #

Re: Whomever made the remark that this was a fantastic world-wide-web web-site truly desires to buy their thoughts examined.


Randy Nault
Randy Nault United States
7/20/2016 7:43:09 AM #

Wow very thorough article. Thanks


Daria Justin
Daria Justin United States
8/25/2016 4:17:24 AM #

Please make a post dicussing the smart house planning using available latest technology equiptments for homes


nutra forskolin
nutra forskolin United States
9/7/2016 2:46:04 PM #

How long does a copyright last on newspaper articles?. . If a service copies newspapers articles and then posts it in a database on the Internet, is there also a copyright on the Internet content?.


potty training video
potty training video United States
9/9/2016 5:07:28 PM #

What are Mark Zuckerberg's blog posts in the movie "The Social Network"?


Andrew Pelt
Andrew Pelt United States
10/18/2016 2:38:43 AM #

Bonjour � vous, j'appr�cie votre site et je tenais � vous le dire ! bonne continuation !


Gregory Despain
Gregory Despain United States
10/18/2016 2:39:20 AM #

Bonjour � vous, j'appr�cie votre site et je tenais � vous le dire ! bonne continuation !


Andrew A. Sailer
Andrew A. Sailer United States
10/18/2016 2:39:38 AM #

Je suis souvent votre site et je le trouve parfait ! ;) continuez comme �a !


Add comment

  • Comment
  • Preview


Month List


Comment RSS