# Win32ole called from watir script

**URL:** <https://www.ruby-forum.com/t/win32ole-called-from-watir-script/125603>\
**Category:** Ruby\
**Created:** [January 10, 2008, 3:01pm UTC](https://www.ruby-forum.com/t/win32ole-called-from-watir-script/125603 "2008-01-10T15:01:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shinji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c6cbf5/32.png) [@shinji](https://www.ruby-forum.com/u/shinji)\
**Post date:** [January 10, 2008, 3:01pm UTC](https://www.ruby-forum.com/t/win32ole-called-from-watir-script/125603/1 "2008-01-10T15:01:30Z")

</div>

I’ve been using WATiR to test a website. At one point, a VBScript  
dialogue is shown which needs a Yes No response.

I found that I can manually browse to the page, and then using the  
win32ole library, send a yes value to the dialogue.

require ‘win32ole’

wsh = WIN32OLE.new(‘WScript.shell’)  
wsh.AppActivate(‘INR Check’)  
wsh.SendKeys(‘y’)

however, if I use similar code within the script itself, it doesn’t seem  
to execute?

I’m nt quite sure how to proceed with this…

---

<div class="post-metadata">

**Author:** ![shinji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c6cbf5/32.png) [@shinji](https://www.ruby-forum.com/u/shinji)\
**Post date:** [January 11, 2008, 2:06pm UTC](https://www.ruby-forum.com/t/win32ole-called-from-watir-script/125603/2 "2008-01-11T14:06:02Z")

</div>

Max R. wrote:

> I’ve been using WATiR to test a website. At one point, a VBScript  
> dialogue is shown which needs a Yes No response.
> 
> I found that I can manually browse to the page, and then using the  
> win32ole library, send a yes value to the dialogue.
> 
> require ‘win32ole’
> 
> wsh = WIN32OLE.new(‘WScript.shell’)  
> wsh.AppActivate(‘INR Check’)  
> wsh.SendKeys(‘y’)
> 
> however, if I use similar code within the script itself, it doesn’t seem  
> to execute?
> 
> I’m nt quite sure how to proceed with this…

One thing I’ve been wondering is whether I should spawn a new process or  
thread in order to deal with this?

---

<div class="post-metadata">

**Author:** ![shinji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c6cbf5/32.png) [@shinji](https://www.ruby-forum.com/u/shinji)\
**Post date:** [January 11, 2008, 2:42pm UTC](https://www.ruby-forum.com/t/win32ole-called-from-watir-script/125603/3 "2008-01-11T14:42:35Z")

</div>

Max R. wrote:

> Max R. wrote:
> 
> > I’ve been using WATiR to test a website. At one point, a VBScript  
> > dialogue is shown which needs a Yes No response.
> > 
> > I found that I can manually browse to the page, and then using the  
> > win32ole library, send a yes value to the dialogue.
> > 
> > require ‘win32ole’
> > 
> > wsh = WIN32OLE.new(‘WScript.shell’)  
> > wsh.AppActivate(‘INR Check’)  
> > wsh.SendKeys(‘y’)
> > 
> > however, if I use similar code within the script itself, it doesn’t seem  
> > to execute?
> > 
> > I’m nt quite sure how to proceed with this…
> 
> One thing I’ve been wondering is whether I should spawn a new process or  
> thread in order to deal with this?

I use similar code in some of my scripts:

sleep(1)  
if ws.AppActivate(“Security Alert”)  
ws.SendKeys “{ENTER}”  
sleep(1)  
end

It runs within the script’s main thread, following the code that might  
trigger the security alert dialog.

Note that WScript.Shell’s AppActivate method returns true if it was able  
to activate the window, and false otherwise. You can use this to  
determine if the AppActivate method was successful before attempting the  
SendKeys method. You may also need to toss in a momentary sleep to allow  
the dialog to appear before calling AppActivate.

There may be a more elegant solution (loop with timeout, watir method,  
etc.), but this is successful for me.

Hope that helps.

David

> **[Ruby on Windows](http://rubyonwindows.blogspot.com)**
>
> On using the Ruby programming language on the Microsoft Windows platform
