How to Access JavaScript Variables

393 views
Skip to first unread message

xeranas

unread,
Nov 22, 2011, 5:32:53 AM11/22/11
to Watir General
I want access some javascript global variables, but out of luck so at
first I want know that I use correct methods to take value.

lets say on website exist simple script:
<script type="text/javascript">
var myvar = 5;
</script>

I was trying with methods below access "myvar" value:

$myBrowser.ie.Document.targetWindow.myvar
$myBrowser.execute_script("targetWindow.myvar")
$myBrowser.execScript("targetWindow.myvar")

Is these methods are correctly write? What differences between them?

Chuck van der Linden

unread,
Nov 23, 2011, 12:48:12 PM11/23/11
to Watir General
The first question I'd have then would be why do you want to do this?
and might there be some other way (e.g. if that variable is
controlling something that is reflected on the screen or in the DOM,
then perhaps you can figure out how to 'see' it the way a user would.

The intent of Watir is to drive the browser the way the user interacts
with it. There's little provision for interacting directly with the
javascript or retrieving/changing values in variables other than via
how it would happen with a genuine user using an input element or
clicking on things etc. About the only exception to this is the
ability to invoke a specific bit of javascript via .execute_script.
That method is mostly there to allow a fallback way to simulate the
response to some user action which for some reason cannot be
automated. It does however return a result, so it might be able to be
used for what you want to do. None of your code however is trying to
capture that response, so perhaps start with that.. e.g.

myvar = $myBrowser.ie.document.targetWindow.myvar #note that
the .document method is lower case, not .Document
puts myvar

also quite possibly you may want .parentWindow there,
not .targetWindow I'm not enough of a JS expert to know which would
be proper.

If that won't work, then you're going to need to know javascript well
enough to figure out what to execute there. If you don't know that,
this might be better posted to Stack Overflow with both Watir and
JavaScript tags on the question, in order to get attention from people
proficient in JS.

Also to better assist in troubleshooting, it's helpful to know what
happened when you tried what you tried, did you get an error, did it
appear to work with no error but you got nothing back, etc.. saying
"I tried this" and not telling us (or the folks on SO) "and here's
what happened' makes it very hard to determine where you might be
going wrong.

If it helps, this is what the .execute_script method is doing under
the covers

def execute_script(source)
escaped_src = source.to_s.gsub(/[\r\n']/) {|m| "\\#{m}"}
document.parentWindow.eval(escaped_src)
rescue WIN32OLERuntimeError, NoMethodError #if eval fails we need to
use execScript(source.to_s) which does not return a value, hence the
workaround
wrapper = "_watir_helper_div_#{rand(100000)}"
cmd = "var e = document.createElement('DIV'); e.id='#{wrapper}';
e.innerHTML = eval('#{escaped_src}'); document.body.appendChild(e);"
document.parentWindow.execScript(cmd)
wrapper_obj = document.getElementById(wrapper)
result_value = wrapper_obj.innerHTML
wrapper_obj.style.display = 'none'
result_value
end

xeranas

unread,
Nov 24, 2011, 8:41:58 AM11/24/11
to Watir General
Thanks for detail answer, Chuck van der Linden.

I found bug in my code so simple code line like this:
$browser.execute_script("global_javascript_variable")
successfully access variable (same can be done with functions).

I agree with you that playing with JavaScript is not what Watir
suppose to do. However in some rare cases extra information can be
useful for testing complex web application. In my case I found this
information about web application actions state in JavaScript
variables. Seems these variables was build for applications need, but
It perfectly suit for my custom "readyCheck" method for Watir
tests.

Reply all
Reply to author
Forward
0 new messages