[Watir] control the popup windows created using JavaScript

Source: Internet
Author: User

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

They wocould 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 popupsdef 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)    endend## MAIN APPLICATION CODE# Setup popup handler$popup = Thread.new { check_for_popups }  # start popup handlerat_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  endend## 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 hangbrowser.button(:id, /someText/).click_no_waithwnd = browser.enabled_popup(5)if (hwnd)  #yeah! a popup  popup = WinClicker.new  popup.makeWindowActive(hwnd)  popup.clickWindowsButton("Windows Internet Explorer", "OK", "30")end

Solution #5-Summary-09 July 2008

I spent some time trying to figure out how to dismiss JavaScript popups. the information is out there, but it took a while to get it all put together and then to try out the various suggestions until I found one that worked for me. here's a summary in case it helps someone else.

1 .) you need to update the winclicker. RB file in your watir directory (in my case, it was at: C:/Ruby/lib/Ruby/gems/1.8/gems/watir-1.5.3/watir ). change the dialog title from "Internet Explorer" to "Windows Internet Explorer ". I used the RegEx/Internet Explorer/so that it will work no matter which version of ie I use.

2.) require 'watir/contrib/enabled_popup' for your tests.

3.) define the popupchecker method to watch for a popup and dismiss it (in this case, by clicking the button with the specified text ):

def popupChecker(text)    Timeout::timeout(2)do        begin            if ie.enabled_popup                hwnd = ie.enabled_popup(5)                w = WinClicker.new                w.makeWindowActive(hwnd)                w.clickWindowsButton_hWnd(hwnd,text)            end        rescue Timeout::Error            puts 'No popup existed'        end    endend

4.) use the click_no_wait method on the link or button that will launch the popup.

ie.button(:text, 'Continue').click_no_wait

5 .) call the popupchecker method in case a popup exists. you may run into timing problems with the watir actions that follow the popupchecker. to get around this, I added an IE. wait statement after calling the popupchecker method.

popupChecker('OK')ie.wait

6 .) some popups are launched by selecting an item from a select_list. since the click_no_wait method is required on the action that launches the popup, you need something like a select_no_wait Method for a select list. charley Baker provided me with this method:

#select_no_wait method for select lists - needed for popups resulting from select listsmodule Watir    class Element        #select_no_wait - selects a drop-down element spawning a new process.        #this is needed to close potential pop-ups that select drop-down can trigger.        def select_no_wait(item)            assert_enabled            highlight(:set)            object = "#{self.class}.new(self, :unique_number,#{self.unique_number})"            @page_container.eval_in_spawned_process(object + ".select('#{item}')")            highlight(:clear)        end    endend

7.) Keep people has the problem about click "OK/cancel" or "OK" in the Pop Up. I think the code below can deal with the problem

require 'watir'def check_for_popups(title="Window Internet Explorer", button="OK")    popup=Thread.new {        autoit=WIN32OLE.new('AutoItX3.Control')        ret=autoit.WinWait(title,"",60)        if (ret==1)            puts "There is popup."            autoit.WinActivate(title)            button.downcase!            if button.eql?("ok") || button.eql?("yes") || button.eql?("continue")                autoit.Send("{Enter}")            else                autoit.Send("{tab}")                autoit.Send("{Enter}")            end        elsif (ret==0)            puts "No popup, please check your code."        end    }    at_exit { Thread.kill(popup) }end$ie.link(:text,//).click_no_waitcheck_for_popups("Window Internet Explorer", "OK")

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.