In my previous post, I talk about How to use windows API send click message to an “ok” button of IE alert dialog. The code used c#. I’d like to transmit them to QTP. It’ll more stable and efficient than QTP built-in Function.
In QTP, if we want to do some action on a dialog, we should add a dialog object to repository.QTP can use their properties to find the dialog then execute command. But the problem is that some dialog’s properties are dynamic. Such as the title of Download Dialog.It lead to QTP couldn’t find exactly object. That’s why our automation script sometimes is slow and broken.
To avoid these tricky, QTP provide us external object. It can be used to call external function. Windows API is good experience. We can use Windows API in QTP. It’s terrific, right? We can using API do everything efficiently.
Following is the script segment to click “OK” button of IE pop dialog.
extern.Declare micHwnd,"FindWindowEx", "user32.dll","FindWindowEx",micHwnd,micHwnd,micString,micString
extern.Declare micLong, "PostMessage", "user32.dll", "PostMessageA", micHwnd, micLong, micLong, micLong
extern.Declare micHwnd,"SetActiveWindow","user32.dll","SetActiveWindow",micHwnd
extern.Declare micInteger,"SetForegroundWindow","user32.dll","SetForegroundWindow",micHwnd
hwndP = extern.FindWindowEx (0,0,"#32770", null)
hwndButton = extern.FindWindowEx (hwndP,0,"Button","OK")
extern.SetForegroundWindow hwndP
extern.SetActiveWindow hwndP
extern.PostMessage hwndButton,&H0F5,0,0
Enjoy QTP; enjoy your auto-life!