Issue with execute_script

0

I am running watir-classic 3.3.0 with the following configuration:

  • ruby 1.9.2p290
  • watir-classic 3.3.0
  • Windows XP Service Pack 3
  • IE 8

When I try to execute the following script on one of the pages I am testing, I get an error

@browser.execute_script "window.confirm = function() { return true; }"

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:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/page-container.rb:46:in `method_missing'
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/page-container.rb:46:in `rescue in execute_script'
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/page-container.rb:39:in `execute_script'
    from (irb):7
    from C:/Ruby192/bin/irb:12:in `<main>'

When I look at the browser for Javascript errors, I get the following:

Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET4.0C; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Timestamp: Thu, 3 Jan 2013 16:13:47 UTC


Message: Invalid character
Line: 1
Char: 1
Code: 0
URI: file:///C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/ext/json2.js


Message: 'JSON' is undefined
Line: 1
Char: 1
Code: 0
URI: http://iis01/XXX/employees/default.asp

NOTE: I get this several times within the IE Javascript Error log.

The site doesn't load up with errors, and I am at a loss as to why JSON2 is unidentified with invalid characters. Any idea on how I can fix this?

ruby
watir
watir-classic
asked on Stack Overflow Jan 3, 2013 by Enrique • edited Sep 17, 2015 by Justin Ko

2 Answers

2

I don't have IE8 here to try it out, but can you try it on your IE8.

  • Open IE8 and go to about:blank.
  • Open Developer Tools and open Script tab
  • Run this command: typeof JSON
  • Run this command: typeof JSON.stringify
  • What's the result?

If either of these is undefined or you get an error, then Watir tries to load json2.js dynamically like this in PageContainer#with_json2_if_needed:

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)
}

Can you try what happens when you run that code manually from the developer tools?

If it's successful, then try to run JSON.stringify too:

JSON.stringify({value: (function() {window.confirm = function() { return true; }})()});
answered on Stack Overflow Jan 5, 2013 by Jarmo Pertman
0

I tried a bunch of stuff to resolve this like modifying many IE (v. 10 in my case) security settings. In general taking off "protected mode" works but then the rest of Watir doesn't work (??). Anyway going to a CDN version of json2.js worked. Here is the monkey patch (I put into spec_helper.rb).

module Watir
  module PageContainer

    private

    def with_json2_if_needed source
      %Q[
  (function() {
    if (!window.JSON || !window.JSON.stringify) {
      var json2=document.createElement('script');
      json2.type='text/javascript';
      json2.src='https://cdnjs.cloudflare.com/ajax/libs/json2/20150503/json2.js';
      document.getElementsByTagName('head')[0].appendChild(json2)
    }
    return JSON.stringify({value: (function() {#{source}})()});
  })()
      ]
    end
  end
end
answered on Stack Overflow Sep 17, 2015 by Minimul

User contributions licensed under CC BY-SA 3.0