overriding javascript

185 views
Skip to first unread message

aidy lewis

unread,
Jan 3, 2009, 4:05:18 PM1/3/09
to watir-...@googlegroups.com
Hi,

I am trying to override js to bypass a js dialog:

This is what I have got:

require 'watir'

ie = Watir::IE.new
ie.goto("http://www.w3schools.com/JS/tryit.asp?filename=tryjs_confirm")
#ie.frame(:name, "view").button(:value, "Display a confirm box").click
ie.document.body.parentElement.execScript("window.confirm=function(){return
true;}")

I am getting unknown property or method `execScript'

Could anyone please direct me?

Aidy

Charley Baker

unread,
Jan 3, 2009, 4:23:12 PM1/3/09
to watir-...@googlegroups.com
Execscript exists on the window object, drop the body call from your code and it should work: 



Charley Baker
blog: http://charleybakersblog.blogspot.com/
Project Manager, Watir, http://wtr.rubyforge.org
QA Architect, Gap Inc Direct

aidy lewis

unread,
Jan 3, 2009, 5:44:59 PM1/3/09
to watir-...@googlegroups.com
Hi Charley,

Thanks for the advice but I am stilling having trouble overriding the js script.

require 'watir'

ie = Watir::IE.new
ie.goto("http://www.w3schools.com/JS/tryit.asp?filename=tryjs_confirm")
ie.document.parentWindow.execScript("'disp_confirm=function{return
true;}', 'JavaScript'")
ie.frame(:name, "view").button(:value, "Display a confirm box").click_no_wait

Aidy

2009/1/3 Charley Baker <charle...@gmail.com>:

Jagdeep Jain

unread,
Jan 4, 2009, 11:51:52 PM1/4/09
to Watir General
Hi Aidy,

I have changed "disp_confirm" with the earlier one you have supplied
"window.confirm" and removed "JavaScript" from the end and it works
fine for me.

require 'watir'

ie = Watir::IE.new
ie.goto("http://www.w3schools.com/JS/tryit.asp?
filename=tryjs_confirm")
ie.maximize
ie.document.parentWindow.execScript("window.confirm=function(){return
true;}")
ie.frame(:name, "view").button(:value, "Display a confirm
box").click_no_wait

Thanks,
Jagdeep

On Jan 4, 3:44 am, "aidy lewis" <aidy.le...@googlemail.com> wrote:
> Hi Charley,
>
> Thanks for the advice but I am stilling having trouble overriding the js script.
>
> require 'watir'
>
> ie = Watir::IE.new
> ie.goto("http://www.w3schools.com/JS/tryit.asp?filename=tryjs_confirm")
> ie.document.parentWindow.execScript("'disp_confirm=function{return
> true;}', 'JavaScript'")
> ie.frame(:name, "view").button(:value, "Display a confirm box").click_no_wait
>
> Aidy
>
> 2009/1/3 Charley Baker <charley.ba...@gmail.com>:
>
> > Execscript exists on the window object, drop the body call from your code
> > and it should work:
> >http://msdn.microsoft.com/en-us/library/ms536420(VS.85).aspx
>
> > Charley Baker
> > blog:http://charleybakersblog.blogspot.com/
> > Project Manager, Watir,http://wtr.rubyforge.org
> > QA Architect, Gap Inc Direct
>
> > On Sat, Jan 3, 2009 at 1:05 PM, aidy lewis <aidy.le...@googlemail.com>

aidy lewis

unread,
Jan 5, 2009, 5:11:19 AM1/5/09
to watir-...@googlegroups.com
Hi Jagdeep,

I am still unable to override the confirm function. The result should
tell me that 'You pressed OK!'.

It seems that execScript is running as a DOS window flashes, however I
am just left with the dialog.

Do I need to change some js settings in IE.

Aidy

2009/1/5 Jagdeep Jain <jagdee...@gmail.com>:

Tony

unread,
Jan 8, 2009, 2:26:10 AM1/8/09
to Watir General
Hi Aidy,

Got the error -
The problem is the script disp_confirm is being called from a frame,
and its this frames disp_confirm and window_confirm, that should be
overridden.
So this should be done on the page and also for all frames. (which is
going to call your overridden function)

require 'watir'
ie = Watir::IE.new
ie.goto("http://www.w3schools.com/JS/tryit.asp?
filename=tryjs_confirm")
ie.maximize
# override the frames window.confirm
ie.frame(:name, "view").document.parentWindow.execScript
("window.confirm=function(){return true;}")
ie.frame(:name, "view").button(:value, "Display a confirm
box").click_no_wait

Wow .. great way to get rid of these confirm boxes.. only have to get
this to work on firewatir tooo...
Anyways I have tried this on IE8 RC1 - 8.0.6001.18344

- Tony

aidy lewis

unread,
Jan 8, 2009, 5:08:00 AM1/8/09
to watir-...@googlegroups.com
Hi Tony,

Certainly works, even though I have changed #click_no_wait to #click.

I think we need to put this example in the js pop-up wiki and we need
an example for alerts as well.

For Firewatir you could re-open the FireWatir class and use #$jssh_socket.send

class FireWatir::Firefox
def close_js_confirm
$jssh_socket.send("[js]\n", 0)
self.read_socket()
sleep 1
end
end

Aidy



2009/1/8 Tony <yno...@gmail.com>:

aidy lewis

unread,
Jan 8, 2009, 8:03:16 AM1/8/09
to watir-...@googlegroups.com
The trouble however with this method is that we will never know if it
executes the js or not or does what it is supposed to do.

Aidy

2009/1/8 aidy lewis <aidy....@googlemail.com>:

sai

unread,
Jan 8, 2009, 8:34:43 AM1/8/09
to Watir General
I tried this
require 'firewatir'

class Element
def close_js_confirm
assert_exist
@container.js_eval("window.confirm = function(){return true;}")
sleep 1
end
end

ff = FireWatir::Firefox.new
ff.goto("http://www.w3schools.com/JS/tryit.asp?
filename=tryjs_confirm")
ff.frame(:name, "view").button(:value, "Display a confirm
box").close_js_confirm
ff.frame(:name, "view").button(:value, "Display a confirm box").click

But it is not working for me. I tried the same js in firebug
navigating to that frame but in vain :(

Any ideas?

Regards,
Sai

On Jan 8, 6:03 pm, "aidy lewis" <aidy.le...@googlemail.com> wrote:
> The trouble however with this method is that we will never know if it
> executes the js or not or does what it is supposed to do.
>
> Aidy
>
> 2009/1/8 aidy lewis <aidy.le...@googlemail.com>:
>
>
>
> > Hi Tony,
>
> > Certainly works, even though I have changed #click_no_wait to #click.
>
> > I think we need to put this example in the js pop-up wiki and we need
> > an example for alerts as well.
>
> > For Firewatir you could re-open the FireWatir class and use  #$jssh_socket.send
>
> > class FireWatir::Firefox
> >  def close_js_confirm
> >    $jssh_socket.send("[js]\n", 0)
> >    self.read_socket()
> >    sleep 1
> >  end
> > end
>
> > Aidy
>
> > 2009/1/8 Tony <ynot...@gmail.com>:
>
> >> Hi Aidy,
>
> >> Got the error -
> >> The problem is the script disp_confirm is being called from a frame,
> >> and its this frames disp_confirm and window_confirm, that should be
> >> overridden.
> >> So this should be done on the page and also for all frames. (which is
> >> going to call your overridden function)
>
> >> require 'watir'
> >> ie = Watir::IE.new
> >> ie.goto("http://www.w3schools.com/JS/tryit.asp?
> >> filename=tryjs_confirm")
> >> ie.maximize
> >> # override the frames window.confirm
> >> ie.frame(:name, "view").document.parentWindow.execScript
> >> ("window.confirm=function(){return true;}")
> >> ie.frame(:name, "view").button(:value, "Display a confirm
> >> box").click_no_wait
>
> >> Wow .. great way to get rid of these confirm boxes.. only have to get
> >> this to work on firewatir tooo...
> >> Anyways I have tried this on IE8 RC1 - 8.0.6001.18344
>
> >> - Tony- Hide quoted text -
>
> - Show quoted text -

sai

unread,
Jan 8, 2009, 8:50:06 AM1/8/09
to Watir General
sorry forgot to mention that it is working in firebug. So I am not
sure why it is not workin in FireWatir
> > - Show quoted text -- Hide quoted text -
Reply all
Reply to author
Forward
0 new messages