How to set focus on file dialog opened in Vbscript
Our Team is automating tests/test data preparation in QTP and we do the scripting in VBScript.
In several tests the tester who runs the script need to supply an MS-Excel file with the indata. We use UserAccounts.CommonDialog
for this and it works great. Except for one litle problem, when we run this from QTP the file dialog does not get focus. It's opened in the background and it's easy for the tester that runs the script to miss it and waste time waiting for the dialog.
How do we give the file dialog focus?
Code Example:
Set ObjFSO = CreateObject("UserAccounts.CommonDialog")
ObjFSO.Filter = "MS-Excel Spreadsheets|*.xls|All Files|*.*"
while ObjFSO.ShowOpen = false
msgbox "Script Error: Please select a file!"
wend
msgbox "You selected the file: "开发者_运维百科 & ObjFSO.FileName
My guess is that since the dialog is modal, the ShowOpen
method doesn't return the execution control back to the script until the dialog is closed. So there's no way to interact with the dialog as part of your test script.
As a workaround, you could spawn a parallel script that would wait for the dialog and activate it. But I guess QTP cannot run two scripts in parallel, so you'll probably need an external shell script (written in VBScript / JScript / PowerShell / etc).
Edit: Try the following:
Create an external VBScript file (.vbs) with the following contents:
Set oShell = CreateObject("WScript.Shell") While Not oShell.AppActivate("Open") WScript.Sleep 500 Wend
This script calls
WshShell.AppActivate
to activate a window whose title containsOpen
(replace it with the actual dialog title). If there's no such widnow at the monent, it retries the attempt after 0.5 sec (you can increase the delay if you wish).Launch this script from your QTP test before opening the dialog. (Use
SystemUtil.Run
or something like this.)
I'm not sure, but I think this should do the trick.
Did you try recording a click on the dialog - so that QTP will click on it to set focus before proceeding?
精彩评论