FireWatir and jQuery
-
20-09-2019 - |
Question
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
Solution 2
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
OTHER TIPS
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);
}
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
.