您的位置:首页 > 编程语言 > Qt开发

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

2015-11-18 17:12 585 查看

老李分享: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")

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

在poptest上自动化测试化的课程,工具本身的功能不是我们培训的重点,重点在于应用场景,利用技术去解决问题才是焦点,现在的自动化测试框架,自动化测试平台再向多任务,跨平台,pc端和移动端整合,框架的核心设计没有变,外层不外乎包裹的是技术的细节,我们在移动端的课程里加了跨平台的框架设计。

(我们已经开发了多个移动端的测试开发(自动化测试)如robotium,appuim,monkeyrunner等,移动互联网是未来的趋势,如何在这些设备上进行自动化测试,如何高效率的进行移动端的自动化测试,在海量机器上单位时间执行自动化测试case更多,我们在课程上利用真机进行模拟,项目是真实的商业项目,关于移动端(手机,pad,watch等)的自动化测试课程,请加微信号:liairan2015,咨询)

手机技术交流请加qq群:300897805
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: