QTP简单框架(5)之脚本编写规范

  脚本是基于saffron来改写的,修改了2个比较大的地方:框架目前支持设置对象多个属性、通过CreationTime来控制当前浏览器。

  另外还添加了许多平时写脚本中需要的小功能。所以在正式编写脚本时,先看一下我们所要使用到的方法:  

  1、打开浏览器:OpenBrowser

' *********************************************************************************************
' 函数名称: OpenBrowser
' 函数说明: 打开一个浏览器窗口
' 参数说明: 无
' 返回结果: 无
' 调用举例: Call OpenBrowser
' ********************************************************************************************* 
Function OpenBrowser
    '首先判断是否已经初始化,False则执行InitializeFrame
    If not bln_Initialized Then 
        Call InitializeFrame
    End If
    Err.clear
    '运行iexplore.exe程序
    Systemutil.Run "iexplore.exe"
    If Err.number = 0 Then
        Int_CreationTime = Int_CreationTime +1
        ReportTo micPass, "Open Browser", "Open Browser ,CreateTime = "&Quote(Int_CreationTime)&",Success"
    Else
        ReportTo  micFail, "Open Browser", "Open Browser Fail"&chr(13)&"错误信息:"&Err.description
    End If
    '更新CreationTime值后,更新Browser、Page的描述性语句
    Call GenerateDescription
    '同步当前浏览器
    Call AutoSync
End Function

  2、打开网页地址:OpenUrl

' *********************************************************************************************
' 函数名称: OpenUrl
' 函数说明: 打开url地址
' 参数说明: 
'           (1).Str_Url: 网页地址
' 返回结果: 无
' 调用举例: Call OpenUrl("www.baidu.com")
' ********************************************************************************************* 
Function OpenUrl (Str_Url)
    '打开当前Str_Url
    Execute   BrowserDesc&"Navigate(" &Quote(Str_Url)&")"
    '根据Err对象的Number属性,获取错误的数值,如何错误的数值为0,则当前没有错误
    If Err.number=0 Then
        ReportTo micPass, "Open Url ", "Open Url : " & Quote(Str_Url)&",Success"
    Else
        ReportTo micFail, "Open Url ", "Open Url : " & Quote(Str_Url)&chr(13)&"错误信息:"&Err.description
    End If 
    '打开Str_Url之后,将Browser、Page同步
    Call AutoSync
End Function

  3、选择浏览器:BrowserSync

' *********************************************************************************************
' 函数名称: BrowserSync
' 函数说明: 打开多个浏览器窗口,同步需要操作的浏览器窗口
' 参数说明: 
'           (1).intCreateTime: 浏览器的CreateTime
' 返回结果: 无
' 调用举例: Call BrowserSync("1")
' ********************************************************************************************* 
Function BrowserSync(intCreateTime)
    'CInt():函数将一个表达式转化为数字类型
    Int_CreationTime = CInt(intCreateTime)
    '重新生成Browser、Page的描述性语句
    Call GenerateDescription
    ReportTo  micDone, "Browser Sync", "Sync Browser,CreateTime ="&intCreateTime&",has Synced"
End Function

 


  4、输入内容:InputText

' *********************************************************************************************
' 函数名称 : InputText
' 函数功能 : 输入框输入内容
' 函数输入 : Str_Property:对象的属性与属性值,书写格式:name:=test   多个属性与属性值已分号间隔
'           Str_InputText : 想要输入的内容         
'           Str_Name:对象的名称,为了方便在结果信息中查看
'函数输出 :  Null                  
' *********************************************************************************************
Function InputText(Str_Property,Str_InputText,Str_Name)
    On error resume next
    var=""
    Str_PropertyDesc = ""
    Str_PropertyDesc = GeneratePropertyDescription(Str_Property)
    Execute GenerateObjectDescription("WebEdit",Str_PropertyDesc)& "Set (" & Quote(Str_InputText) & ")"
    '获取文本框输入的内容,判断是否成功输入
    Execute "var= " & GenerateObjectDescription("WebEdit",Str_PropertyDesc ) & "GetROProperty(" & Quote("value") & ")"
    If Cstr(Str_InputText) = var Then
        ReportTo micPass, "Input Text", "Text: " & Quote(Str_InputText) & " was Input into WebEdit: " &Quote(Str_Name)
    Else
        ReportTo micFail, "Input Text", "Text: " & Quote(Str_InputText) & " was not Input  into WebEdit: " &Quote(Str_Name)&chr(13)&"错误信息:"&Err.description
    End If
