QTP 描述性编程进阶

上一篇 / 下一篇  2014-02-20 13:54:14 / 个人分类:QTP

Dim frameDesc:frameDesc = ""
Dim objectArray:objectArray = Split("Link,WebButton,WebList,WebEdit,Image,WebTable,Viewlink,WebArea,WebCheckBox,WebFile,WebRadioGroup,WebElement",",")

'打开网络地址
Sub LoginUrl (url)
   systemutil.Run "iexplore.exe",Url
End Sub

'关闭IE
Sub CloseIE ()
   systemutil.CloseProcessByName("iexplore.exe")
End Sub

'输出双引号
Public Function Quote (txt)
   Quote = Chr(34) & txt & Chr(34)
End Function

'获取字符或字符串在一个数组中的位置
'myArray为数组名称,myString为字符名称
Public Function IndexOf (myArray,myString)
myValue = -1
For i = 0 to UBound(myArray)
  If myArray(i) = myString Then
  myValue = i
  Exit For
  End If
Next
IndexOf = myValue
End Function

'验证报告输出
Public Function Report (status,objtype,text)
   Reporter.Filter = rtEnableAll
   Reporter.ReportEvent status,objtype,text
End Function

Public Function GenerateDescriptions(brNumber,ifModule)
Dim brinx:brinx = brNumber - 1
Dim windex:windex = ""
Dim bindex:bindex = ""
Dim descString:descString = ""
If ifModule = "Y" Or ifModule = True Then
  descString = "Window(" & Quote("nativeclass:=Internet Explorer_TridentDlgFrame") & "," & Quote("Location:=0") & ")." & "Page(" & Quote("micclass:Page") & "," & Quote("index:=0") & ")."
Else
bindex = "CreationTime:=" & brinx
descString = "Browser(" & Quote("micclass:=Browser") & "," & Quote(bindex) & ")." & "Page(" & Quote("micclass:=Page") & "," & Quote("index:=0") & ")."
End If
' descString = descString & frameDesc
GenerateDescriptions = descString
End Function

Public Function GenerateObjectDescriptions (objClassName,otherAtt)
   Dim objNameString:objNameString = ""
   Dim objNx:objNx = IndexOf(objectArray,objClassName)
   If objNx >= 0 Then
  bjNameString = objClassName & "("& Quote("micclass:="& objClassName) &"," & Quote(otherAtt) & "," & Quote("index:=0") & ")"
   End If
GenerateObjectDescriptions = objNameString
End Function

Public Function GenerateSpecifiedObject (className,textDesc,brNumber,ifModule)
Dim objectString:objectString = ""
Dim MySpecifiedObject:MySpecifiedObject = ""
objectString = GenerateDescriptions(brNumber,ifModule) & GenerateObjectDescriptions(className,textDesc)
Execute "Set MySpecifiedObject = " & objectString
Set GenerateSpecifiedObject = MySpecifiedObject
End Function

Public Function SetEditValue(specifiedText,EditValue,brNumber,ifModule)
   If  GenerateSpecifiedObject("WebEdit",specifiedText,brNumber,ifModule).exist(0)Then
GenerateSpecifiedObject("WebEdit",specifiedText,brNumber,ifModule).set EditValue
Report micDone,"函数SetEditValue操作完成","【WebEdit】【" & specifiedText & "】的值成功设置为" &EditValue
Else
Report micFail,"函数SetEditValue操作失败","【WebEdit】【" & specifiedText & "】未找到。"
End If
End Function

Public Function ClickSpecifiedObject (objectClass,specifiedText,brNumber,ifModule)
   Dim boolValue:boolValue = False
   Dim isDisabled:isDisabled = ""
   Dim isReadonly:isReadonly = ""
   Dim blVa11:blVa11 = (UCase(objectClass) <> "LINK")
   Dim blVa12:blVa12 = (UCase(objectClass) <> "WEBBUTTON")
   Dim blVa13:blVa13 = (UCase(objectClass) <> "IMAGE")
   Dim blVa14:blVa14 = (UCase(objectClass) <> "VIEWLINK")
   Dim blVa15:blVa15 = (UCase(objectClass) <> "WEBFILE")
   Dim blVa16:blVa16 = (UCase(objectClass) <> "WEBELEMENT")
   If blVa11 And blVa12 And blVa13 And blVa14 And blVa15 And blVa16 Then
  Report MicFail,"ClickSpecifiedObject函数参数错误","ClickSpecifiedObject函数只适用于LINK、WEBBUTTON、IMAGE、VIEWLINK、WEBFILE、WEBELEMENT。"
  Exit Function
   End If
   If Not GenerateSpecifiedObject (objectClass,specifiedText,brNumber,ifModule).exist(0) Then
  Report MicFail,"ClickSpecifiedObject操作失败","网页对象【"& objectClass &"】:【"& specifiedText &"】没有找到。"
  boolValue = False
Else
isDisabled = GenerateSpecifiedObject (objectClass,specifiedText,brNumber,ifModule).GetRoProperty("disabled")
isReadonly = GenerateSpecifiedObject (objectClass,specifiedText,brNumber,ifModule).GetRoProperty("readonly")
If Trim(isDisabled) = "1" Or Trim(isReadonly) = "1" Then
Report MicFail,"ClickSpecifiedObject操作失败","网页对象【"& objectClass &"】:【"& specifiedText &"】Disabled."
boolValue = False
Else
GenerateSpecifiedObject (objectClass,specifiedText,brNumber,ifModule).click
Report MicDone,"函数ClickSpecifiedObject操作完成","网页对象【"& objectClass &"】:【"& specifiedText &"】点击完成,并且已执行网页初始化。"
boolValue = True
' Syn
End If
   End If
