[Wtr-general] Calling JavaScript code in the browser

Egil Sorensen egil at yahoo-inc.com
Fri Sep 8 13:00:56 EDT 2006


I would like to add, as Bill did in his original posting Fri, 11 Aug 2006,
that all credit goes to the blog writer "deshev"
(http://blogs.telerik.com/user/Profile.aspx?UserID=1002), and say that the
blog,
http://blogs.telerik.com/blogs/twisted_asp_net/archive/2006/04/26/201.aspx,
also has more details. I have merely tried to move a step from deshev's code
into his execute_script function.


I also started to make a version of the execute_script function that could
be called on a Watir Element's underlying OLEObject. I never got to finish
that, and I will be away from Watir for awhile. So, in case anyone should
want to run with it, here is the first version. The code works, except for
the 'this' part mentioned in the comments.


module Watir
  class Element
    # function not all there yet....
    def execute_script(scriptCode)
      obj = getOLEObject
      win = @ieController.ie.Document.parentWindow
      # have to create the variable before assigning to it in the following
line:
      win.execScript("window.WatirTempJSObject=''") 
      win.WatirTempJSObject = obj
      # STILL TO DO:
      # The idea is now to replace references to 'this' in the scriptCode 
      # with references to 'window.WatirTempJSObject'
      # I think that should work...
      win.execScript("window.WatirJSResult = (#{scriptCode})")
      return win.WatirJSResult
    end
  end # class 
end

~~ Egil

-----Original Message-----
Date: Thu, 7 Sep 2006 17:12:08 -0700
From: "Egil Sorensen" <egil at yahoo-inc.com>
Subject: [Wtr-general] Calling JavaScript code in the browser
Message-ID: <00b201c6d2db$6c256a60$9f9315ac at ds.corp.yahoo.com>

Thanks to Bill Attebery for pointing to a blog with a good solution to this
problem. I have experimented with making it easier to get any return value
back to Watir. This is what I came up with:
(...)



More information about the Wtr-general mailing list