End Function

 


  5、点击按钮:ClickButton

 

'*************************************************************
' 函数名称 : ClickButton
' 函数功能 : 点击按钮函数
' 函数输入 : Str_Property:对象的属性与属性值,书写格式:name:=test   多个属性与属性值已分号间隔
'            Str_Name:对象的名称,为了方便在结果信息中查看
' 函数输出 : Null                  
'*************************************************************
Function ClickButton(Str_Property,Str_Name)
    On error resume next
    Str_PropertyDesc = ""
    Str_PropertyDesc = GeneratePropertyDescription(Str_Property)
    Err.clear
    Execute  GenerateObjectDescription("WebButton",Str_PropertyDesc)& "Click"
    If Err.number=0 Then
        ReportTo micPass, "Click WebButton", "Click  WebButton "&Chr(32)& Quote(Str_Name) & " Success"
    Else
        ReportTo micFail, "Click WebButton", "Click  WebButton "&Chr(32)& Quote(Str_Name) & " Fail."&chr(13)&"错误信息:"&Err.description
    End If
End Function

 

 


  6、点击链接:ClickLink

 

'*************************************************************
' 函数名称 : ClickLink
' 函数功能 : 点击链接函数
' 函数输入 : Obj_Type : 链接的类型,包括:Link、Image、WebButton等等
'           Str_Property:对象的属性与属性值,书写格式:name:=test   多个属性与属性值已分号间隔
'           Str_Name:对象的名称,为了方便在结果信息中查看
' 函数输出 : Null                  
'*************************************************************
Function ClickLink(Obj_Type,Str_Property,Str_Name)
    On error resume next
    Str_PropertyDesc = ""
    var = ""
    Str_PropertyDesc = GeneratePropertyDescription(Str_Property)
    Execute "var="&GenerateObjectDescription(Obj_Type,Str_PropertyDesc)& "GetROProperty (" & Quote("target") & ")"
    Err.clear
    Execute  GenerateObjectDescription(Obj_Type,Str_PropertyDesc)& "Click"
    If Err.number=0 Then
        ReportTo micPass, "Click "& Obj_Type, "Click " & Obj_Type&Chr(32)& Quote(Str_Name) & " Success"
    Else
        ReportTo micFail, "Click "& Obj_Type, "Click  " & Obj_Type&Chr(32)& Quote(Str_Name) & " Fail.  错误信息:"&Err.description
    End If
    '判断当前链接打开的页面方式,若是新窗口打开,则同步新打开的窗口
    If  var = "_blank" Then
        Call BrowserSync(Int_CreationTime + 1)
    End If
End Function

 

 


  7、选择单选框:SelectCheckBox

 

'***************************************************************************
' 函数名称 : SelectCheckBox
' 函数功能 : 选择单选框
' 函数输入 : Str_Property:对象的属性与属性值,书写格式:name:=test   多个属性与属性值已分号间隔
'           Str_SetValue: 复选框的新状态:On、Off
'           Str_Name:对象的名称,为了方便在结果信息中查看
' 函数输出 : Null                  
'***************************************************************************
Function SelectCheckBox (Str_Property,Str_SetValue,Str_Name)
    On error resume next
    var  = ""
    Str_PropertyDesc = ""
    Str_PropertyDesc = DescriptionProperty(Str_Property)
    Err.clear
    Execute DescriptionObject("WebCheckBox", Str_PropertyDesc)& "Set " &Quote(Str_SetValue)
    Execute "var = "& DescriptionObject("WebCheckBox",Str_PropertyDesc)& "GetROProperty(" & Quote("checked") & ")"
    Select Case Str_SetValue
        Case "on"
            If  var = "1" Then
                ReportTo micPass,"Select WebRadioGroup", "The WebRadioGroup:"&Quote(Str_Name)&" item:" & Quote(Str_SelectText)& " was selected."
            Else
                ReportTo micFail, "Select WebRadioGroup","The WebRadioGroup:"&Quote(Str_Name)&" item:" & Quote(Str_SelectText)&  " was not selected."&chr(13)&"错误信息:"&Err.description
            End If
        Case "off"
            If  var = "0" Then
                ReportTo micPass,"Select WebRadioGroup", "The WebRadioGroup:"&Quote(Str_Name)&" item:" & Quote(Str_SelectText)& " was Cancled."
            Else
                ReportTo micFail, "Select WebRadioGroup","The WebRadioGroup:"&Quote(Str_Name)&" item:" & Quote(Str_SelectText)&  " was not Cancled."&chr(13)&"错误信息:"&Err.description
            End If
    End Select
