老李分享:qtp自动化测试框架赏析-关键字自动化测试框架(二)

老李分享:qtp自动化测试框架赏析-关键字自动化测试框架(二)

  为了展现关键字自动化测试框架的特点,利用对象的层次关系的特点,设计成关键字,这种关键字的缺陷就在于数据表格中的数据要求不仅仅对业务要熟悉,还要对技术有所了解,所以技术技巧大于实际应用价值。

  一.数据表格(execl中存储testcase,每个sheet是一个testcase,按照下面的方式填充)

   

二.核心脚本引擎

'Variable Initialization and Declaration
Dim sExcelLocation
Dim sParent, sParentProp, arrParent, arrParentProp, sControl, sControlProp, arrControlProp, sAction, sDataValue
Dim obj_hierarchy, control_object

sExcelLocation = "D:\QTP\QTP Framework Samples\QTP-Keyword-Driven-Framework-1\Flow Sheets\MercuryFlightReservation.xls"


'========================================
' Function Name - fnExecuteTestCase
' Purpose - This is just a wrapper function that calls fnReadExcel function
'========================================
Function fnExecuteTestCase(sTestCaseName)

  fnReadExcel(sTestCaseName)

End Function
'============= End Function ===============

 

'========================================
' Function Name - fnReadExcel
' Purpose - This function loads the excel sheet into QTP data table and stores the cell values in Global Variables
'========================================
Function fnReadExcel(sSheetName)

'Add the Data Sheet into QTP Data Table
DataTable.AddSheet("dtSheet")

'Import the Excel Sheet into QTP Data Table
DataTable.ImportSheet sExcelLocation, sSheetName, "dtSheet"

  'Loop through all the rows in the Data Sheet
  iRow = DataTable.GetSheet("dtSheet").GetRowCount

  For iR = 1 to iRow
  'Set the Current Row in the Data Sheet according to the loop counter
    DataTable.SetCurrentRow iR

    'Capture all the cell values in different variables
    sParent = DataTable("Parent", "dtSheet")
    sParentProp = DataTable("PProperty", "dtSheet")
    sControl = DataTable("Control", "dtSheet")
    sControlProp = DataTable("CProperty", "dtSheet")
    sAction = DataTable("Action", "dtSheet")
    sDataValue = DataTable("Data", "dtSheet")

    'Call the function that will convert the excel data into QTP readable format
    If sParent = "End" Then
      Exit For
    ElseIf sParent <> "" Then
      fnIdentifyParentHierarchy()
    Else
      'The action is independent of the all the controls (refer NOTE 2 from the article)
      fnAction()
    End If

Next

End Function
'============= End Function ===============

 

'========================================
' Function Name - fnIdentifyParentHierarchy
' Purpose - This function converts the values in cells A and B into QTP readable format
'========================================
Function fnIdentifyParentHierarchy()

'Split Parent Property so that multiple objects casn be resolved
arrParentProp = Split(sParentProp, ",")

  'Resolve the hierarchy of all the objects that are parent to the actual control
  Select Case sParent

  Case "Dialog"
    Set obj_hierarchy = Dialog(arrParentProp(0))

  Case "Window"
    Set obj_hierarchy = Window(arrParentProp(0))

  Case "Window,Dialog"
    Set obj_hierarchy = Window(arrParentProp(0)).Dialog(arrParentProp(1))

  Case "Window,Dialog,Dialog"
    Set obj_hierarchy = Window(arrParentProp(0)).Dialog(arrParentProp(1)).Dialog(arrParentProp(2))

  End Select

  'Call the function that will resolve the Control Object
  fnIdentifyControl()

End Function
'============= End Function ===============

 

'========================================
' Function Name - fnIdentifyControl
' Purpose - This function converts the values in cells C and D into QTP readable format and then combines values from Cells A, B, C and D to come up with a single object
'========================================
Function fnIdentifyControl()

  'Split the Control Property
  If sControl <> "" Then
    arrControlProp = Split(sControlProp, ":=")

    'Resolve the Control object to obtain the complete heirarchy on which the action can be performed
    Set child_object = Description.Create()
    child_object("micclass").value = sControl
    child_object(arrControlProp(0)).value = arrControlProp(1)

    'Create the object on which the action will be performed
    Set control_object = obj_hierarchy.ChildObjects(child_object)
  Else
    'Control Object is the parent hierarchy on which the action will be performed (refer NOTE 1 from the article)
    Set control_object = obj_hierarchy
    End If

  'Call the function that will perform the necessary action on the object
    fnAction()

End Function
'============= End Function ===============

 

'========================================
' Function Name - fnAction
' Purpose - This function performs action on the object based upon the defined keyword
'========================================
Function fnAction()

'Perform Action on the control_object based upon the keyword defined in the excel sheet
  Select Case sAction

  Case "SetValue"
    control_object(0).Set sDataValue

    Case "SelectMenu"
    control_object(0).Select sDataValue

  Case "Type"
    control_object(0).Type sDataValue

    Case "SelectDropDownValue"
    control_object(0).Select sDataValue

  Case "SelectWinListValue"
    control_object(0).Select sDataValue

  Case "Click"
    control_object(0).Click

  Case "Activate"
    control_object.Activate

  Case "WindowClose"
    control_object.Close

  Case "Run"
    SystemUtil.Run sDataValue

  Case "SendKeys"
    'This function uses SendKeys method to select value from the Menu
    Set WshShell = CreateObject("WScript.Shell")

    Select Case sDataValue
    Case "Alt F+N"
      WshShell.SendKeys "%(fn)"
    Case "Alt F+O"
      WshShell.SendKeys "%(fo)"
    End Select

    Set WshShell = Nothing

  End Select
End Function
'============= End Function ===============

三.在qtp中调用testcase

   'Execute --> TC_01_CreateOrder  测试用例名称(sheet名称)

  fnExecuteTestCase("TC_01_CreateOrder")

  'Execute --> TC_02_ModifyOrder
  fnExecuteTestCase("TC_02_ModifyOrder")

  'Execute --> TC_03_DeleteOrder
  fnExecuteTestCase("TC_03_DeleteOrder")

   关键字自动化测试框架形式千变万化,但是设计核心应该注意表格是测试开发工程师和功能测试工程师交流的通道,表格的关键字输入应本着对业务熟悉的测试工程师不存在填充障碍,让功能测试工程师更加专注于业务层面的测试。后台的技术的解决让测试开发工程师去解决(自动化测试工程师)。


你可能感兴趣的:(软件测试开发)