首页 > 其他 > 详细

VBScript/QTP 的常用COM对象列表

时间:2014-05-15 14:54:10      阅读:462      评论:0      收藏:0      [点我收藏+]

众所周知,我们经常在脚本中创建一些对象来实现某些特定的功能。尤其是当我们使用QTP的描述性编程时,需要创建这些对象。

下边是我们经常在QTP或VBScript中用到的对象列表:

Set objEmail = CreateObject("CDO.Message" )

Set objIE = CreateObject("InternetExplorer.Application" )

Set objInet = CreateObject("InetCtls.Inet.1" )

Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1" )

Set objExcel =CreateObject("Excel.Application" )

Set objOutlook = CreateObject("Outlook.Application" )

Set objPpt = CreateObject( "PowerPoint.Application")

Set objWord = CreateObject("Word.Application" )

Set objCal = CreateObject("MSCAL.Calendar" )

Set objQPro = CreateObject("QuattroPro.PerfectScript")

Set objWP = CreateObject("WordPerfect.PerfectScript”")

Set objConn = CreateObject("ADODB.Connection" )

Set objRecSet = CreateObject("ADODB.Recordset" )

Set objDic = CreateObject("Scripting.Dictionary")

Set objFSO = CreateObject("Scripting.FileSystemObject" )

Set wshNetwork = CreateObject("WScript.Network" )

Set wshShell = CreateObject("WScript.Shell")

Set objRandom = CreateObject("System.Random" )

Set objArrList = CreateObject("System.Collections.ArrayList")

Set objSortList = CreateObject("System.Collections.SortedList" )

Set xmlDoc = CreateObject( "Microsoft.XmlDom")

Set xml2Doc = CreateObject("Msxml2.DOMDocument.5.0")

Set objiTunes = CreateObject("iTunes.Application")

Set objPlayer = CreateObject("WMPlayer.OCX" )

Set objWMPlayer = CreateObject("WMPlayer.OCX.7" )

Set objReal = CreateObject( "rmocx.RealPlayerG2 Control.1")

Set objFSDialog = CreateObject("SAFRCFileDlg.FileSave")

Set objFODialog = CreateObject("SAFRCFileDlg.FileOpen" )

Set objDialog = CreateObject("UserAccounts.CommonDialog")

Set SOAPClient = CreateObject("MSSOAP.SOAPClient")

Set objWOL = CreateObject("UltraWOL.ctlUltraWOL")

Set objSearcher = CreateObject("Microsoft.Update.Searcher")

Set objShell = CreateObject("Shell.Application")

SetobjDeviceReplay=CreateObject("Mercury.DeviceReplay")

以下是示例演示:

描述:创建和返回一个自动化对象的引用

语法: CreateObject(class)

class 参数使用了语法servername.typename

servername: 提供对象的应用程序的名称

typename: 对象的类型或类

说明:每个自动化对象至少提供一种对象类型。例如,一个文字处理程序可能会提供一个应用程序对象,一个文件对象和一个工具栏对象。要创建一个自动化对象,需要把CreateObject创建的对象变量赋给一个对象变量。

示例1:创建一个Excel应用程序对象 (“Excel.Application” )

‘关闭桌面上打开的所有Excel表格

Systemutil.CloseProcessByName"excel.exe"

 

‘创建一个新的Excel对象

Set Excel =CreateObject("Excel.Application")

 

‘打开Excel表格

Set SExcelSheet =Excel.Workbooks.Open("C:\Users\djiao\Documents\ella\CR_Report.xlsx")

 

‘设置表格可见

SExcelSheet.Application.visible=true

 

‘关闭任何Excel的错误窗口或提示信息

Excel.DisplayAlerts = False

 

‘重命名并保存Excel到不同的路径下

SExcelSheet.SaveAs"C:\BaseLine.xlsx"

 

‘关闭Excel的表格

SExcelSheet.Close

 

‘退出Excel应用程序

Excel.Quit

