Package Peach :: Package Agent :: Module gui
[hide private]

Source Code for Module Peach.Agent.gui

  1   
  2  ''' 
  3  Some GUI related monitors. 
  4   
  5  @author: Michael Eddington 
  6  @version: $Id$ 
  7  ''' 
  8   
  9  # 
 10  # Copyright (c) 2008 Michael Eddington 
 11  # 
 12  # Permission is hereby granted, free of charge, to any person obtaining a copy  
 13  # of this software and associated documentation files (the "Software"), to deal 
 14  # in the Software without restriction, including without limitation the rights  
 15  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 16  # copies of the Software, and to permit persons to whom the Software is  
 17  # furnished to do so, subject to the following conditions: 
 18  # 
 19  # The above copyright notice and this permission notice shall be included in     
 20  # all copies or substantial portions of the Software. 
 21  # 
 22  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
 23  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
 24  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
 25  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 26  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 27  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 28  # SOFTWARE. 
 29  # 
 30   
 31  # Authors: 
 32  #   Michael Eddington (mike@phed.org) 
 33   
 34  # $Id$ 
 35   
 36  try: 
 37           
 38          import win32gui, win32con 
 39          import sys,time, os, signal 
 40          from threading import * 
 41          from Peach.agent import * 
 42           
43 - class _WindowWatcher(Thread):
44 45 CloseWindows = False 46 FoundWindowEvent = None # Will be Event() 47 WindowNames = None # Will be [] 48 StopEvent = None # Will be Event() 49
50 - def enumCallback(hwnd, self):
51 #print "!" 52 title = win32gui.GetWindowText(hwnd) 53 54 for name in _WindowWatcher.WindowNames: 55 if title.find(name) > -1: 56 try: 57 _WindowWatcher.FoundWindowEvent.set() 58 59 if _WindowWatcher.CloseWindows: 60 win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0) 61 except: 62 pass 63 64 return True
65 enumCallback = staticmethod(enumCallback) 66
67 - def run(self):
68 while not _WindowWatcher.StopEvent.isSet(): 69 win32gui.EnumWindows(_WindowWatcher.enumCallback, self) 70 time.sleep(.2)
71 72
73 - class PopupWatcher(Monitor):
74 ''' 75 Will watch for specific dialogs and optionally kill 76 or log a fault when detected. 77 ''' 78
79 - def __init__(self, args):
80 ''' 81 Constructor. Arguments are supplied via the Peach XML 82 file. 83 84 @type args: Dictionary 85 @param args: Dictionary of parameters 86 ''' 87 88 # Our name for this monitor 89 self._name = "PopupWatcher" 90 self._closeWindows = False 91 self._triggerFaults = False 92 93 if args.has_key("CloseWindows"): 94 if args["CloseWindows"].lower() in ["yes", "true", "1"]: 95 self._closeWindows = True 96 97 if args.has_key("TriggerFaults"): 98 if args["TriggerFaults"].lower() in ["yes", "true", "1"]: 99 self._triggerFaults = True 100 101 if not args.has_key("WindowNames"): 102 raise Exception("PopupWatcher requires a parameter named WindowNames.") 103 104 self._names = args["WindowNames"].split(',')
105
106 - def OnTestStarting(self):
107 ''' 108 Called right before start of test case or variation 109 ''' 110 111 _WindowWatcher.CloseWindows = self._closeWindows 112 _WindowWatcher.FoundWindowEvent = Event() 113 _WindowWatcher.WindowNames = self._names 114 _WindowWatcher.StopEvent = Event() 115 116 self._thread = _WindowWatcher() 117 self._thread.start()
118
119 - def OnTestFinished(self):
120 ''' 121 Called right after a test case or varation 122 ''' 123 self._thread.StopEvent.set() 124 time.sleep(.6)
125
126 - def DetectedFault(self):
127 ''' 128 Check if a fault was detected. 129 ''' 130 if self._triggerFaults: 131 return self._thread.FoundWindowEvent.isSet() 132 133 return False
134
135 - def OnShutdown(self):
136 ''' 137 Called when Agent is shutting down, typically at end 138 of a test run or when a Stop-Run occurs 139 ''' 140 try: 141 self._thread.StopEvent.set() 142 time.sleep(.6) 143 except: 144 pass
145 146 except: 147 pass 148 149 # end 150