views:

412

answers:

3

Hello there,

after using this excellent peace of software called FireWatir, I wonder if there is a way to integrate jQuery-selector-magic to my test.

My first attempt is to use firewatir's js_eval() method like this

require 'rubygems'
require 'firewatir'
f = FireWatir::Firefox.new
f.js_eval("alert(42);")

The only thing I get is a

JsshSocket::JSReferenceError: alert is not defined

which is kind of strange because other expressions like

f.js_eval("document.location.toString();")

Work like a charm!

Anyone with a hint?

Thanks and greets,

Joe

A: 

From the FireWatir Wiki:

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 alert function belongs to the window object of a browser, not the DOM of the page. So you can call an alert like window.alert('hello dollly').

FireWatir creates a JSSh session, that sends your js_eval to the DOM, therefore unable to interact with the window.

UberNeet
Hi and thanks for your answer. With the help of the guys at the firewatir irc channel I was able to get alert running through using window.alert - but still the final goal, jQuery, remains unsolved :/ I'll update my post today.
Joe
If you have `window.alert`, is `window.jQuery` not working?
UberNeet
No window.jQuery isn't working - but there is the workaround I posted as answer. Thanks for your help!
Joe
+1  A: 

Okay folks,

i got jQuery working with a lot of researching and I hope this will be help you out if you have similar problems:

firefox.js_eval("var target = getWindows()[0]; target.content.jQuery('#selector').toggle()")

What i had to do was to select the window I am working in explicitly and within its content jQuery is available and one is able to use all of its awesomeness! ;)

Greetings Joe

Joe
Sweet!! So it seems to me that all the command get directly send to the JSSh session, not exactly to the DOM. That's neat. Maybe it will be more useful something like this `firefox.js_eval("var $ = getWindows()[0].content.jQuery")` (don't know if this will work) And the next command, just type them as normal jQuery.
UberNeet
yeah sounds good! I'll give it a try on mondays :)
Joe
A: 

If you want to use it like you would normally with a $ you need to wrap it in a function which will run it on the context of the widows document.

var win = new getWindows()[0].content; var doc = browser.contentDocument;

$ = function(selector) {

win.content.jQuery(selector, doc);

}

chicago web design

brad