What follows is a working solution and some lessons learned in
handling popups. Thanks for everyone that has helped along the way.

I needed to handle confirmation popups. After trying autoit based
solutions without success, I decided to try a solution that has worked
for me in the past - overriding the javascript that displays the
popup.

The two things that made this hard are : 1) I was working within a
frame, 2) IE.

When you are working within a frame environment, you must call
execScript on the frame's window. This is because IE - unlike all
other browsers - only passes a reference to your function. The
function will get called, but any references to the js object 'window'
will be to the window where the js resides, not the window for the
frame containing the button. I would probably not have this problem
with any other browser (although I haven't actually tested it as my
app doesn't run on anything but IE)

The final working code looks like:



    onclick_js =   "function(event){"
    onclick_js +=  "  event = window.event;"
    onclick_js +=  "  if(event){"
    onclick_js +=  "    alert('found event');"
    onclick_js +=  "  } else { "
    onclick_js +=  "    alert('no event available'); "
    onclick_js +=  "  }"
    onclick_js +=  "}"

    js ="  var myelem=document.getElementById('" + html_id  + "');"
    js +="  if(myelem){ "
    js +="    myelem.onclick=" + onclick_js + ";"
    js +="  }"

   @browser.frame(:id, 'top_frame').document.parentWindow.execScript
(js, "javascript")


Please note the last line. Here is an alternative that did NOT work.

  @browser.document.parentWindow.execScript(js, "javascript")  # seems
to work, but no event object is available to handler!!

Thanks and I hope this helps someone else.

On Mar 13, 5:54 am, Andy Sipe <ajs.gene...@gmail.com> wrote:
> Using this html file:
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 
> 4.01//EN""http://www.w3.org/TR/html4/strict.dtd";>
> <html>
>   <head>
>     <title>Sample</title>
>     <script type="text/javascript">
>       function doSomething(e) {
>         alert('inside preexisting hanlder');
>         alert((window.event ? '' : 'not ') + 'found : window.event');
>         alert((e ? '' : 'not ') + 'found : window.event');
>       }
>     </script>
>   </head>
>   <body>
>     <div>
>       <button id="one">One</button>
>       <button id="two">Two</button>
>     </div>
>   </body>
> </html>
>
> and this ruby code:
> ------------------------------------
> require 'watir'
> include Watir
>
> module Watir
>   class IE
>     def run_script(js)
>       ie.document.parentWindow.execScript(js)
>     end
>   end
> end
>
> ie = IE::start('c:/test.html')
> ie.button(:id, 'one').click                #does nothing - no handler
>
> ie.run_script("document.getElementById('one').onclick=doSomething")
> p 'clicking one'
> ie.button(:id, 'one').click                #shows doSomething handler
>
> ie.run_script("var func = function() {" +
>                 "alert(window.event ? 'found' : 'not found');"
>                 "if (window.event) alert(window.event.altKey);"+
>                "};" +
>                "document.getElementById('two').onclick=func");
> p 'clicking two'
> ie.button(:id, 'two').click                #shows injected handler
> #press two manually here with alt held down to see event info
> -----------------------
> See what you get.
>
> You mentioned IE, so I tried it on IE 6 (the only IE I have laying around).
>
> There are a lot of ways to attach event handlers in javascript and I'm not a
> javascript wizard by any means so I'm sure there are better ways.   Hope it
> helps.
>
> -andy
>
>
>
> On Thu, Mar 12, 2009 at 2:54 PM, <andrew.d...@lthree.com> wrote:
>
> > Yea, I've tried passing it as a parm with no luck also. Everything
> > says it is supposed to work, but I can't get it.
>
> > -~----------~----~----~----~------~----~------~--~---- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Watir General" group.
To post to this group, send email to watir-general@googlegroups.com
Before posting, please read the following guidelines: 
http://wiki.openqa.org/display/WTR/Support
To unsubscribe from this group, send email to 
watir-general-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/watir-general
-~----------~----~----~----~------~----~------~--~---

Reply via email to