示例3A: 创建文件系统对象 (“Scripting.FileSystemObject”)

‘将要创建的文件夹的存放路径

strDrive = "D:\DATA"

 

‘要创建的文件夹名称

strfoldername= "New Folder"

 

‘获取文件夹的绝对路径

strPath=strDrive&"\"&strfoldername

 

‘创建 FileSystemObject 对象

Set objFSO =CreateObject("Scripting.FileSystemObject")

 

‘文件夹不存在时,创建文件夹

If NOT objFSO.FolderExists(strPath) Then

  objFSO.CreateFolder strPath

End If

 

‘释放文件系统对象

Set objFSO = nothing

示例3B:

‘Get the name of file extention

msgbox GetAnExtension("C:\ProgramFiles (x86)\HP\QuickTest Professional\bin\QTPro.exe")

 

Function GetAnExtension(DriveSpec)

         Dimfso

         Setfso = CreateObject("Scripting.FileSystemObject")

         GetAnExtension= fso.GetExtensionName(Drivespec)

         Setfso = nothing

End Function

示例4: 邮件发送对象(“CDO.Message”)

Dim objMessage

 

‘创建邮件发送对象

Set objMessage =CreateObject("CDO.Message")

 

‘给邮件添加主题

objMessage.Subject = "QTP Results –Automated Testing"

objMessage.From ="QTPTesting@address.com" ‘ 改为你自己的邮箱地址

objMessage.To = "your@email.com"‘收件人邮箱地址

objMessage.CC = "your@email.com"‘CC to email id

 

‘邮件内容

objMessage.TextBody ="N.B. – Please DoNot Reply This Message Directly."

 

‘添加附件

objMessage.AddAttachment"D:\Data\file.text"

 

‘这部分提供了远程SMTP server的配置信息

objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")= 2

‘设置SMTP server的name或IP

objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")= "smtp.163.com"

‘服务器端口号(通常是 25)

objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport")= "someserver.domain.com"

objMessage.Configuration.Fields.Update

 

‘发送邮件

objMessage.Send

 

‘释放邮件对象

Set objMessage = nothing

示例5: 创建WshShell对象 (“Wscript.Shell”)

说明:WScript.Shell是WshShell对象的ProgID,创建WshShell对象可以运行程序、操作注册表、创建快捷方式、访问系统文件夹、管理环境变量。

Set WshShell =CreateObject("Wscript.Shell")

Dim Response

‘显示一个带有yes和no的消息框

Response = MsgBox("Please Select yourchoice as ‘Yes’ or ‘No’.", vbYesNo)

 

If Response = vbYes Then

         ‘消息框会显示3秒钟

         WshShell.Popup"You Have Been Selected “Yes”. Please wait.", 3, "YourSelection"

Else

        

         ‘消息框会显示5秒

         WshShell.Popup"You Have Been Selected “No”", 5, "Your Selection"

End If

 

Set WshShell = nothing

示例6A: Mercury.DeviceReplay对象  (“Mercury.DeviceReplay”)

说明:这个对象用来模拟鼠标的单击和移动、键盘输入等。使用该对象前,需要保证键盘状态正确,如NUMLOCK是否打开等,DeviceReplay不能检测键盘状态。

‘定义坐标

Dim abs_x, abs_y

abs_x = 200

abs_y = 200

 

‘创建Mercury.DeviceReplay对象

Set objMercuryMouse = CreateObject("Mercury.DeviceReplay")

 

‘移动鼠标到指定坐标

objMercuryMouse.MouseMove abs_x,abs_y

 

‘鼠标左击

objMercuryMouse.MouseClick abs_x,abs_y,LEFT_MOUSE_BUTTON

 

Wait 3

 

‘释放Mercury.DeviceReplay对象

Set objMercuryMouse = nothing

示例7:字典对象(“Scripting.Dictionary”)

‘创建dictionary对象

Dim d

Set d =CreateObject("Scripting.Dictionary")

 

‘给dictionary添加key和value

d.Add "a","Athens"  

