just_add_watir | Advanced recipes for Watir
21Aug/08Off

Q. How do I get the text displayed in a javascript popup?

A. Use the WIN32OLE extension library with the autoit function library ...
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("This is the text in the popup")
@b.button(:name,"submit").click_no_wait
autoit.WinWaitActive("[Class:#32770]")
text = autoit.ControlGetText("[Class:#32770]", "", "Static2")
autoit.ControlClick("[Class:#32770]","","Button1")
puts text

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 the static text.

21Aug/08Off

How do I get the URL in IE?

A. Use the autoit function library with the win32OLE extension library...
You will need to install autoit. You can download it here, once installed you can call the function library using the win32OLE extension library.

require 'watir'
require 'rubygems'
require 'win32ole'
autoit = WIN32OLE.new('AutoItX3.Control')
url = autoit.ControlGetText("[CLASS:IEFrame]", "", "Edit1")
puts "url is " + url

The defintion for the autoit function 'ControlGetText' is ControlGetText(title, text, controlId)
You can use the "AutoIt Window Info" tool to find the title and controlId, you do not need to use the 'text' parameter. I find that using the Class instead of the window title a neater and more accurate solution.