# Watir Javascript popup handler: works with popups from links

**URL:** <https://www.ruby-forum.com/t/watir-javascript-popup-handler-works-with-popups-from-links/97194>\
**Category:** Ruby\
**Created:** [December 12, 2005, 2:09am UTC](https://www.ruby-forum.com/t/watir-javascript-popup-handler-works-with-popups-from-links/97194 "2005-12-12T02:09:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 12, 2005, 2:09am UTC](https://www.ruby-forum.com/t/watir-javascript-popup-handler-works-with-popups-from-links/97194/1 "2005-12-12T02:09:14Z")

</div>

(long post!)

Hello, I’m creating a little helper class for Watir that can read the  
text from an IE Javascript popup and click buttons. It works great for  
popups that are created by links. E.g., for the following HTML:

\<test\_html\>

Link:  
Click this link

I can put the following in a Watir script:

\<link\_code\>  
require ‘watir’  
@ie = Watir::IE.new  
@ie.goto(“file://C:/test.html”)

puts “Working with popup from links:”  
jsh = JavascriptPopupHandler.new(@ie, ‘OK’)  
jsh.pauseDuration = 0.1  
jsh.handle { @ie.link(:text, “Click this link”).click }  
puts “Here’s the text that was showing in the popup: #{jsh.text}”  
\</link\_code\>

The above code outputs:  
  
Working with popup from links:  
Here’s the text that was showing in the popup: Alert showing!

The jsh.handle starts a monitor thread that looks for the Javascript  
popup, and then executes the block. When it finds a popup, it can pull  
the text out of the popup and click the appropriate button (by the  
button caption). It works when I pass in a link click in the block.

The code doesn’t work for form buttons though, and I can’t figure out  
why. E.g., for the following html:

The following doesn’t work:  
\<form\_watir\_code\>

# This next call hangs when the popup pops up.

puts “from button:”  
jsh.handle { @ie.button(:value, “Click this button”).click }  
puts “Here’s the text that was showing in the popup: #{jsh.text}”  
\</form\_watir\_code\>

This is a bit annoying, and it might be easily fixable … I was hoping  
someone might have some ideas about how to fix it.

Here’s the scratch code:

===========================================

require ‘watir/winClicker.rb’

# Need to redefine the supplied getWindowHandle in Winclicker

class WinClicker

# Stop printing to console.

def puts(s)  
# nothing  
end

# getWindowHandle

# Purpose: return the handle to the window with desired title and

class.

# Returns -1 if no match.

# 

# Code is taken from winClicker.rb and modified.

# Note: Needed to redefine getWindowHandle, since it returns a window

with

# a caption that matches using a regex, instead of an exact match.

# 

def getWindowHandle(title, winclass = “” )

```
winclass.strip!

# Callbacks
callback_enum_windows = @User32['EnumWindows', 'IPL']
callback_get_class_name = @User32['GetClassName', 'ILpI']
callback_get_caption_length = @User32['GetWindowTextLengthA' ,'LI'

```

]  
# format here - return value type (Long) followed by parameter  
types - int in this case -  
# see  
[http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/~checkout~/ruby/ext/dl/doc/dl.txt](http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/~checkout~/ruby/ext/dl/doc/dl.txt)?

```
callback_get_caption = @User32['GetWindowTextA', 'iLsL']

len = 32 # seemingly arbitrary number
buff = " " * len

bContinueEnum = -1

# Main callback - block of code to execute for all open windows.
callback_enum_windows_proc = DL.callback('ILL') do |hwnd,lparam|

  # sleep 0.05 <-- original code had sleep in here, not sure why

```

…  
r, rs = callback\_get\_class\_name.call(hwnd, buff, buff.size)  
found\_win\_class = rs[1].to\_s.strip

```
  if ( winclass == "") or ( found_win_class == winclass )
    # Get the caption (allocate buffer space, then load)
    caption_length, a = callback_get_caption_length.call(hwnd)
    captionBuffer = " " * (caption_length + 1)
    t, textCaption = callback_get_caption.call(hwnd,

```

captionBuffer, caption\_length + 1)  
the\_caption = textCaption[1].to\_s

