just_add_watir | Advanced recipes for Watir
21Aug/08Off

Q. How do I deal with javascript popup's?

A. Invoke the autoit function library with the .click_no_wait method...
You will need to install autoit. You can download it here, once installed you can call the function library using the win32OLE extension library.
Example html:

<html>
   <head>
   <script language="javascript">
      function msgbox (textstring) {
      alert (textstring) }
   </script>
   </head>
   <body id="test_00test_0107" onload="">
      <form>
         <input name="text1" type=text>
         <input name="submit" type=button value="show me" onclick="msgbox(form.text1.value)">
     </form>
  </body>
</html>

Example watir:

require 'watir'
require 'rubygems'
require 'win32ole'
 
autoit = WIN32OLE.new('AutoItX3.Control')
@b.goto('http://justaddwatir.com/watir/test_html/tc_0101_0200/test_0107.html')
@b.text_field(:name,"text1").set("Justaddwatir")
@b.button(:name,"submit").click_no_wait
autoit.WinWaitActive("[Class:#32770]")
result =autoit.ControlClick("[Class:#32770]","","Button1")
puts "successful click =1 unsuccessful =0, the result was "+ result

In order for the autoit function to execute, you need to use the .click_no_wait method in watir before the autoit function. This is because once the pop up is presented, the focus comes off the IE window and the script will pause. The .click_no_wait tells the script to continue running regardless of what happens next.
You may use the autoit window identifier to get the properties of the pop up and the button attributes.

Comments (2) Trackbacks (1)
  1. How can you handle pop-ups that arent javascript dialogues or alerts? Just a vanilla pop-up, like adding a target attribute to a link?

  2. Hi,

    I run same script in my system. while executing autoit.WinWaitActive() function it’s taking more time. So can you please explain how WinWait Active() control function will work in above script.

    autoit.WinWaitActive(“[Class:#32770]“) what is class identifier in this script?

    Thanks/babu

Trackbacks are disabled.