End Function 

 

 


  8、选择下拉列表值:SelectList

 

'*************************************************************
' 函数名称 : SelectList
' 函数功能 : 选择下拉列表值
' 函数输入 : Str_Property:对象的属性与属性值,书写格式:name:=test   多个属性与属性值已分号间隔
'            Str_SelectText : 选择下拉项值         
'            Str_Name:对象的名称,为了方便在结果信息中查看
' 函数输出 : Null                  
'*************************************************************
Function SelectList (Str_Property,Str_SelectText,Str_Name)
    On error resume next
    var=""
    Str_PropertyDesc = ""
    Str_PropertyDesc = DescriptionProperty(Str_Property)
    Err.clear
    Execute  DescriptionObject("WebList", Str_PropertyDesc)& "Select " & Quote(Str_SelectText)
    Execute "var="&DescriptionObject("WebList", Str_PropertyDesc)& "GetROProperty (" & Quote("value") & ")"
    If var=Str_SelectText Then
        ReportTo micPass, "Select WebList", "The WebList: "& Quote(Str_Name)&" item:" & Quote(Str_SelectText) & " was selected."
    Else
        ReportTo micFail, "Select WebList", "The WebList: "& Quote(Str_Name)&" item:" & Quote(Str_SelectText) & " was not selected."&chr(13)&"错误信息:"&Err.description
    End If
End Function

 

 


  9、选择单选按钮:SelectRadioGroup

 

'*********************************************************************************************
' 函数名称 : SelectRadioGroup
' 函数功能 : 选择单选按钮
' 函数输入 : Str_Property:对象的属性与属性值,书写格式:name:=test   多个属性与属性值已分号间隔
'            Str_SelectText : 选择单选按钮     
'            Str_Name:对象的名称,为了方便在结果信息中查看
' 函数输出 : Null                  
'*********************************************************************************************
Function SelectRadioGroup (Str_Property,Str_SelectText,Str_Name)
    On error resume next
    var=""
    Str_PropertyDesc = ""
    Str_PropertyDesc = DescriptionProperty(Str_Property)
    Err.clear
    Execute DescriptionObject("RadioGroup", Str_PropertyDesc)& "Select " & Quote(Str_SelectText)
    Execute "var="& DescriptionObject("WebRadioGroup",Quote("value:="&Str_SelectText))& "GetROProperty(" & Quote("checked") & ")"
    If var ="1" Then
        ReportTo micPass,"Select RadioGroup", "The RadioGroup:"&Quote(Str_Name)&" item:" & Quote(Str_SelectText)& " was selected."
    Else
        ReportTo micFail, "Select RadioGroup","The RadioGroup:"&Quote(Str_Name)&" item:" & Quote(Str_SelectText)&  " was not selected."&chr(13)&"错误信息:"&Err.description
    End If
End Function 

 

 


  10、获取对象属性:GetPropertyValue

 

'********************************************************************************************
' 函数名称 : GetPropertyValue
' 函数功能 : 返回对象的某个属性值
' 函数输入 : Obj_Type : 链接的类型,包括:Link、Image、WebButton等等
'            Str_Property:对象的属性与属性值,书写格式:name:=test   多个属性与属性值已分号间隔
'            Str_Name:对象的名称,为了方便在结果信息中查看
' 函数输出 : Null                  
'********************************************************************************************
Function GetPropertyValue(Obj_Type,Str_Property,Str_ProName,Str_Name)
    On error resume next
    var = ""
    Str_PropertyDesc = ""
    Str_PropertyDesc = GeneratePropertyDescription(Str_Property)
    Err.clear
    Execute "var= " & GenerateObjectDescription(Obj_Type,Str_PropertyDesc ) & "GetROProperty(" & Quote(Str_ProName) & ")"
    If Err.number=0 Then
        ReportTo micPass, "Get Property Value",Obj_Type&": "& Quote(Str_Name)&"的" & Quote(Str_ProName)& "属性值为: " &Quote(var) & ",has got "
    Else
        ReportTo micFail , "Get Property Value",Obj_Type&": "& Quote(Str_Name)&"的" & Quote(Str_ProName)&"属性值" & "has not got 。"&chr(13)&"错误信息:"&Err.description
    End If 
    GetPropertyValue = var
End Function

 

 


  11、验证对象是否存在:VerifyObject

 

