学习QTP AOM之Action对象 一

上一篇 / 下一篇  2012-10-18 10:11:07 / 个人分类:QTP的学习研究

action对象下又包含Objectrepositories和ActionParameterDefinitions对象
action对象有AddNewAction,GetScript,SetScript,ValidateScript四种方法,ActionParameterDefinitions,Description,Location,Name,ObjectRepositories,Type6种属性。
首先学习Action的几种方法和属性的使用。
AddNewAction
 
描述:创建一个新action,并且该action有指定脚本内容,当前action调用该action。
注:1.如果脚本有语法或其他错误,addnewaction 步骤就失败;2.使用此方法,你只能在父action脚本开始或最后添加新action。
语法:object.addnewAction(Name,Description,ScriptContent,Reusable,ActionPosition)
详细信息:
 Argument               Description
 object                 action 类型的对象表达式
 name                   必选参数。字符串类型。新action的名称
 Description            必选。字符串类型。对action的文字描述
 ScriptContent          必选。字符串类型。单个字符串包含了action脚本的VBscript内容。断行使用VBCRLF或者chr(13)+chr(11).
 Reusable               必选。布尔类型。指示新action是否设置为可重用
 ActionPosition         必选。预定义常数或数字。查看如下常数表格。指示是在父action的第一步还是最后一步调用新action

ActionPosition参数可能的值
 常数           值   描述
 qtatbegining   1  父action的开始
 qtatEnd        2  父action的最后
返回类型:action对象
 
实例:
'************************************************************************************************************************
' Dim qtApp, ActionContent, ActionName, ActionDescr, NewAction, SecondAction, script
Set qtApp = CreateObject("QuickTest.Application") ' Create the application object
qtApp.Launch
qtApp.Visible = True
qtApp.New
ActionContent = "Window(""Calculator"").WinButton(""1"").Click" & vbCrLf & "Window(""Calculator"").WinButton(""+"").Click" & vbCrLf & "Window(""Calculator"").WinButton(""+"").Click" & vbCrLf & "Window(""Calculator"").WinButton(""="").Click"
ActionDescr = "A new sample action for the test."
ActionName = "Action" + CStr(qtApp.Test.Actions.Count + 1)
'Add a new action at the begining of the test
Set NewAction = qtApp.Test.AddNewAction(ActionName, ActionDescr, ActionContent, False, qtAtBegining)
'Add another action and call it from the begining of the action that was just added above.
ActionContent = "Function f1(str)" & vbCrLf & vbTab & "msgbox str" & vbCrLf & "End Function"
ActionName = "Action" + CStr(qtApp.Test.Actions.Count + 1)
Set SecondAction = NewAction.AddNewAction(ActionName, ActionDescr, ActionContent, True, qtAtBegining)
'Use the Load Script. function to store the content of the first new action into the script. array
script. = Load_Script(NewAction.GetScript())
'Move Call to the action to another script. line
script. = Move_CallAction(script, SecondAction.Name, UBound(script))
'Convert script. array to the text
ActionContent = Save_Script(script)
'Set new script. source to the action
scriptError = NewAction.ValidateScript(ActionContent)
If Len(scriptError) = 0 Then
    NewAction.SetScript. ActionContent
    NewAction.Description = "Check the calculator application and run next Action"
    NewAction.Type = "Reusable" 'Put non-reusable type to the new action
    Set aParamDefs = NewAction.ActionParameterDefinitions
    aParamDefs.Add "InParam", "An input action parameter definition", 0, 0, "Input Value" 'Add an input string parameter to the newly created action
    aParamDefs.Add "ResParam", "An output action parameter definition", 0, 1, "Output Value" 'Add an output string parameter to the newly created action
    Set resParamDef = aParamDefs.Item ("ResParam")
    resParamDef.Type = 1 'Put the boolean parameter type
    resParamDef.DefaultValue = false
    resParamDef.Description = "Result parameter"
Else
    Err.Raise E_SCRIPT_NOT_VALID, "Validate Script. Error", scriptError
End If
Const E_SCRIPT_TABLE = 1
Const E_EMPTY_SCRIPT. = 2
Const E_SCRIPT_NOT_VALID = 3
Public Function Load_Script(src)
    If Len(src) = 0 Then
        Err.Raise E_SCRIPT_TABLE, "Load_Script", "Script. is empty"
    End If
    Load_Script. = Split(Trim(src), vbCrLf)
