I played around with win32-utils code, but I was getting odd results.  
(It appeared that the fork() was really exec()ing the program in a  
subprocess, rather than continuing the subprogram from the point of  
the fork.) So I went back to popen.

I ran into a further problem. One button press would generate two  
confirmation dialogs, one after the other, without control returning  
to Watir between them. So I needed to invent some new syntax. I'm not  
wildly fond of it, but it works. It looks like this:

$ie.after {
   button(:name, "action").click
}.dismiss_windows(
         "Choose a digital certificate" => ["{Tab}", "{Space}"],
         "Signing" => ["{Space}"]
   )

Code below. I should write tests for it. Realistically, though, I  
won't have time to be a good submitter for the near future. I have to  
learn both Rails and Selenium next week for a different client, and  
I'll be going flat out for at least all of June.

This code should probably invoke the watcher scripts using ruby -S.  
Right now, it assumes the watcher script is in the same directory as  
the test, which is sloppy.

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


class IE
   def after(&block)
     @block = block
     self
   end

   def grab_window(window_title)
     @window_title = window_title
     self
   end

   def and_send(*keys)
     launch_watcher(@window_title, keys)
     continue_script(@block)
   end


   def dismiss_windows(hash)
     hash.each do | title, keys |
       launch_watcher(title, keys)
     end
     continue_script(@block)
   end

   private

   def launch_watcher(window_title, keys)
     commandline = ["/ruby/bin/ruby",
                           "watcher-popen.rb",
                           "'" + window_title + "'"] +
                           keys
     IO.popen(commandline.join(' '))
   end

   def continue_script(block)
     instance_eval(&block) if block
   end

end

==== watcher.rb =====

require 'watir'

# Do this in JMock style, just because DSLs are all the rage.

class Watcher

   private_class_method :new

   def self.after_seeing(title)
     new(title)
   end

   def initialize(title)
     @autoit = Watir.autoit
     @title = title
   end

   def send(keys)
     @keys = keys
     do_await_window
     do_send_keys
     self
   end

   private

   def do_await_window
     @autoit.WinWait @title, ""
   end

   def do_send_keys
     @keys.each do | key |
       sleep 1  # Just to watch it happen.
       @autoit.Send key
     end
   end

end

if $0 == __FILE__
   title = ARGV[0]
   keys = ARGV[1..-1]
   # $stderr.puts title, keys.inspect; $stderr.flush

   Watcher.after_seeing(title).send(keys)
end


-----
Brian Marick, independent consultant
Mostly on agile methods with a testing slant
www.exampler.com, www.exampler.com/blog


_______________________________________________
Wtr-general mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/wtr-general

Reply via email to