Problem Statement
There can be situations when you need to execute your QTP scripts so that they can run when you are not present in front of your PC. But the problem is if the script execution takes more than 10 minutes or longer, the machine gets locked automatically due to security reasons. There are various options available for the system to not to get locked by making some changes in Windows Environment (registry,disable screen saver). This is again a security violation and also one should never leave the system unlocked.
In this situation, it has been observed that QTP does not work properly, however scripts can be started in locked mode but there is no guarantee that QTP will be able to interact GUI application. In most of the cases it is the Window message pop-up causes the issue. whenever QTP has to interact with such a pop-up it stuck at the place and this impacts in the script execution, in other cases it works fine.
Reason why QTP scripts does not work while system is locked
When the machine is locked, Windows disables windows messages, which is required to perform a window operation.There are some methods in QTP which uses these system messages to communicate and interact with the application. On a locked system, these QuickTest Professional methods are unable to replay. This is a limitation of the O/S that is preventing the QuickTest Professional script to replay in a locked mode. If we can send the messages that are required to perform a particular operation through QTP, then this problem can be sorted out and script execution can be done while the system is locked.
Solution for the above problem
To overcome the above problem we need to use Windows API in QTP.
Below code help us to work with the QTP objects while the screen is locked.
Function HandleDialog
'Include Win API
Extern.Declare micHwnd, "FindWindowEx", "user32.dll", "FindWindowExA", micHwnd, micHwnd, micString, micString
Extern.Declare micLong, "GetWindowText", "user32.dll", "GetWindowTextA", micHwnd, micString + micByRef, micLong
Extern.Declare micLong, "GetWindow", "user32.dll", "GetWindow", micLong, micLong
Extern.Declare micLong, "PostMessage", "user32.dll", "PostMessageA", micHwnd, micLong, micLong, micLong
Const GW_HWNDNEXT = 2 'Retrieve Text Constant
Const WM_COMMAND = &H111& 'Send command
pHwnd = extern.FindWindowEx(0,0, vbNullString, "Windows Internet Explorer") 'Get Window Handle
Hwnd = extern.FindWindowEx(pHwnd,0,"Static" ,vbNullString) 'Get Text Handle
'Find All the Text that are there in the window
Do until Hwnd = 0
staticText=Space(256)
Extern.GetWindowText Hwnd,staticText, 256
Result = Result + staticText
Hwnd = extern.GetWindow(Hwnd, GW_HWNDNEXT)
Loop
'Send click message to click on 'Yes' button
lRes = extern.PostMessage(pHwnd, WM_COMMAND, 6, 0)
Print lRes
HandleDialog = True
End Function
'Include Win API
Extern.Declare micHwnd, "FindWindowEx", "user32.dll", "FindWindowExA", micHwnd, micHwnd, micString, micString
Extern.Declare micLong, "GetWindowText", "user32.dll", "GetWindowTextA", micHwnd, micString + micByRef, micLong
Extern.Declare micLong, "GetWindow", "user32.dll", "GetWindow", micLong, micLong
Extern.Declare micLong, "PostMessage", "user32.dll", "PostMessageA", micHwnd, micLong, micLong, micLong
Const GW_HWNDNEXT = 2 'Retrieve Text Constant
Const WM_COMMAND = &H111& 'Send command
pHwnd = extern.FindWindowEx(0,0, vbNullString, "Windows Internet Explorer") 'Get Window Handle
Hwnd = extern.FindWindowEx(pHwnd,0,"Static" ,vbNullString) 'Get Text Handle
'Find All the Text that are there in the window
Do until Hwnd = 0
staticText=Space(256)
Extern.GetWindowText Hwnd,staticText, 256
Result = Result + staticText
Hwnd = extern.GetWindow(Hwnd, GW_HWNDNEXT)
Loop
'Send click message to click on 'Yes' button
lRes = extern.PostMessage(pHwnd, WM_COMMAND, 6, 0)
Print lRes
HandleDialog = True
End Function
Just Open QTP and Paste the above function and the below statement in a function library -
RegisterUserFunc "Dialog","Exist","HandleDialog"
Associate the function library to the Test and its ready.
The function can be broken into parts or extended to handle as per the requirement, like retriving the window text, click on a particular button etc