ClickSpecifiedObject = boolValue
End Function

'点击复选框WebCheckBox
Public Function SetCheckBox(specifiedText,CBoxValue,brNumber,ifModule)
    If GenerateSpecifiedObject("WebCheckox",specifiedText,brNumber,ifModule).exist(0) Then
  If (CBoxValue = "ON" or CBoxValue = "OFF") Then
GenerateSpecifiedObject("WebCheckox",specifiedText,brNumber,ifModule).set CBoxValue
Report micDone,"函数SetCheckBox操作完成","【WebCheckox】:【"& specifiedText &"】的值已经设置为"&CBoxValue&"! "
else
Report micFail,"输入的参数无效:","输入参数【CBoxValue】:【"& CBoxValue &"】无效,【CBoxValue】的值只能是OFF或ON! "
  End If
else
Report micFail,"函数SetCheckBox操作失败:","网页对象【WebCheckox】:【"& specifiedText &"】未找到! "
    End If
End Function

'选择网页单选框WebRadioGroup
Public Function SelectWebRadioGroup (radioName,attValue,brNumber,ifModule)
   Dim localDesc:localDesc = ""
   Dim canSelect:canSelect = False
   Dim boolValue:boolValue = False
   Dim isDisabled:isDisabled = ""
localDesc = GenerateDescriptions(brNumber,ifModule) & GenerateObjectDescriptions("WebRadioGroup",radioName)
Execute "Set MyRadioGroup = " & localDesc
If Not MyRadioGroup.Exist(0) Then
Report micFail,"Web单选框查询失败","单选框【"& radioName &"】按照关键字【"& attValue &"】没有查询到! "
Exit Function
else
isDisabled = MyRadioGroup.GetROProperty("disabled")
End If
If Trim(isDisabled) = "0" Then
MyRadioGroup.Select attValue
Report micDone,"Web单选框选择成功","选择项【"& attValue &"】已经被查询到、选择,并且执行初始化!"
boolValue = True
else
Report micFail,"Web单选框选择失败","单选框【"& radioName &"】不可选择编辑! "
boolValue = False
End If
' SyncronizeBrowser
SelectWebRadioGroup = boolValue
End Function

'从指定的WebList下拉列表中选择指定项(Tirm 去空格,不可模糊匹配)
Public Function SelectFromWebList (listName,textValue,brNumber,ifModule)
   Dim localDesc:localDesc = ""
   Dim canSelect:canSelect = False
   Dim boolValue:boolValue = False
localDesc = GenerateDescriptions(brNumber,ifModule) & GenerateObjectDescriptions("WebList",listName)
Execute "Set MyWebList  = " & localDesc
If Not MyWebList.Exist(0) Then
Report micFail,"SelectFromWebList","网页下拉列表【"& listName &"】按照关键字【"& textValue &"】没有查询到! "
Exit Function
End If
iCount = MyWebList.GetROProperty("items count")
canModl = MyWebList.GetROProperty("disabled")
For i = 1 to iCount
itemValue = MyWebList.GetItem(i)
If Trim(itemValue) = Trim(textValue) And canModl = "0" Then
canSelect = True
Exit For
End If
Next
If  canSelect Then
MyWebList.Select itemValue
' SyncronizeBrowser
Report micDone,"下拉表格选择成功","选择项【"& itemValue &"】已经被查询到、选择,并且执行初始化!"
boolValue = True
else
Report micFail,"下拉表格选择失败","单选框【"& listName &"】不可编辑! "
boolValue = False
End If
SelectFromWebList = boolValue
End Function

'验证网页中文本的存在
Public Function CheckTextExist(specifiedText,brNumber,ifModule)
   Dim boolValue:boolValue = False
   Dim specifiedAtt:specifiedAtt = ""
   Dim specifiedPro:specifiedPro = ""
specifiedAtt = Split(specifiedText,":=",-1,1)
specifiedPro = specifiedAtt(0)
If GenerateSpecifiedObject("WebElement",specifiedText,brNumber,ifModule).exist(0) Then
Report micPass,"匹配对象成功","文本内容【"&specifiedAtt(1)&"】验证成功"
boolValue = True
else
Report micFail,"匹配对象失败","文本内容【"&specifiedAtt(1)&"】验证失败"
boolValue = False
End If
CheckTextExist = boolValue
End Function

'验证网页中文本的不存在
Public Function CheckTextNotExist(specifiedText,brNumber,ifModule)
   Dim boolValue:boolValue = False
   Dim specifiedAtt:specifiedAtt = ""
   Dim specifiedPro:specifiedPro = ""
specifiedAtt = Split(specifiedText,":=",-1,1)
specifiedPro = specifiedAtt(0)
If Not GenerateSpecifiedObject("WebElement",specifiedText,brNumber,ifModule).exist(0) Then
Report micPass,"匹配对象失败","文本内容【"&specifiedAtt(1)&"】不存在,验证成功"
boolValue = True
else
Report micFail,"匹配对象成功","文本内容【"&specifiedAtt(1)&"】存在,验证失败"
boolValue = False
End If
CheckTextNotExist = boolValue
End Function




TAG: QTP 描述性编程进阶

1093322329的个人空间 引用 删除 1093322329   /   2015-02-24 00:57:01
谢谢分享!
 

评分:0

我来说两句

我的栏目

日历

« 2024-05-02  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 4634
  • 日志数: 9
  • 建立时间: 2014-02-20
  • 更新时间: 2015-07-01

RSS订阅

Open Toolbar