d.Add "b", "Belgrade"

d.Add "c", "Cairo"

 

‘获取所有的item

a = d.Items

For i = 0 To d.Count -1 ‘ Iterate thearray.

  s =s & a(i) & VBCrLF ‘ Create return string.

Next

print s

 

‘根据key获取item

print d.Item("a")

 

‘释放dictionary对象

Set d = nothing

Example 8A: Connection对象,数据集对象("ADODB.Connection")/("ADODB.Recordset")

Function database()

         v_DBInstance= "RENPCRT8"

         v_MHXMLDBPwd= "grudge"

         v_MHXMLDBSchema= "Mhxmledit"

 

         ‘创建数据库连接对象和数据集对象

         ConstadOpenStatic = 3

         ConstadLockOptimistic = 3

         ConstadUseClient = 3

         SetobjConnection = CreateObject("ADODB.Connection")

         SetobjRecordset = CreateObject("ADODB.Recordset")

         objConnection.Open"DRIVER={Microsoft ODBC for Oracle};UID="& v_MHXMLDBSchema&" ;PWD="& v_MHXMLDBPwd & ";SERVER="&v_DBInstance &”;"

         objRecordset.CursorLocation= adUseClient

         objRecordset.CursorType= adopenstatic

         objRecordset.LockType= adlockoptimistic

         objRecordset.Source= "select SOP from MHXML.FIRMS_STG where org_id in 681915"

         ObjRecordset.ActiveConnection= ObjConnection

 

         ‘执行查询语句

         ObjRecordset.Open

         IfObjRecordset.recordcount>0 then

                   Field1= ObjRecordset("SOP").Value

                   msgboxField1

         Endif

End Function

Example 8B: (“Database connection withoutADODB.Connection Object”)

此方法可以从数据库中获取值为空或者null的数据

Function GetAttorneyInfo(InField,ALid)

         v_DBInstance="RENPCRT8"

         v_MHXMLDBPwd="creek"

         v_MHXMLDBSchema="lbmgradmin"

         GetAttorneyInfo=""

 

         ‘创建数据库连接

         MHXMLconnection_string="DRIVER={MicrosoftODBC for Oracle};UID="& v_MHXMLDBSchema &" ;PWD="&v_MHXMLDBPwd &";SERVER=" & v_DBInstance &";"

         isMHXMLConnected= db_connect ( MHXMLConnection ,MHXMLconnection_string )

 

         IfisMHXMLConnected=0Then ‘从表中获取数据

 

                   v_Exe_SQL2="Selectlength(NVL(" & InField & ",‘Data Not Found‘)) fromlbmgradmin.ilv_vw where ilisting_id = " & ALid

                   setRecSet_SOPInfo_LEN = db_execute_query( MHXMLConnection , v_Exe_SQL2 )

                   d_SOPInfo_Length= db_get_field_value( RecSet_SOPInfo_LEN , 0 , 0 )

                   v_Exe_SQL2="selectsubstr(to_char(NVL(" & InField & ",‘Data NotFound‘)),1," & d_SOPInfo_Length & ") from lbmgradmin.ilv_vwwhere ilisting_id = " & ALid

                   setRecSet_SOPInfo = db_execute_query( MHXMLConnection , v_Exe_SQL2 )

                   RowCnt= db_get_rows_count( RecSet_SOPInfo )

 

                   IfRowCnt=1Then

                            d_SOPInfo=db_get_field_value(RecSet_SOPInfo , 0 , 0 )

                   else

                            datatable.SetCurrentRow(1)

                            d_SOPInfo=db_get_field_value(RecSet_SOPInfo , 0 , 0 )

                   EndIf

         else

         EndIf

         GetAttorneyInfo=d_SOPInfo

End Function

 

‘打开数据库连接

