windows 如何将焦点设置在 Vbscript 中打开的文件对话框上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3708425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:14:43  来源:igfitidea点击:

How to set focus on file dialog opened in Vbscript

windowsvbscriptqtp

提问by Jonas S?derstr?m

Our Team is automating tests/test data preparation in QTP and we do the scripting in VBScript.

我们的团队在 QTP 中自动化测试/测试数据准备,我们在 VBScript 中编写脚本。

In several tests the tester who runs the script need to supply an MS-Excel file with the indata. We use UserAccounts.CommonDialogfor 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.

在几个测试中,运行脚本的测试人员需要提供一个包含 indata 的 MS-Excel 文件。我们UserAccounts.CommonDialog为此使用它并且效果很好。除了一个小问题,当我们从 QTP 运行它时,文件对话框没有获得焦点。它在后台打开,运行脚本的测试人员很容易错过它并浪费时间等待对话框。

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

回答by Tom E

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

您是否尝试记录对对话框的单击 - 以便 QTP 在继续之前单击它以设置焦点?

回答by Helen

My guess is that since the dialog is modal, the ShowOpenmethod 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.

我的猜测是,由于对话框是模态的,该ShowOpen方法在对话框关闭之前不会将执行控制返回给脚本。因此,无法将对话框作为测试脚本的一部分进行交互。

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).

作为一种解决方法,您可以生成一个等待对话框并激活它的并行脚本。但我猜 QTP 不能并行运行两个脚本,所以你可能需要一个外部 shell 脚本(用 VBScript / JScript / PowerShell / 等编写)。



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.AppActivateto 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.Runor something like this.)

  • 使用以下内容创建外部 VBScript 文件 (.vbs):

    Set oShell = CreateObject("WScript.Shell")
    While Not oShell.AppActivate("Open")
      WScript.Sleep 500
    Wend
    

    此脚本调用WshShell.AppActivate以激活标题包含的窗口Open(将其替换为实际的对话框标题)。如果 monent 没有这样的寡妇,它会在 0.5 秒后重试(如果您愿意,可以增加延迟)。

  • 在打开对话框之前从 QTP 测试中启动此脚本。(使用SystemUtil.Run或类似的东西。)

I'm not sure, but I think this should do the trick.

我不确定,但我认为这应该可以解决问题。