Module: WinGui

Extended by:
Win::Gui
Includes:
Win::Gui
Defined in:
lib/win_gui/convenience.rb,
lib/version.rb,
lib/win_gui.rb,
lib/win_gui/app.rb,
lib/win_gui/window.rb

Overview

Module contains Win32 API gui-related functions as both module and instance methods. See documentation of Win::Gui module of win gem for a full scope of available functions. In addition, module defines several higher-level convenience methods that can be useful when dealing with GUI-related tasks on Windows platform (such as testing automation).

Defined Under Namespace

Modules: Errors Classes: App, Window

Constant Summary collapse

VERSION_FILE =

:nodoc:

Pathname.new(__FILE__).dirname + '../VERSION'
VERSION =
VERSION_FILE.exist? ? VERSION_FILE.read.strip : nil
KEY_DELAY =

Delay between key commands/events (in sec)

0.00001
SLEEP_DELAY =

Delay when sleeping (in sec)

0.001
CLOSE_TIMEOUT =

Timeout waiting for Window to be closed (in sec)

1
LOOKUP_TIMEOUT =

Default timeout for dialog operations (in sec)

3
DIALOG_WINDOW_CLASS =

Windows class identifying standard modal dialog window

'#32770'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.require_libs(libs, opts = {}) ⇒ Object

Requires ruby source file(s). Accepts either single filename/glob or Array of filenames/globs. Accepts following options:

:file

Lib(s) required relative to this file - defaults to __FILE__

:dir

Required lib(s) located under this dir name - defaults to gem name



15
16
17
18
19
20
21
# File 'lib/win_gui.rb', line 15

def self.require_libs( libs, opts={} )
  file = Pathname.new(opts[:file] || __FILE__)
  [libs].flatten.each do |lib|
    name = file.dirname + (opts[:dir] || file.basename('.*')) + lib.gsub(/(?<!.rb)$/, '.rb')
    Pathname.glob(name.to_s).sort.each {|rb| require rb}
  end
end

Instance Method Details

#dialog(opts = {}) ⇒ Object

Finds top-level dialog window by title and yields found dialog window to attached block. We work with dialog window in a block, and then we wait for it to close before proceeding. That is, unless your block returns nil, in which case dialog is ignored (and method immediately returns nil?). If no block is given, method just returns found dialog window (or nil if dialog is not found). Options:

:title

dialog title

:class

dialog class - default DIALOG_WINDOW_CLASS

:timeout

timeout (seconds) - default LOOKUP_TIMEOUT (3)

:raise

raise this exception instead of returning nil if nothing found



36
37
38
39
40
41
42
# File 'lib/win_gui/convenience.rb', line 36

def dialog(opts={})  # :yields: dialog_window
  dialog = Window.top_level( {class: DIALOG_WINDOW_CLASS, timeout: LOOKUP_TIMEOUT}.merge opts )
  dialog.set_foreground_window if dialog
  wait = block_given? ? yield(dialog) : false
  dialog.wait_for_close if dialog && wait
  dialog
end

#keystroke(*keys) ⇒ Object

Emulates combinations of (any amount of) keys pressed one after another (Ctrl+Alt+P) and then released. *keys should be a sequence of a virtual-key codes (value in the range 1 to 254). For a complete list, see msdn:Virtual Key Codes. If alphanumerical char is given instead of virtual key code, only lowercase letters result (no VK_SHIFT!).



49
50
51
52
53
54
55
56
57
# File 'lib/win_gui/convenience.rb', line 49

def keystroke(*keys)
  return if keys.empty?
  key = String === keys.first ? keys.first.upcase.ord : keys.first.to_i
  keybd_event key, 0, KEYEVENTF_KEYDOWN, 0
  sleep KEY_DELAY
  keystroke *keys[1..-1]
  sleep KEY_DELAY
  keybd_event key, 0, KEYEVENTF_KEYUP, 0
end

#type_in(message) ⇒ Object

Types text message into a window currently holding the focus



61
62
63
64
65
# File 'lib/win_gui/convenience.rb', line 61

def type_in(message)
  message.scan(/./m) do |char|
    keystroke(*char.to_key)
  end
end