Public colWindows ''****************************************************************** ' Name: Class clsWindow ' ' Purpose: ' ' Functions: ' GetCustomWindowHwnd ' GetRecentlyOpenWindowHwnd ' Name ' ' Properties: ' colObject ' oDict ' ' Methods: ' AddNew ' LaunchAdd ' AddCustom ' Class_Initialize ' Class_Terminate ' Destroy ' ' Author: Anshoo Arora (RelevantCodes.Com) ' ' Date: November 09, 2009 ' ' Version: 0.1 ''****************************************************************** Class clsWindow Private col_Object Private o_Dict Public sFilePath 'Contains the Path to the .Exe file to the AUT Public sName 'The given name of the AUT Public sPropertyValue 'Property:=Value of the Application added through AddCustom Public arrPropertyValue 'Array("Property:=Value") of the Application added through AddCustom '****************************************************************** ' Name: Sub AddNew ' ' Purpose: Automatically adds a new open window to the collection object. ' This method should only be used if the application is launched using ' the LaunchAdd method. If LaunchAdd method is not used initially, this ' method may add an invalid window to the collection. ' ' Input: ' sName - Given name of the Window ' iTimeOutBeforeWindowOpens - Wait this many number of seconds for the ' application to load. Default = 1. ' ' Output: ' None ' ' Dependencies: This procedure will only work successfully once after LaunchAdd is used to add ' the main AUT window. ' ' ' Author: Anshoo Arora (RelevantCodes.Com) ' ' Date: November 09, 2009 ''****************************************************************** Public Sub AddNew(sName, iTimeOutBeforeWindowOpens) Dim oDesc, oParent, x, iTimeElapsed, lngHwnd 'Set default application timeout If iTimeOutBeforeWindowOpens = "" Then iTimeOutBeforeWindowOpens = 1 End If 'Create a Description Object Set oDesc = Description.Create 'oParent holds all open windows Set oParent = Desktop.ChildObjects(oDesc) 'Wait iTimeOutBeforeWindowOpens number of seconds 'Default iTimeOutBeforeWindow Opens = 1 Wait(iTimeOutBeforeWindowOpens) 'Loop for 5 seconds 'If within 5 seconds, a new unadded window is found, the window will ' be added to the collection and this procedure will end. Do 'Create a new collection Set oParent = Desktop.ChildObjects(oDesc) 'Loop for all objects in the collection and find a one that does 'not exist in the global dictionary: colWindows For x = 0 to oParent.Count - 1 If Not oDict.Exists(oParent(x).GetROProperty("hwnd")) Then lngHwnd = oParent(x).GetROProperty("hwnd") oDict.Add lngHwnd, lngHwnd colObject.Add sName, Window("hwnd:=" & lngHwnd) Exit Do End If Next Wait(1) iTimeElapsed = iTimeElapsed + 1 Loop Until iTimeElapsed = 5 'This timeout can be minized to 1, if desired End Sub '****************************************************************** ' Name: Sub LaunchAdd ' ' Purpose: Launches the AUT and automatically adds its description to ' the collection object. ' ' Input: ' sFilePath - Location of the AUT ' sName - Given name of the window ' ' Output: ' None ' ' Author: Anshoo Arora (RelevantCodes.Com) ' ' Date: November 09, 2009 ''****************************************************************** Public Sub LaunchAdd(sFilePath, sName) Dim lngHwnd Me.sFilePath = sFilePath Me.sName = sName 'Retrieve the window handle of the recently launched window lngHwnd = GetRecentlyOpenWindowHwnd() 'If the handle is valid, then add it to the global collection object If lngHwnd <> -1 Then colObject.Add sName, Window("hwnd:=" & lngHwnd) If Not oDict.Exists(lngHwnd) Then oDict.Add lngHwnd, lngHwnd End If End If End Sub '****************************************************************** ' Name: Sub AddCustom ' ' Purpose: Used to add a custom window to the collection object by ' specifying its property. ' ' Input: ' sPropertyValue - Property:=Value ' sName - Given name of the window ' ' Output: ' None ' ' Author: Anshoo Arora (RelevantCodes.Com) ' ' Date: November 09, 2009 ''****************************************************************** Public Sub AddCustom(arrPropertyValue, sName) Dim lngHwnd Me.sName = sName Me.arrPropertyValue = arrPropertyValue 'Retrieve the handle of the specified window lngHwnd = GetCustomWindowHwnd() 'If the handle is valid, then add it to the global collection If lngHwnd <> -1 Then colObject.Add sName, Window("hwnd:=" & lngHwnd) If Not oDict.Exists(lngHwnd) Then oDict.Add lngHwnd, lngHwnd End If End If End Sub '****************************************************************** ' Name: Function GetCustomWindowHwnd() ' ' Purpose: Retrieves the window handle. ' ' Input: ' None ' ' Output: ' Integer ' ' Author: Anshoo Arora (RelevantCodes.Com) ' ' Date: November 09, 2009 ''****************************************************************** Private Function GetCustomWindowHwnd() 'As Integer Dim sName, arrPropertyValue, x, sDesc, hWnd, bExist 'Default handle = -1 GetCustomWindow = -1 arrPropertyValue = Me.arrPropertyValue sName = Me.sName If UBound(arrPropertyValue) >= 0 Then Set oDesc = Description.Create For x = 0 to UBound(arrPropertyValue) If x = UBound(arrPropertyValue) Then sDesc = "" & sDesc & """" & arrPropertyValue(x) & """" Else sDesc = """" & arrPropertyValue(x) & """," & sDesc & "" End If Next End If On Error Resume Next Execute "bExist = Window(" & sDesc & ").Exist(5)" If bExist Then Execute "GetCustomWindowHwnd = Window(" & sDesc & ").GetROProperty(""hwnd"")" End If On Error Goto 0 End Function '****************************************************************** ' Name: Function GetRecentlyOpenWindowHwnd() ' ' Purpose: Retrieves the handle of the newly launch window. This method ' also adds handles of all the windows open on the desktop so the ' AddNew method can work as expected. ' ' Input: ' None ' ' Output: ' Integer ' ' Author: Anshoo Arora (RelevantCodes.Com) ' ' Date: November 09, 2009 ''****************************************************************** Private Function GetRecentlyOpenWindowHwnd() 'As Integer Dim sFilePath, sName, oDesc, oParent, mHwndDict, x, iTimeElapsed sFilePath = Me.sFilePath sName = Me.sName GetRecentlyOpenWindowHwnd = -1 On Error Resume Next 'Create a description object Set oDesc = Description.Create 'oParent holds references to all the open windows Set oParent = Desktop.ChildObjects(oDesc) 'Scripting.Dictionary: holds all open windows' handles Set mHwndDict = CreateObject("Scripting.Dictionary") 'Loop until all the handles are successfully added to the collection For x = 0 to oParent.Count - 1 mHwndDict.Add oParent(x).GetROProperty("hwnd"), x If Not oDict.Exists(oParent(x).GetROProperty("hwnd")) Then oDict.Add oParent(x).GetROProperty("hwnd"), oParent(x).GetROProperty("hwnd") End If Next 'Launch the target application SystemUtil.Run sFilePath 'Loop max 10 seconds for the window to open Do Set oParent = Desktop.ChildObjects(oDesc) For x = 0 to oParent.Count - 1 Select Case oParent(x).GetTOProperty("micclass") Case "Window", "Dialog" If Not mHwndDict.Exists(oParent(x).GetROProperty("hwnd")) Then GetRecentlyOpenWindowHwnd = oParent(x).GetROProperty("hwnd") Exit Do End If End Select Next Wait(1) iTimeElapsed = iTimeElapsed + 1 Loop Until iTimeElapsed = 10 'This timeout can be increased of decreased depending ' upon the time it takes for your application to open. ' If your application takes approximately, 2-3 seconds, ' then you can leave it at 10 seconds. If it takes more ' than 10 seconds, then you can increase this to 20 and so on.. Set oDesc = Nothing Set oParent = Nothing Set mHwndDict = Nothing On Error Goto 0 End Function '****************************************************************** ' Name: Function Name ' ' Purpose: ' ' Input: ' Key ' ' Output: ' Object ' ' Author: Yaron Assa ' ' Date: November 09, 2009 ''****************************************************************** Public Function Name(Key) 'As Object Dim Keys Keys = colObject.Keys If IsNumeric(Key) Then Key = Keys(Key) End If If IsObject(colObject.Item(Key)) Then Set Name = colObject.Item(Key) Else Name = colObject.Item(Key) End If End Function '****************************************************************** ' Name: Sub Destroy ' ' Purpose: Releases objects. ' ' Input: ' None ' ' Output: ' None ' ' Author: Anshoo Arora (RelevantCodes.Com) ' ' Date: November 09, 2009 ''****************************************************************** Public Sub Destroy colObject = Nothing oDict = Nothing End Sub '****************************************************************** ' Name: Sub Class_Initialize ' ' Purpose: Singleton for colWindows and oDict ' ' Input: ' None ' ' Output: ' None ' ' Author: Anshoo Arora (RelevantCodes.Com) ' ' Date: November 09, 2009 ''****************************************************************** Private Sub Class_Initialize Dim bInit: bInit = False If IsObject(colWindows) Then If Not colWindows Is Nothing Then bInit = True End If End If If bInit = False Then Set colWindows = CreateObject("Scripting.Dictionary") oDict = CreateObject("Scripting.Dictionary") End If colObject = colWindows End Sub '****************************************************************** ' Name: Let oDict ' ' Purpose: ' ' Input: ' Val ' ' Output: ' None ' ' Author: Anshoo Arora (RelevantCodes.Com) ' ' Date: November 09, 2009 ''****************************************************************** Public Property Let oDict(ByRef Val) 'As Collection Set o_Dict = Val End Property '****************************************************************** ' Name: Get oDict ' ' Purpose: ' ' Input: ' None ' ' Output: ' Object ' ' Author: Anshoo Arora (RelevantCodes.Com) ' ' Date: November 09, 2009 ''****************************************************************** Public Property Get oDict() 'As Collection Set oDict = o_Dict End Property '****************************************************************** ' Name: Let colObject ' ' Purpose: ' ' Input: ' Val ' ' Output: ' None ' ' Author: Anshoo Arora (RelevantCodes.Com) ' ' Date: November 09, 2009 ''****************************************************************** Public Property Let colObject(ByRef Val) 'As Collection Set col_Object = Val End Property '****************************************************************** ' Name: Get colObject ' ' Purpose: ' ' Input: ' None ' ' Output: ' Object ' ' Author: Anshoo Arora (RelevantCodes.Com) ' ' Date: November 09, 2009 ''****************************************************************** Public Property Get colObject() 'As Collection Set colObject = col_Object End Property End Class Set WindowObject = New clsWindow ''******************************************************************