```
    return hwnd if (title == the_caption)
  end

  bContinueEnum # return value
end # do

# execute the callback for all open windows.
r,rs = callback_enum_windows.call(callback_enum_windows_proc, 0)
return bContinueEnum

```

end

end

# end WinClicker redefinition

class JavascriptPopupHandler

JS\_POPUP\_TITLE = “Microsoft Internet Explorer”  
DEFAULT\_PAUSE\_BEFORE\_FINDING\_WINDOW\_DURATION = 0.8

def initialize(parent\_ie\_session, caption\_of\_button\_to\_push)  
@parent\_hwnd = parent\_ie\_session.ie.hwnd  
@caption\_of\_button\_to\_push = caption\_of\_button\_to\_push  
@handle\_was\_called = false  
@popup\_text = nil  
@pause\_before\_finding\_popup =  
DEFAULT\_PAUSE\_BEFORE\_FINDING\_WINDOW\_DURATION  
end

def pauseDuration=(duration =  
DEFAULT\_PAUSE\_BEFORE\_FINDING\_WINDOW\_DURATION)  
raise “Duration must be \>=0” if (duration \< 0)  
@pause\_before\_finding\_popup = duration  
end

# handle

# Does the action which displays the popup, then gets the popup

# text and clicks the appropriate button.

def handle( &action )  
@handle\_was\_called = true

```
# Calling the action blocks until the popup dialog box is closed,
# so we need to create a thread that looks out for the dialog box
# before the action is executed.
Thread.new do
  sleep @pause_before_finding_popup

  wc = WinClicker.new
  javascriptPopupHwnd = wc.getWindowHandle(JS_POPUP_TITLE)

  raise "No popup matching #{JS_POPUP_TITLE} found" if

```

(javascriptPopupHwnd == -1)  
raise “Got wrong popup?” if (wc.getParent(javascriptPopupHwnd) !=  
@parent\_hwnd)

```
  # Get the button
  button_hwnd = wc.getChildHandle(javascriptPopupHwnd,

```

@caption\_of\_button\_to\_push)  
raise “Can’t find button #{@caption\_of\_button\_to\_push}” if  
(button\_hwnd == -1)

```
  # Store info about the popup.
  @popup_text = wc.getStaticText_hWnd(javascriptPopupHwnd)

  # Click the button to continue.
  wc.clickButtonWithHandle(button_hwnd)
end

# The thread is now looking for a popup dialog, so when we do
# the action and the dialog box appears, the thread will
# pick it up.
action.call

```

end

# After the action has occurred, and the handler has clicked

# the appropriate button, we can get information about the popup.

def text  
raise “Handle not called yet” if (@handle\_was\_called == false)  
@popup\_text  
end

end

require ‘watir’

@ie = Watir::IE.new  
@ie.goto(“file://C:/test.html”)

puts “Working with popup from links:”  
jsh = JavascriptPopupHandler.new(@ie, ‘OK’)  
jsh.pauseDuration = 0.1  
jsh.handle { @ie.link(:text, “Click this link”).click }  
puts “Here’s the text that was showing in the popup: #{jsh.text}”

# This next call hangs when the popup pops up.

puts “from button:”  
jsh.handle { @ie.button(:value, “Click this button”).click }  
puts “Here’s the text that was showing in the popup: #{jsh.text}”

@ie.close

===========================================

Here’s the test HTML file:

===========================================

testing
# testing

Handler can deal with the link:  
Click this link

* * *

Handler can't deal with the form submit button:

===========================================

Any insight would be appreciated!  
Jeff

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 11, 2008, 3:16pm UTC](https://www.ruby-forum.com/t/watir-javascript-popup-handler-works-with-popups-from-links/97194/2 "2008-02-11T15:16:44Z")

</div>

Jeff,

I can get it to not hang, by changing .click to .click\_no\_wait and  
adding the @ie.waitForIE. The only problem is I still cannot get the  
text.

puts “Working with popup from links:”  
jsh = JavascriptPopupHandler.new(@ie, ‘OK’)  
jsh.pauseDuration = 0.1  
jsh.handle {@ie.button(:type, “submit”).click\_no\_wait}  
puts “Here’s the text that was showing in the popup: #{jsh.text}”

@ie.waitForIE

Darin