'********************************************************************************************
' 函数名称 : VerifyObject
' 函数功能 : 判断网页中对象是否存在
' 函数输入 : Obj_Type : 链接的类型,包括:Link、Image、WebButton等等
'            Str_Property:对象的属性与属性值,书写格式:name:=test   多个属性与属性值已分号间隔
'            Str_Name:对象的名称,为了方便在结果信息中查看
' 函数输出 : Boolean,对象是否存在                
'********************************************************************************************
Function VerifyObject (Obj_Type,Str_Property,Str_Name)
    On error resume next
    Str_PropertyDesc = ""
    Str_PropertyDesc = DescriptionProperty(Str_Property)
    Err.clear
    Execute "rval = " &DescriptionObject(Obj_Type,Str_PropertyDesc)&"Exist(3)"
    If rval Then
         ReportTo micPass,"Verification Object", "The " & Obj_Type &chr(32) & Quote(Str_Name) & " was verified to exist"
         VerifyObject=True
    Else
         ReportTo micFail,"Verification Object", "The " & Obj_Type &Chr(32) & Quote(Str_Name) & " was not found"
         VerifyObject=False
    End If
End Function

 

 


  12、关闭浏览器:CloseBrowsers

 

'*********************************************************************************************
' 函数名称: CloseBrowsers
' 函数说明: 关闭所有打开的IE,不使用CloseProcessByName ,容易造成IE 崩溃
' 参数说明: 无
' 返回结果: 无
' 调用举例: Call CloseBrowsers
'********************************************************************************************* 
Function CloseBrowsers
    '关闭浏览器的所有Dialog窗口,否则无法关闭IE浏览器
    Call CloseDialogs
    '获取当前IE浏览器的版本号
    intIEVersion = GetIEVersion()
    Set BrowserDesc_ = Browser("CreationTime:=0")
    'While循环等待,直到所有的IE浏览器都成功关闭
    While BrowserDesc_.Exist(0)
        BrowserDesc_.Close
        Set DialogDesc= Description.Create()
        DialogDesc("micClass").Value = "Dialog"
        Set DialogList = Desktop.ChildObjects(DialogDesc)
        If DialogList.count <>0 Then
            'IE7、8浏览器打开多个页签,QTP关闭IE时会出现提示信息
            print intIEVersion
            Select Case intIEVersion
                Case 7
                    'MessageDialog 关闭浏览器的提示信息框
                    MessageDialog "是否关闭所有选项卡?","2"
                Case 8
                    MessageDialog "关闭所有选项卡还是关闭"&chr(13)&chr(10)&"当前的选项卡?","2"
            End Select
        End If
        Call CloseDialogs
    Wend
    '关闭所有的Ie浏览器后,Int_CreationTime的值初始化
    Int_CreationTime = -1
    ReportTo  micDone, "Close Browsers", "Close All Exist Browsers has Done"
End Function

 

 



  13、关闭Dialog窗口:CloseDialogs

 

'*********************************************************************************************
' 函数名称: CloseDialogs
' 函数说明: 关闭所有打开的Dialog窗口
' 参数说明: 无
' 返回结果: 无
' 调用举例: Call CloseDialogs
'********************************************************************************************* 
Function CloseDialogs
    '定义一个Dialog窗体对象
    Set DialogDesc= Description.Create()
    DialogDesc("micClass").Value = "Dialog"
    '获取桌面上所有的Dialog对象
    Set DialogList = Desktop.ChildObjects(DialogDesc)
    '循环依次关闭所有的Dialog窗口
    If DialogList.count <> 0 Then
        For i = 0 to DialogList.Count - 1
            DialogList(i).Close
        Next 
    End If
End Function

 

 


  14、选择上传文件:UploadFile

 

