watir-classic issue with execute_script and JSON

490 views
Skip to first unread message

captin

unread,
Jan 30, 2013, 6:01:11 PM1/30/13
to watir-...@googlegroups.com
I have encountered an issue with execute_script using IE9 with ruby 1.9.3p327 and watir-classic 3.4.0. I was doing some research and found my exact same issue detailed at Stack Overflow, but the thread is stale (and I'm too new to be able to comment on the issue there). Jarmo was assisting but the OP hasn't responded in over three weeks.
 
I hate redundancy but I also hate waiting indefinitely so I figured I'd pose the question here. Using the following code:
 
browser.execute_script "window.confirm = function() { return true; }"
 
I get the following error:
 
WIN32OLERuntimeError: (in OLE method `execScript': )
    OLE error code:80020101 in <Unknown>
      Could not complete the operation due to error 80020101.
    HRESULT error code:0x80020009
      Exception occurred.
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.4.0/lib/watir-classic/page-container.rb:29:in `method_missing'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.4.0/lib/watir-classic/page-container.rb:29:in `rescue in execute_script'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.4.0/lib/watir-classic/page-container.rb:22:in `execute_script'
        from (irb):14
        from C:/Ruby193/bin/irb:12:in `<main>'
 
The following JavaScript errors are also present in the browser:
 
SCRIPT1014: Invalid character
json2.js, line 1 character 1
 
SCRIPT5009: 'JSON' is undefined
edit-location?pageTitle=Edit Location&contactId=8590307238&folderId=0&folderName=%5BGlobal%5D&cardId=8590242330, line 1 character 1
 
Jarmo, I performed the actions you suggested at Stack Overflow and got the following results:
 
>> typeof JSON
"undefined"
 
>> typeof JSON.stringify
"'JSON' is undefined"
 
>> if (!window.JSON || !window.JSON.stringify) {
var json2=document.createElement('script');
json2.type='text/javascript';
json2.src='file:///C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/ext/json2.js';
document.getElementsByTagName('head')[0].appendChild(json2)
}
SCRIPT1014: Invalid character
json2.js, line 1 character 1
[object] {
nextSibling : ,
onresizeend : null,
onrowenter : null,
aria-haspopup : "",
childNodes : [object],
ondragleave : null,
oncut : null,
clientHeight : 0,
onbeforepaste : null,
ondragover : null
...
}
Do you or anyone else have any further troubleshooting steps I can attempt?
 
Thanks!

Jarmo Pertman

unread,
Feb 2, 2013, 10:47:53 AM2/2/13
to watir-...@googlegroups.com
Can you try by copying all the contents of the json2.js file from file:///C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/ext/json2.js to your IE developer tools window? Do you get any errors when doing that too? What does this code return after doing that: JSON.stringify({a: "foo"})


Jarmo Pertman
-----
IT does really matter - http://itreallymatters.net

captin

unread,
Feb 4, 2013, 9:53:52 AM2/4/13
to watir-...@googlegroups.com
Jarmo,
 
There is no error when I copy the local json2.js file (located at file:///C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.4.0/lib/watir-classic/ext/json2.js) contents to the IE developer tools window. When I run the stringify command you gave me, the out put is "{"a":"foo"}". And I get the exact same results when I use the other json2.js you linked and then run the command again.
I also tried the original commands you gave previously (typeof JSON and typeof JSON.stringify) after I loaded the json2.js script manually, and the outputs were "object" and "function" respectively.
 
In your opinion, is there some sort of automatic loading or accessing issue between JSON and IE?
 
Thanks.

Jarmo Pertman

unread,
Feb 6, 2013, 12:11:19 PM2/6/13
to watir-...@googlegroups.com
Hi!

I just noticed that you had Ruby 1.9.3 vs 1.9.2 and Watir-Classic 3.3.0 vs 3.4.0 in your error messages.
Which one was it actually and why do they differ?

Can you try to copy the watir-classic gem's json2.js file over with the contents of https://raw.github.com/douglascrockford/JSON-js/master/json2.js and see if that makes any difference?

Jarmo Pertman
-----
IT does really matter - http://itreallymatters.net

captin

unread,
Feb 6, 2013, 5:58:36 PM2/6/13
to watir-...@googlegroups.com
Jarmo,
 
My apologies for the versioning miscommunication. My initial post and initial error messages are correct with Ruby 1.9.3 and Watir-Classic 3.4.0, but the line "json2.src='file:///C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/ext/json2.js';" is probably what threw you off. That was a copy/paste mistake. The line that I truly used for troubleshooting was "json2.src='file:///C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.4.0/lib/watir-classic/ext/json2.js';" I hope this clears things up.
 
I also tried copying the json2.js file you linked into my watir-classic gem, and then I ran the same troubleshooting commands along with the original watir command I am trying to use. Same results, even though the two json2.js files are slightly different.

Jarmo Pertman

unread,
Feb 8, 2013, 2:39:14 AM2/8/13
to watir-...@googlegroups.com
That's sad to hear. What if you run execute_script two times in a row - will you also get an error for the second run?

Jarmo Pertman
-----
IT does really matter - http://itreallymatters.net

captin

unread,
Feb 8, 2013, 10:51:27 AM2/8/13
to watir-...@googlegroups.com
Same result. The script errors just continue to pile up.
 
Do you think this is a bug? If so, is it a json2.js or a watir-classic issue?
 
Thanks.

Jarmo Pertman

unread,
Feb 9, 2013, 6:19:14 AM2/9/13
to watir-...@googlegroups.com
To be honest - i'm not sure where's the problem since i cannot reproduce it on my IE8. I have one idea - create a local html file with the contents:
<html>
  <head>
    <script type="text/javascript" src="'file:///C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.4.0/lib/watir-classic/ext/json2.js"></script>
  </head>
  <body>
    foo
  </body>
</html>

Open that file with your internet explorer and try then the JavaScript commands in the developer tools. If they still don't work then it has probably something to do with your IE security settings.

Jarmo Pertman
-----
IT does really matter - http://itreallymatters.net


captin

unread,
Feb 12, 2013, 5:51:37 PM2/12/13
to watir-...@googlegroups.com
The issue still exists when trying your suggestion. I will play around with security settings and see if that leads to any resolution.
 
Thanks!

enroxorz

unread,
Feb 13, 2013, 8:14:42 AM2/13/13
to watir-...@googlegroups.com
Please make sure to post your findings. I've been monitoring this because I also have the same issue as you (didn't post because I didn't have anything new to add to this).

Jarmo Pertman

unread,
Feb 13, 2013, 3:15:50 PM2/13/13
to watir-...@googlegroups.com
I just noticed an error in my previous code. I have an extra ' character at the start of src attribute value :(

Please try again with the following correct code:
<html>
  <head>
    <script type="text/javascript" src="file:///C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.4.0/lib/watir-classic/ext/json2.js"></script>
  </head>
  <body>
    foo
  </body>
</html>
Jarmo Pertman
-----
IT does really matter - http://itreallymatters.net

Jarmo Pertman

unread,
Feb 13, 2013, 3:28:56 PM2/13/13
to watir-...@googlegroups.com
I think that i can reproduce the problem with tweaking a setting in IE.

There is a setting under Internet Options -> Advanced -> Security -> Allow active content to run in files on My Computer. This is disabled by default. Enable it, restart IE and try if it will make #execute_script working.

Jarmo Pertman
-----
IT does really matter - http://itreallymatters.net

captin

unread,
Feb 13, 2013, 3:40:20 PM2/13/13
to watir-...@googlegroups.com
The HTML code worked properly when I fixed the extra character issue you highlighted. When I run the "typeof JSON" command it returns "object" and when I run "typeof JSON.stringify" it returns "function".
 
My enterprise security settings don't allow me to change that setting so I'm unable to prove out your theory. On my system, the setting appears to be disabled by default, and when I open the HTML file from your previous suggestion I get a message saying there's blocked content, but I'm able to click a button to allow it. I am able to modify my registry, so if I can locate that setting in the registry I might be able to test out your theory.
 
In the meantime, maybe enroxorz is able to proove it out.
 
Thanks.

captin

unread,
Feb 13, 2013, 3:56:44 PM2/13/13
to watir-...@googlegroups.com
I located the registry setting and successfully changed it and verified the setting was enabled. Then I ran the execute_script command and it still errored out with the original error message and code:
 
WIN32OLERuntimeError: (in OLE method `execScript': )
    OLE error code:80020101 in <Unknown>
      Could not complete the operation due to error 80020101.
    HRESULT error code:0x80020009
      Exception occurred.
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.4.0/lib/watir-classic/page-container.rb:29:in `method_missing'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.4.0/lib/watir-classic/page-container.rb:29:in `rescue in execute_script'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.4.0/lib/watir-classic/page-container.rb:22:in `execute_script'
        from (irb):3

Jarmo Pertman

unread,
Feb 13, 2013, 4:12:21 PM2/13/13
to watir-...@googlegroups.com
:(

What about opening the html you created above?

Jarmo Pertman
-----
IT does really matter - http://itreallymatters.net

enroxorz

unread,
Feb 13, 2013, 4:24:27 PM2/13/13
to watir-...@googlegroups.com
I tested Jarmo's suggestion regarding changing security options (Allow active content to run files on My Computer and Allow software to run or install even if the signature is invalid) and it worked in my preliminary test. Attached is the HTML file, my screenshot from the IE settings, and I tested these settings with the script below (with results):

Windows XP Service Pack 3
IE 8

Script:

browser = Watir::Browser.new
browser.goto 'file:///c:/test.html'
browser.execute_script "window.confirm = function() { return true; }"
browser.execute_script "window.alert = function() { return true; }"
browser.execute_script "window.prompt = function() { return true; }"
browser.button(:value => 'Press').click

Results from IRB:

irb(main):001:0> browser = Watir::Browser.new
=> #<Watir::IE:0x..f2d5577a url="about:blank" title="">
irb(main):002:0> browser.goto 'file:///c:/test.html'
=> 0.078111
irb(main):003:0> browser.execute_script "window.confirm = function() { return true; }"
=> nil
irb(main):004:0> browser.execute_script "window.alert = function() { return true; }"
=> nil
irb(main):005:0> browser.ecute_script "window.prompt = function() { return true; }"
=> nil
irb(main):006:0> browser.button(:value => 'Press').click
=> 0.015622
irb(main):007:0>

Thank you very much, Jarmo! This issue has been bugging me since watir-classic 3.1.0. 

captin: Can you recreate my results successfully?
test.html
IE_Settings.png

captin

unread,
Feb 13, 2013, 6:37:18 PM2/13/13
to watir-...@googlegroups.com
So for me, if I run the execute_script commands against the HTML page I created (with the content either unblocked or the previously mentioned IE setting enabled), everything works. As soon as I navigate to a different page and run the command again, it fails as above.
 
Glad to hear there's a resolution for enroxorz. I'm guessing there is probably another setting or two that I need to find to get it working on my end. One more detail I have not yet mentioned is that I'm running on Windows Vista Service Pack 2 (it is probably inconsequential).

Chuck van der Linden

unread,
Mar 6, 2013, 2:28:27 PM3/6/13
to watir-...@googlegroups.com
One thing to consider if you are running into conflicts with enterprise level policy and security settings.  Try running the scripts from a Virtual Machine (using something like Virtual Box for example), where the VM is not joined to your domain, and thus not subject to domain level policies and restrictions.    (thats also safer as it would not expose the browser(s) on your primary workstation to potential exploits should you ever get directed to a compromised website which is attempting to own your system.)

Željko Filipin

unread,
Mar 7, 2013, 3:58:29 AM3/7/13
to watir
On Wed, Mar 6, 2013 at 8:28 PM, Chuck van der Linden <cvande...@climate.com> wrote:
(thats also safer as it would not expose the browser(s) on your primary workstation to potential exploits should you ever get directed to a compromised website which is attempting to own your system.)

Assuming that the exploit does not target a vulnerability in virtual machine software. :)

Željko

Chuck van der Linden

unread,
Mar 7, 2013, 5:17:37 PM3/7/13
to watir-...@googlegroups.com
Snort, yeah I suppose that might be possible, although the number of layers you'd have to be breaking through at that point would be substantial, not a likely thing, especially with modern VM's that use a Hypervisor type approach..   (but still "possible"  if you want a PC to be 100% safe, connect it to nothing, never insert an disk or USB drive you're not 100% sure is completely clean, and operate it inside a faraday cage off it's own generator) 

Željko Filipin

unread,
Mar 8, 2013, 3:33:51 AM3/8/13
to watir
On Thu, Mar 7, 2013 at 11:17 PM, Chuck van der Linden <cvande...@climate.com> wrote:
if you want a PC to be 100% safe, connect it to nothing, never insert an disk or USB drive you're not 100% sure is completely clean, and operate it inside a faraday cage off it's own generator

Welcome back to the list Chuck, I have missed your sense of humor. :)

Željko
Reply all
Reply to author
Forward
0 new messages