WshShell 的对象学习

在学习WshShell的对象之前,先看下面的一个例子,该实例是一个很使用也很常用的例子,当然网上的也早有流传,就是在我们进行web测试之前关闭所有的IE浏览器,避免影响我们的测试,其实用到了WshShell,由于刚刚学习QTP不久,所有对该方法不是很明白,就拿出来分享一下

'**************************************************************************
'设计说明:关闭所有打开的IE
'调用举例:CloseBrowsers
'**************************************************************************
'调用举例
Call CloseAllBrowser

Public Sub CloseAllBrowser
Set Wshshell = CreateObject("Wscript.Shell")
Set DialogObject = Description.Create()
DialogObject("micclass").Value = "Dialog"
Set Windows32Dialog = Desktop.ChildObjects(DialogObject)
dlNum = Windows32Dialog.Count - 1
msgbox dlNum
For v = 0 to dlNum
Windows32Dialog(v).Close
Next
Set Windows32Dialog = Nothing
Set theBrowser = Browser("micclass:=Browser", "index:=0")
While theBrowser.Exist(0)
theBrowser.Close
'有些系统页面可能在关闭的时候会有提示对话框出现
waitNx = 1
Do While waitNx < 5
Set Windows32Dialog = Desktop.ChildObjects(DialogObject)
dlNum = Windows32Dialog.Count - 1
For v = 0 to dlNum
dlText = Windows32Dialog(v).GetROProperty("regexpwndtitle")
Wshshell.AppActivate(dlText)
Wait(1)
Wshshell.SendKeys "{ENTER}"
Next
Set Windows32Dialog = Nothing
waitNx = waitNx + 1
Loop
Reporter.ReportEvent micPass, "使用CloseAllOpenedBrowsers函数页面关闭成功","当前页面关闭成功!"
Wend
Set theBrowser = Nothing
Set DialogObject = Nothing
Set Wshshell = Nothing
End Sub


WshShell 对象
ProgID Wscript.Shell
文件名 WSHom.Ocx
1.Environment 属性 (环境变量属性)
SpecialFolders 使用 WshSpecialFolders 对象提供对 Windows shell 文件夹的访问,如桌面文件夹,开始菜单文件夹和个人文档文件夹。
例子:Set wshell = CreateObject("wscript.shell")
MsgBox wshell.specialfolders("desktop")'显示桌面的绝对路径
这样的特殊文件夹还有:
AllUsersDesktop
AllUsersStartMenu
AllUsersPrograms
AllUsersStartup
Desktop
Favorites
Fonts
MyDocuments
NetHood
PrintHood
Programs
Recent
SendTo
StartMenu
Startup
Templates
2.CreateShortcut方法(创建快捷图标)
例子1:Set WshShell = CreateObject("Wscript.Shell")
Set oShellLink = WshShell.CreateShortcut("Current Script.lnk")
oShellLink.TargetPath = Wscript.ScriptFullName
oShellLink.Save
Set oUrlLink = WshShell.CreateShortcut("我的博客.URL")
oUrlLink.TargetPath = "http://hi.baidu.com/wuxiping101/blog"
oUrlLink.Save '此快捷图标保存在了桌面上
例子2:set wshell=createobject("wscript.shell")
desk=wshell.specialfolders("desktop")
msgbox desk & wscript.scriptfullname
set Calc=wshell.CreateShortCut(desk&"\CalC.lnk")
Calc.WindowStyle=1
Calc.TargetPath="CalC.exe"
Calc.IconLocation="D:\Program Files\Tencent\Bin\QQ.exe,0"
Calc.Description="这是windows系统自带的计算器"
Calc.WorkingDirectory=desk
Calc.HotKey="CTRL+SHIFT+P"
Calc.Save

例子3:

WshShell.SendKeys "{ENTER}"     '模拟键盘进行操作
WshShell.AppActivate "Calculator" '启动应用程序

你可能感兴趣的:(shell)