First, let’s take a look at how I solved my problem of automating keystrokes from the number pad. This will be genericized so that you can run it inside QTP or even just save it in a .vbs file and execute it. It launches Notepad, waits for the notepad window to exist, makes sure it is activated, and then sends the keystrokes to generate the copyright symbol ©.
Set oAutoIt = CreateObject("AutoItX3.Control") oAutoIt.Run("notepad.exe") oAutoIt.WinWait "Untitled - Notepad" oAutoIt.WinActivate "Untitled - Notepad" oAutoIt.Send "{ALTDOWN}{NUMPAD0}{NUMPAD1}{NUMPAD6}{NUMPAD9}{ALTUP}" Set oAutoIt = Nothing
Through messing around with AutoIt while trying to solve the problem of simulating typing on the number pad, I found that it is a great compliment to QTP’s functionality. The mouse simulations are great, the window management methods are wonderful (wait for a particular window’s existence, etc.), the registry management methods are outstanding, it has methods for setting and retrieving data from the system’s clipboard, and the list goes on and on! So, I really wanted to use AutoIt for more than just that one test case. That just presents the problem of knowing whether or not the system running the tests in the future would have the AutoItX3 COM object available.
To work around that fact, I figured out I could uploaded the AutoItX3.dll file to the Quality Center project’s “Subject” folder and create a quick script that can be run at the beginning of a test set in a stand-alone script or as part of any test script that requires AutoIt’s functionality. The code downloads and sets up the AutoIt COM object automatically on the machine running the tests. This script could most likely be refined, but I just haven’t really had the time or need to at this point. (Click here to view full-screen)
Dim regClr Set regClr = DotNetFactory.CreateInstance("Microsoft.Win32.Registry", "mscorlib").ClassesRoot If IsEmpty(regClr.OpenSubKey("AutoItX3.Control")) Then Dim sbjNode, attachmentFilter, attachmentList, attachment Dim fso, autoItX3File 'This is the name and destination path for the AutoItX3.dll file Dim autoItX3DllName, autoItX3DllPath autoItX3DllName = "AutoItX3.dll" autoItX3DllPath = "C:WINDOWSsystem32" & autoItX3DllName 'Isolate the file in QC and download it Set sbjNode = QCUtil.TDConnection.TreeManager.TreeRoot("Subject") Set attachmentFilter = sbjNode.Attachments.Filter attachmentFilter.Filter("CR_REFERENCE") = "'ALL_LISTS_" & sbjNode.NodeID & "_" & autoItX3DllName & "'" Set attachmentList = attachmentFilter.NewList Set attachment = attachmentList.Item(1) attachment.Load True, "" 'Move it to the destination path Set fso = CreateObject("Scripting.FileSystemObject") Set autoItX3Dll = fso.GetFile(attachment.FileName) autoItX3Dll.Copy(autoItX3DllPath) 'Register it Set WshShell = CreateObject("WScript.Shell") WshShell.Exec "RegSvr32 /s " & autoItX3DllPath 'Release objects Set sbjNode = Nothing Set attachmentFilter = Nothing Set attachmentList = Nothing Set attachment = Nothing Set autoItX3Dll = Nothing Set fso = Nothing Set WshShell = Nothing End If Set regClr = Nothing
Of course, this code could be used to load any COM object. This could be really handy if you develop your own custom dll’s for your tests. Other enhancements could be to make paths more dynamic, but I’m confident that this would work on any computer in my current client’s organization.