Dashboard > Watir > ... > Pop Ups > JavaScript Pop Ups
Watir Log In View a printable version of the current page.
JavaScript Pop Ups
Added by Zeljko, last edited by Mike Badger on Apr 28, 2008  (view change)
Labels: 
(None)

JavaScript Pop Ups

These are pop ups created using javascript. The ones below come from the watir unit tests.

They would be created using javascript, and an example is shown under each of them.

<input type = button onClick = 'javascript:x = confirm('Do you really want to do this');">

<input type = button onClick = 'javascript:y = prompt('Enter Something Useful');">

<input type = button onClick = 'javascript:z = alert('This is an alert box');">

Solution #1
http://rubyforge.org/pipermail/wtr-general/2005-April/001461.html

Solution #2

# Auto Popup Handler. Posted by brad@longbrothers.net
#
require 'win32ole'  # already included if you use 'require watir'
#
# Function to look for popups
def check_for_popups
    autoit = WIN32OLE.new('AutoItX3.Control')
    #
    # Do forever - assumes popups could occur anywhere/anytime in your application.
    loop do
        # Look for window with given title. Give up after 1 second.
        ret = autoit.WinWait('Popup Window Title', '', 1)
        #
        # If window found, send appropriate keystroke (e.g. {enter}, {Y}, {N}).
        if (ret==1) then autoit.Send('{enter}') end
        #
        # Take a rest to avoid chewing up cycles and give another thread a go.
        # Then resume the loop.
        sleep(3)
    end
end
#
# MAIN APPLICATION CODE
# Setup popup handler
$popup = Thread.new { check_for_popups }  # start popup handler
at_exit { Thread.kill($popup) }           # kill thread on exit of main application
#
# Main application code follows
# ...

Solution #3

# A Better Popup Handler using the latest Watir version. Posted by Mark_cain@rl.gov
#
require 'watir\contrib\enabled_popup' 
#
def startClicker( button , waitTime= 9, user_input=nil )
  # get a handle if one exists
  hwnd = $ie.enabled_popup(waitTime)  
  if (hwnd)  # yes there is a popup
    w = WinClicker.new
    if ( user_input ) 
      w.setTextValueForFileNameField( hwnd, "#{user_input}" )
    end
    # I put this in to see the text being input it is not necessary to work
    sleep 3         
    # "OK" or whatever the name on the button is
    w.clickWindowsButton_hwnd( hwnd, "#{button}" )
    #
    # this is just cleanup
    w=nil    
  end
end
#
# MAIN APPLICATION CODE
#
$ie = Watir::IE.start( "c:\test.htm" )

# This is whatever object that uses the click method.  
# You MUST use the click_no_wait method.
$ie.image( :id, '3' ).click_no_wait
#
# 3rd parameter is optional and is used for input and file dialog boxes.
startClicker( "OK ", 7 , "User Input" )
#
# Main application code follows
# ...

Solution #4

require 'Watir'
require 'watir/contrib/enabled_popup'

# Use click_no_wait to launch the popup or your script will hang
browser.button(:id, /someText/).click_no_wait 

hwnd = browser.enabled_popup(5)
if (hwnd)  #yeah! a popup
  popup = WinClicker.new
  popup.makeWindowActive(hwnd)
  popup.clickWindowsButton("Windows Internet Explorer", "OK", "30")
end
Go to contents, previous, next page.

Site running on a free Atlassian Confluence Community License granted to OpenQA. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.6 Build:#812 Aug 06, 2007) - Bug/feature request - Contact Administrators