Function db_connect( byRef curSession,connection_string)

         Dimconnection

         onerror Resume next

 

         ‘创建数据库连接对象

         setconnection = CreateObject("ADODB.Connection")

         IfErr.Number <> 0 then

                   db_connect="Error # " & CStr(Err.Number) &" " &Err.Description

                   err.clear

                   ExitFunction

         EndIf

 

         connection.Openconnection_string

         IfErr.Number <> 0 then

                   db_connect="Error # " & CStr(Err.Number) & " " &Err.Description

                   err.clear

                   ExitFunction

         EndIf

         setcurSession=connection

         db_connect=0

End Function

 

‘关闭数据库连接

Function db_disconnect( byRef curSession )

         curSession.close

         setcurSession = Nothing

End Function

 

‘执行查询语句

Function db_execute_query ( byRefcurSession , SQL)

         setrs = curSession.Execute( SQL )

         setdb_execute_query = rs

End Function

 

‘获取数据集的行数

Function db_get_rows_count( byRef curRS )

         dimrows

         rows= 0

         curRS.MoveFirst

         DoUntil curRS.EOF

         rows= rows+1

         curRS.MoveNext

         Loop

         db_get_rows_count= rows

End Function

Example 8C: (“ADODB.Connection”)/(“ADODB.Recordset”)

此示例从Excel表单中获取数据

‘创建数据库连接对象

Dim Get_Field

set connectToDB =CreateObject("ADODB.Connection")

connectToDB.Provider ="Microsoft.Jet.OLEDB.4.0"

connectToDB.Properties("ExtendedProperties").Value = "Excel 8.0"

connectToDB.Open "D:\Documents andSettings\pauldx\Desktop\Data.xls"

strQuery="Select Age from [Data$]WHERE Name =‘Joli‘"

 

‘创建数据集对象

Set rsRecord =CreateObject("ADODB.Recordset")

rsRecord.Open strQuery,connectToDB,1,1

 

If rsRecord.RecordCount>0 Then

         fori= 1 to rsRecord.RecordCount

                   Get_Field=rsRecord.Fields(0)

                   printGet_Field

                   rsRecord.movenext

         next

Else

         Get_Field="FieldNot Present"

End If

Example 9:("AcroExch.App"/"AcroExch.AVDoc")

此段代码可以从pdf 文件中搜索Software单词

备注:这段代码只有当你安装了acrobat 专业版安装的时候才可以使用。如果只安装了adobe 阅读器的标准版,你会看到如下报错“ActiveX component can’t create object: ‘AcroExch.PDDoc””

 

Option Explicit

Dim accapp, acavdocu

Dim pdf_path, bReset, Wrd_count

pdf_path="C:\test.pdf"

 

‘创建AcroExch i应用程序对象

Setaccapp=CreateObject("AcroExch.App")

accapp.Show()

‘创建AVDoc对象

Setacavdocu=CreateObject("AcroExch.AVDoc")

‘打开PDF文件

If acavdocu.Open(pdf_path,"")Then

acavdocu.BringToFront()

bReset=1 : Wrd_count = 0

 

‘查找,定位并高亮指定文本

Do Whileacavdocu.FindText("software", 1, 1, bReset)

bReset=0: Wrd_count=Wrd_count+1

Wait 0, 200

Loop

End If

accapp.CloseAllDocs()

accapp.Exit()

msgbox "The word ‘software‘ was found" & Wrd_count & "times"

Set accap=nothing

Set accapp=nothing

 

Example 10: .Net 对象("DotNetFactory")

说明:DotNetFactory对象允许创建一个.NET对象,并访问其方法和属性

‘创建.Net的form对象

Dim var_CreateInstance

Set var_CreateInstance =DotNetFactory.CreateInstance("System.Windows.Forms.Form","System.Windows.Forms")

‘显示form

var_CreateInstance.Show

wait 2


‘关闭form

var_CreateInstance.Close

 

‘释放.Net对象

Set var_CreateInstance = nothing

VBScript/QTP 的常用COM对象列表,布布扣,bubuko.com

VBScript/QTP 的常用COM对象列表

原文:http://blog.csdn.net/elladu/article/details/25748289

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!