'*********************************************************************************************
' 函数名称: UploadFile
' 函数说明: 选择、上传文件,支持单个文件和多个文件
' 参数说明: 
'           (1).strTitle: 文件选择框的标题
'           (2).strPicFile: 图片路径、文件夹路径
' 返回结果: 无
' 调用举例: Call UploadFile("选择要上载的文件,通过: www.renrenwei.com","c:\1.gif")
'********************************************************************************************* 
Function UploadFile(strTitle,strPicFile)
    Dim strPicFile_ : strPicFile_ = ""
    '定义一个Dialog对象
    Set DialogDesc=Description.Create()
    DialogDesc("micClass").Value = "Dialog"
    '获取桌面下所有的Dialog对象
    Set DialogList=Desktop.ChildObjects(DialogDesc)
    If  DialogList.count <> 0 Then
        For i=0 to DialogList.count-1
            '判断Dialog的Title值
            If  DialogList(i).GetROProperty("text") = strTitle Then
                '定义一个文本框对象
                Set objWinEdit = Description.Create()
                '定义一个按钮对象
                Set objWinButton = Description.Create()
                objWinEdit("micClass").Value = "WinEdit"
                objWinButton("micClass").Value = "WinButton"
                Set objList_1 = DialogList(i).ChildObjects(objWinEdit) 
                Set objList_2 = DialogList(i).ChildObjects(objWinButton) 
                Set oFso  =  CreateObject("Scripting.FileSystemObject")
                'GetFileType 判断输入的路径是文件还是文件夹
                '0-路径不存在 1-文件 2-文件夹
                Select Case GetFileType(strPicFile)
                    Case 0
                        ReportTo micFail, "Upload File", "UploadFile : 对不起,文件:"&Quote(strPicFile)&"不存在!"
                        Exit Function
                    Case 2
                        strPicFile_ = GetFilesString(strPicFile)
                        If Trim(strPicFile_) = ""  Then
                            ReportTo micFail, "Upload File", "UploadFile : 对不起,文件夹:"&Quote(strPicFile)&"为空!"
                            Exit Function
                        Else
                            objList_1(0).Set strPicFile
                            objList_2(0).click
                            strPicFile = strPicFile_
                        End If
                End Select
                objList_1(0).set strPicFile
                objList_2(0).click
                ReportTo micPassl, "Upload File", "UploadFile:"&vbcrlf&"文件:"&strPicFile&" has been selected!"
                Exit for
            End If
            If i = DialogList.count-1 Then
                ReportTo micFail, "Message Dialog ", "MessageDialog : "&vbcrlf&"对不起,没有找到标题为:"&Quote(strTitle)&"的文件选择框!"
            End If
        Next
    Else
        ReportTo micFail, "Message Dialog ", "MessageDialog: " & Quote("对不起,没有任何文件选择框!")
    End If
End Function

 

 


  15、发送邮件:SendMail

 

'*********************************************************************************************
' 函数名称: SendMail
' 函数说明: 自动发送邮件功能
' 参数说明: 
'           (1).SendFrom: 发件人帐号
'           (1).Password: 发件人帐号密码
'           (1).SendTo: 收件人,多个收件人可使用";"隔开
'           (1).MailTopic: 邮件主题
'           (1).MailBody: 邮件内容,此处内容格式为Html
'           (1).MailAttachment: 邮件附件,多个附件可使用";"隔开
' 返回结果: Boolean,邮件是否发送成功
' 调用举例: Call SendMail("Test@163.com","111111","TEST@126.com","主题","内容","附件")
'********************************************************************************************* 
Function SendMail(SendFrom,Password,SendTo,MailTopic,MailBody,MailAttachment) 
    On error resume next
    MS_Space = "http://schemas.microsoft.com/cdo/configuration/" 
    Set Email = CreateObject("CDO.Message") 
    Email.BodyPart.Charset = "GBK" 
    Email.From = SendFrom
    Email.To = SendTo
    Email.Subject = MailTopic 
    Email.Htmlbody = MailBody 
    If MailAttachment <> "" Then 
        For i=0 to Ubound(Split(MailAttachment,";"))
            Email.AddAttachment Split(MailAttachment,";")(i) 
        Next
    End If 
    With Email.Configuration.Fields 
        .Item(MS_Space&"sendusing") = 2 
        .Item(MS_Space&"smtpserver") = "smtp."&Split(SendFrom,"@",-1, vbTextCompare)(1)
        .Item(MS_Space&"smtpserverport") = 25 
        .Item(MS_Space&"smtpauthenticate") = 1 
        .Item(MS_Space&"sendusername") =Split(SendFrom,"@",-1,vbTextCompare)(0)
        .Item(MS_Space&"sendpassword") = Password
        .Update 
    End With 
    Err.clear
    Email.Send 
    If Err.number<>0 Then
        SendMail=False  
    Else
        SendMail=True
    End If
    Set Email=Nothing 
End Function 

 

 


  16、添加双引号:Quote

 

'**************************************************
' 函数名称: Quote
' 函数说明: 给指定的字符串前后添加双引号
' 参数说明: 
'           (1).Str_text: 想要添加双引号的字符串
' 返回结果: String,两边已添加双引号的字符串
' 调用举例: Call Quote("TEST")
'***************************************************
Function Quote(Str_text)
    Quote = chr(34) & Str_text & chr(34)
End Function

 

 


posted @ 2012-02-16 15:21  Sirrah  阅读(439)  评论(0编辑  收藏  举报