Class: Bewildr::Application

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attach_or_launch(path_to_exe) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/bewildr/application.rb', line 82

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



78
79
80
# File 'lib/bewildr/application.rb', line 78

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

.attach_to_process_name(process_name) ⇒ Object



74
75
76
# File 'lib/bewildr/application.rb', line 74

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



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

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



107
108
109
# File 'lib/bewildr/application.rb', line 107

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

takes name or full path of exe to start



61
62
63
64
# File 'lib/bewildr/application.rb', line 61

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



94
95
96
97
98
# File 'lib/bewildr/application.rb', line 94

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



66
67
68
69
70
71
72
# File 'lib/bewildr/application.rb', line 66

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



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

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



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

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

#running?Boolean

Returns:

  • (Boolean)


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

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

#wait_for_terminationObject



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

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

#wait_for_window(input, wait_time = 30) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bewildr/application.rb', line 43

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

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



33
34
35
36
37
38
39
40
# File 'lib/bewildr/application.rb', line 33

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



29
30
31
# File 'lib/bewildr/application.rb', line 29

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