robertwahler / win32-autogui

A Ruby Win32 GUI testing framework packaged as a RubyGem
http://www.gearheadforhire.com/articles/ruby/win32-autogui/using-ruby-to-drive-windows-applications
MIT License
52 stars 6 forks source link

What about Java applications? #8

Closed onknows closed 11 years ago

onknows commented 11 years ago

I tried to use win32-autogui on a Java application, basically it is an Eclipse application. Several method return the error message "execution expired"

Following how to http://www.gearheadforhire.com/articles/ruby/win32-autogui/using-ruby-to-drive-windows-applications

I do myapp = Myapp.new. My application starts, seems okay. But if do myapp.running? it shows

irb(main):003:0> myapp.running? Timeout::Error: execution expired from C:/Ruby193/lib/ruby/gems/1.9.1/gems/win32-autogui-0.5.2/lib/win32/a utogui/application.rb:213:in sleep' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/win32-autogui-0.5.2/lib/win32/a utogui/application.rb:213:inblock in main_window' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/win32-autogui-0.5.2/lib/win32/a utogui/application.rb:207:in main_window' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/win32-autogui-0.5.2/lib/win32/a utogui/application.rb:242:inrunning?' from (irb):3 from C:/Ruby193/bin/irb:12:in `

' irb(main):004:0>

Other methods for example myapp.main_window.window_class myapp.main_window.children.count also return the execution expired error message.

Is it possible to do Java applications? If so, how?

robertwahler commented 11 years ago

Win32-autogui will work with any windowed application regardless of programming language. As long as you can drive your app with a keyboard and you have unique, non-unicode window titles, you should be able to use the library.

The error you are getting means that the library can't find the main window. Did you set a 'title' in 'MyApp'? The title must 'match' exactly. Here is the code that is failing.

timeout(main_window_timeout) do
    begin
      # There may be multiple instances, use title and pid to id our main window
      @main_window = Autogui::EnumerateDesktopWindows.new.find do |w|
        w.title.match(title) && w.pid == pid
      end
      sleep 0.1
    end until @main_window
  end

Here is a snippet for setting the title of the main window from the calculator.rb spec example. Note Win32-autogui can't yet handle Unicode.

# initialize with the binary name 'calc' and the window title
# 'Calculator' used along with the application pid to find the
# main application window
def initialize(options = {})
  defaults = {
               :name => "calc",
               :title => "Calculator",
               :logger_level => Autogui::Logging::DEBUG
             }
  super defaults.merge(options)
end