As usual, after I got into writing this, it got too long to be a single post. Part 1 covers the background for why I began to use AutoIt in QTP and how you can go about setting up a system to use it within QTP without installing all the tools that come with a full version of the AutoIt software. Part 2 will follow shortly and will cover some sample code that demonstrates how I used AutoIt in my QTP script to solve my problem and will cover some code that allows you to automate downloading and registering the AutoItX3.dll from a Quality Center project. This allows you to be confident that any computer that runs any script requiring the AutoItX3.dll will automatically have it set up for them.
First the background, I recently had a requirement that I wanted to automate that was apparently something that is very difficult to do with automation. The requirement was that the inputs for a web application would be able to accept special characters like ¢ or ©. These special characters are available via Windows Character Map, or there are also ALT+<Number> keyboard shortcuts for the most common ones. For example, © is generated by ALT+0169. The rub here is that the shortcuts only work with the number pad keys (the block of keys to the right of a standard keyboard). They do not work with the number keys above the letters. Therefore, you cannot use the shortcuts on most laptop keyboards and as I found out, most keystroke automations do not specifically support sending the number pad keys.
I figured it couldn't be THAT hard to automate the entering of a special character via its shortcut. I figured there must be a way using WshShell.SendKeys or the .NET System.Windows.Forms.SendKeys object. Boy was I wrong. I tried several methods using the above approaches, none of which worked since, as I already pointed out, those methods do not have separate commands for simulating the number pad. Having given up on finding the answer without help, I posed the question asking how to do it on my favorite QTP forum/site, AdvancedQTP.
One of my favorite people, Tarun Lalwani, gave me a solution privately and asked that I not share it since he was planning on writing an article about it on his site (which I think may have taken over as my favorite QTP forum/site). He hasn't posted an article about it yet so I can't link to it, sorry. I know that he has been busy developing yet another awesome contribution to the community called PowerDebug, so I'm pretty sure the article in question is not a priority for him at this point.
His solution was quite complex and he had to go deeper into the Windows API than I probably would have for the answer. It employs the usage of the Extern object and the user32.dll. Of course, I probably could have just automated the character map application to enter special characters, but that seemed like a ham-handed approach to me. Or, I suppose I could have just set the text with some text generated ahead of time, but I was convinced there must be a way to automate the generation of the characters.
Anyway, I digress. Another solution I researched was an automation scripting tool called AutoIt. I found out that I could easily simulate the keystrokes I needed with it, but I always hesitate to use anything other than pure QTP since I don't know who is going to run my tests in the future and I can only count on their configuration including QTP. Of course, the main reason I didn't want to use it was that I didn't want to have to figure out a new scripting language and research or devise a method of calling it from QTP (laziness).
However, I found out that they have a COM automatable version that could be registered with COM and then created as an object and used directly in QTP scripts (or any VBScript for that matter), no fuss, no muss. Here are the steps to get the AutoItX3 COM object working on your system without installing any of the other software associated with a full install of the AutoIt environment (this is on a WinXP system, I haven't done it on Vista or Win7 yet):
- Go to http://www.autoitscript.com/autoit3/downloads.shtml and click the "Download ZIP" button. (Or just click this link.)
- Extract the contents of the zip file (this is a self-extracting zip, just double click it).
- Open the "install" folder, then open the "AutoItX" folder.
- Copy the AutoItX3.dll file and paste the copy of it in the C:\WINDOWS\system32 folder.
- Copy the AutoItX.chm file and paste the copy somewhere handy for future reference. This is the compiled help file that contains the documentation for all the available methods.
- Open a command prompt and type "regsvr32 AutoItX3.dll".
- You should get confirmation that the registration succeeded.
- You may now create an instance of the AutoItX3.Control object and use it in QTP or VBScript to your heart's content!
- Refer to the help file in step 5 for all the details about what you can do with it. AutoIt is a very, very powerful automation tool!
Stick around for part 2, with code!