views:

55

answers:

2

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
+2  A: 

Did you try recording a click on the dialog - so that QTP will click on it to set focus before proceeding?

Tom E
@Tom Hi. Good suggestion. I tried it but to no avail. What Helen wrote in another answer seems to be correct. The dialog is modal. So once I open it QTP just waits for the reply and I can not interact with the file dialog in any way.
Jonas Söderström
+2  A: 

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 contains Open (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.

Helen
@Helen Hi. Seems like you are correct, I can't interact with it any way. But it seems like there should be some option I can send to it, telling windows "give me focus!".
Jonas Söderström
@Jonas: I've updated the answer.
Helen