Class: Bewildr::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/bewildr/application.rb

Overview

Wraps System::Diagnostics::Process object and provides class methods to interrogate currently running processes, attach to processes or start new ones

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attach_or_launch(path_to_exe) ⇒ Object

Given an exe name/path this method will return a new Bewildr::Application based either on a process already in memory or the process started by executing the app at the path specified in the argument



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/bewildr/application.rb', line 116

def self.attach_or_launch(path_to_exe)
  #if app is already open attach to it
  potential_process_name = File.basename(path_to_exe, ".exe")
  if Bewildr::Application.processes_with_name(potential_process_name).size > 0
    #app is running, attach to it
    Bewildr::Application.attach_to_process_name(potential_process_name)
  else
    #app is not running, start a new instance
    Bewildr::Application.start(path_to_exe)
  end
end

.attach_to_process(process) ⇒ Object

Returns a Bewildr::Application wrapping a process already in memory where the argument is the process object to be wrapped



110
111
112
# File 'lib/bewildr/application.rb', line 110

def self.attach_to_process(process)
  Bewildr::Application.new(process)
end

.attach_to_process_id(process_id) ⇒ Object

Returns a Bewildr::Application wrapping a process already in memory where the argument is the process id



105
106
107
# File 'lib/bewildr/application.rb', line 105

def self.attach_to_process_id(process_id)
   Bewildr::Application.new(System::Diagnostics::Process.get_process_by_id(process_id))
end

.attach_to_process_name(process_name) ⇒ Object

Returns a Bewildr::Application wrapping a process already in memory where the argument is the process name



100
101
102
# File 'lib/bewildr/application.rb', line 100

def self.attach_to_process_name(process_name)
  Bewildr::Application.new(System::Diagnostics::Process.get_processes_by_name(process_name).first)
end

.kill_all_processes_with_name(input) ⇒ Object

Kills all processes that match the name passed as an argument



137
138
139
140
141
142
# File 'lib/bewildr/application.rb', line 137

def self.kill_all_processes_with_name(input)
  System::Diagnostics::Process.get_processes_by_name(input).each do |p|
    p.kill
    p.wait_for_exit
  end
end

.processes_with_name(input) ⇒ Object

Returns an array of Bewildr::Application instances wrapping processes whose name match the string passed as an argument



146
147
148
# File 'lib/bewildr/application.rb', line 146

def self.processes_with_name(input)
  System::Diagnostics::Process.get_processes_by_name(input).collect {|p| Bewildr::Application.attach_to_process(p) }
end

.start(process_name) ⇒ Object

Returns a new instance of Bewildr::Application wrapping the process created when the specified exe file is invoked.

Application.start("notepad.exe")
Application.start("c:\some\path\my_app.exe")


85
86
87
88
# File 'lib/bewildr/application.rb', line 85

def self.start(process_name)
  raise "Can't find: #{process_name}" unless File.exist?(process_name)
  Bewildr::Application.new(System::Diagnostics::Process.start(process_name))
end

.start_app_and_wait_for_window(path, window_name) ⇒ Object

Starts the app specified in the first argument and then waits and returns the window that matches the string or regex passed as the second argument



130
131
132
133
134
# File 'lib/bewildr/application.rb', line 130

def self.start_app_and_wait_for_window(path, window_name)
  app = Bewildr::Application.start(path)
  window = app.wait_for_window(window_name)
  return app, window
end

.start_with_settings(path_to_exe, settings_hash) ⇒ Object

Same as the start method but allows the passing of arguments



91
92
93
94
95
96
97
# File 'lib/bewildr/application.rb', line 91

def self.start_with_settings(path_to_exe, settings_hash)
  start_info = System::Diagnostics::ProcessStartInfo.new(path_to_exe)
  unless settings_hash[:args].nil? and settings_hash[:args].size > 0
    start_info.arguments = settings_hash[:args].collect {|arg| arg.to_s}.join(" ")
  end
  Bewildr::Application.new(System::Diagnostics::Process.start(start_info))
end

.wait_for_process_with_name(input) ⇒ Object

Waits for and eventually returns a Bewildr::Application wrapping the process whose name matches the string passed as an argument



152
153
154
155
# File 'lib/bewildr/application.rb', line 152

def self.wait_for_process_with_name(input)
  Timeout.timeout(30) {sleep 0.1 until System::Diagnostics::Process.get_processes_by_name(input).size > 0}
  self.attach_to_process_name(input)
end

Instance Method Details

#killObject

kills this process. Uses taskkill to perform the butchery



30
31
32
33
34
35
# File 'lib/bewildr/application.rb', line 30

def kill
  `taskkill /f /t /pid #{proc_id}`
  Timeout::timeout(5) do
    sleep 0.1 while running?
  end
end

#nameObject

Return the name of the underlying process



25
26
27
# File 'lib/bewildr/application.rb', line 25

def name
  running? ? @proc.process_name.to_s : nil
end

#proc_idObject

Returns the id of the underlying process



20
21
22
# File 'lib/bewildr/application.rb', line 20

def proc_id
  running? ? @proc.id.to_i : nil
end

#processObject

Returns the underlying process wrapped by this Application object



15
16
17
# File 'lib/bewildr/application.rb', line 15

def process
  @proc
end

#running?Boolean

Returns true if this process is running, false if it isn’t

Returns:

  • (Boolean)


38
39
40
# File 'lib/bewildr/application.rb', line 38

def running?
  @proc.nil? ? false : !@proc.has_exited
end

#wait_for_terminationObject

Waits for up to 30 seconds for the process to terminate



43
44
45
46
47
# File 'lib/bewildr/application.rb', line 43

def wait_for_termination
  Timeout::timeout(30) do
    sleep 0.2 while running?
  end
end

#wait_for_window(input, wait_time = 30) ⇒ Object

Waits for up to 30 seconds for a window belonging to this application to appear. When it does, a Bewildr::Element representing the window is returned. This method allows you in one line to wait for a window to appear and also return it



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/bewildr/application.rb', line 68

def wait_for_window(input, wait_time = 30)
  begin
    Timeout::timeout(wait_time) do
      loop do
        my_window = window(input)
        return my_window unless my_window.nil?
        sleep 0.2
      end
    end
  rescue Timeout::Error
    raise Bewildr::ElementDoesntExist
  end
end

#window_by_name(input) ⇒ Object Also known as: window

Returns a window whose title matches the string or regex passed in as the argument



55
56
57
58
59
60
61
62
# File 'lib/bewildr/application.rb', line 55

def window_by_name(input)
  case input
  when String then return windows.select{|window| window.name.strip == input.strip}.first
  when Regexp then return windows.select{|window| input.match(window.name.strip).nil? == false}.first
  else
    raise ArgumentError, "input not a string or regexp but a #{input.class}"
  end
end

#windowsObject

Returns a list of windows associated with this application



50
51
52
# File 'lib/bewildr/application.rb', line 50

def windows
  Bewildr::Windows.windows_by_process_id(proc_id)
end