End Function
Public Function Save_Script(script)
   If Not IsArray(script) Then
       Err.Raise E_SCRIPT_TABLE, "Save_Script", "Script. should be string array"
   End If
    If UBound(script) - LBound(script) = 0 Then
        Err.Raise E_SCRIPT_TABLE, "Save_Script", "Script. is empty"
    End If
    Save_Script. = Join(script, vbCrLf)
End Function
Public Function Find_Line(script, criteria)
    Dim rExp, I
    '********************************************************************
    'Verify that the first argument contains a string array
   If Not IsArray(script) Then
       Err.Raise E_SCRIPT_TABLE, "Find_Line", "The script. should be a string array"
   End If
    Set rExp = New RegExp
    ptrn = ""
    If IsArray(criteria) Then
        ptrn = Join(criteria, " * ")
    Else
        ptrn = criteria
    End If
    rExp.Pattern = ptrn 'Set pattern string
    rExp.IgnoreCase = True ' Set case insensitivity.
    rExp.Global = True ' Set global applicability.
    I = 0
    For Each scrItem In script
        If rExp.Execute(scrItem).Count > 0 Then
            Find_Line = I
            Exit Function
        End If
        I = I + 1
    Next
    Find_Line = -1
End Function
Public Function Move_Line(script, curPos, newPos)
    '********************************************************************
    'Verify that the first argument contains a string array
   If Not IsArray(script) Then
       Err.Raise E_SCRIPT_TABLE, "Move_Line", "Script. should be string array"
   End If
   scrLen = UBound(script) - LBound(script)
    If curPos = newPos Or curPos < 0 Or newPos < 0 Or scrLen < curPos Or scrLen < newPos Then
        Move_Line = script
        Exit Function
    End If
    tmpLine = script(curPos)
    If newPos > curPos Then
        For curPos = curPos + 1 To scrLen
            script(curPos - 1) = script(curPos)
            If curPos = newPos Then
                script(curPos) = tmpLine
                Exit For
            End If
        Next
    Else
        For curPos = curPos - 1 To 0 Step -1
            script(curPos + 1) = script(curPos)
            If curPos = newPos Then
                script(curPos) = tmpLine
                Exit For
            End If
        Next
    End If
    Move_Line = script
End Function
Function Insert_Line(script, lineSrc, linePos)
    '********************************************************************
    'Verify that the first argument contains a string array
   If Not IsArray(script) Then
       Err.Raise E_SCRIPT_TABLE, "Insert_Line", "Script. should be string array"
   End If
   scrLen = UBound(script) - LBound(script)
   If (scrLen = 0 And linePos <> 0) Or (linePos > scrLen + 1) Then
        Insert_Line = script
        Exit Function
   End If
   newScript. = Split(String(scrLen + 1, " "), " ")
   shiftIndex = 0
   For I = 0 To scrLen + 1
        If linePos = I Then
            newScript(I) = lineSrc
            shiftIndex = 1
        Else
            newScript(I) = script(I + shiftIndex)
        End If
   Next
   Insert_Line = newScript
End Function
Function Delete_Line(script, linePos)
    '********************************************************************
    'Verify that the first argument contains a string array
   If Not IsArray(script) Then
       Err.Raise E_SCRIPT_TABLE, "Delete_Line", "Script. should be string array"
   End If
    scrLen = UBound(script) - LBound(script)
   If (scrLen = 0) Or (linePos > scrLen) Then
        Insert_Line = script
        Exit Function
   End If
   If scrLen = 1 Then
       Delete_Line = Array()
       Exit Function
   End If
    newScript. = Split(String(scrLen - 1, " "), " ")
    shiftIndex = 0
    For I = 0 To scrLen
        If linePos = I Then
            shiftIndex = 1
        Else
            newScript(I - shiftIndex) = script(I)
        End If
    Next
    Delete_Line = newScript
End Function
Public Function Move_CallAction(script, actName, newPos)
    curPos = Find_Line(script, Array("RunAction", """" & actName & """"))
    Move_CallAction = Move_Line(script, curPos, newPos)
End Function
 

 

TAG:

 

评分:0

我来说两句

Open Toolbar