18.3.4 测试ComponentOne的MainMenu控件(2)

18.3.4  测试ComponentOne的MainMenu控件(2)

把如下XML代码插入到QTP安装目录中的dat目录下的SwfConfig.xml文件中(注意修改DLL的文件路径):

  1. <!-- Merge this XML content into file "<QuickTest Professional>\dat\ SwfConfig.xml". --> 
  2.   <Control Type="C1.Win.C1Command.C1MainMenu" 
  3.     <CustomRecord
  4.       <Component
  5.         <Context>AUT</Context
  6.         <DllName>D:\QTP_dotNET\ComponentOne\MainMenu\QuickTestCustomServer_C1MainMenu\QuickTestCustomServer_C1MainMenu\Bin\QuickTestCustomServer_C1MainMenu.dll</DllName
  7.         <TypeName>QuickTestCustomServer_C1MainMenu.C1MainMenu</TypeName
  8.       </Component
  9.     </CustomRecord
  10.     <CustomReplay
  11.       <Component
  12.         <Context>AUT</Context
  13.         <DllName>D:\QTP_dotNET\ComponentOne\MainMenu\QuickTestCustomServer_C1MainMenu\QuickTestCustomServer_C1MainMenu\Bin\QuickTestCustomServer_C1MainMenu.dll</DllName
  14.         <TypeName>QuickTestCustomServer_C1MainMenu.C1MainMenu</TypeName
  15.       </Component
  16.     </CustomReplay
  17. <!--<Settings
  18.        <Parameter Name="sample name">sample value</Parameter
  19.     </Settings> --
  20.   </Control

这样,启动QTP之后,就可以直接使用QTP的录制功能来录制和产生ComponentOne的MainMenu控件脚本了,例如:

  1. SwfWindow("New document").SwfObject("SwfObject").C1MainMenu_Select "&File"  ‘可录制到主菜单的单击操作 

但是这种方法还有些缺陷,就是仅录制到主菜单的单击操作,无法录制到下一级菜单的单击操作,为此,我们改用VBS脚本封装一个专门的菜单选择函数:

  1. Function C1Menu_Select(w_C1MainMenu , MenuPath )  
  2.    Dim MainIndex , CmdLikObj  
  3.    MainIndex = -1  
  4.    ' 解析菜单路径  
  5.    MenuPathStr = Split (MenuPath,";")  
  6.    ' 获取指定主菜单的位置  
  7.    For I =0  to w_C1MainMenu.Object.CommandLinks.Count -1   
  8.        If w_C1MainMenu.Object.CommandLinks.Item(I).Text =  MenuPathStr(0) Then  
  9.             MainIndex = I  
  10.             Exit For  
  11.        End If  
  12.    Next  
  13.      
  14.    If MainIndex >=0 Then  
  15.         CommandBound = w_C1MainMenu.Object.CommandLinks.Item(MainIndex).Bounds  
  16.         CommandBoundsArray = Split(CommandBound,",")  
  17.         Bounds_XArray = Split(CommandBoundsArray(0),"=")  
  18.         Bounds_YArray = Split(CommandBoundsArray(1),"=")  
  19.         X = Bounds_XArray(1)  
  20.         Y = Bounds_YArray(1)  
  21.         ' 单击主菜单项  
  22.         w_C1MainMenu.Click X,Y  
  23.  
  24.         Set CmdLikObj = w_C1MainMenu.Object.CommandLinks.Item(MainIndex)  
  25.         Set DeviceReplay = CreateObject("Mercury.DeviceReplay")  
  26.         ' 如果只有一层子菜单  
  27.         If  UBound(MenuPathStr) = 1 Then  
  28.             ' 查找指定子菜单项的Index位置  
  29.             SubMenuIndex = FindSubMenuIndex(CmdLikObj,MenuPathStr(1))  
  30.             For J =0 to SubMenuIndex  
  31.                 DeviceReplay.PressKey 80 '通过按向下键移动到指定子菜单  
  32.             Next  
  33.             DeviceReplay.PressKey 28  '通过按回车键模拟单击菜单的动作  
  34.         Else ' 如有多层子菜单  
  35.             For I =1 to UBound(MenuPathStr)  
  36.                 If I = 1 Then  
  37.                     SubMenuIndex = FindSubMenuIndex(CmdLikObj,MenuPathStr(I))  
  38.                     For J =0 to SubMenuIndex  
  39.                         DeviceReplay.PressKey 80  
  40.                     Next  
  41.                     DeviceReplay.PressKey 77 ' 单击向右键打开下一层菜单  
  42.                     For K =0 to CmdLikObj.Command.CommandLinks.Count -1   
  43.                         If CmdLikObj.Command.CommandLinks.Item(K).Text = MenuPathStr(I) Then  
  44.                             Set CmdLikObjCmdLikObj = CmdLikObj.Command. CommandLinks.Item(K)  
  45.                         End If  
  46.                     Next              
  47.                 ElseIf I = UBound(MenuPathStr) Then  
  48.                     SubMenuIndex = FindSubMenuIndex(CmdLikObj,MenuPathStr(I))  
  49.                     For J =0 to SubMenuIndex-1  
  50.                         DeviceReplay.PressKey 80  
  51.                     Next  
  52.                     DeviceReplay.PressKey 28    
  53.                 Else  
  54.                     SubMenuIndex = FindSubMenuIndex(CmdLikObj,MenuPathStr(I))  
  55.                     For J =0 to SubMenuIndex-1  
  56.                         DeviceReplay.PressKey 80  
  57.                     Next  
  58.                     DeviceReplay.PressKey 77  
  59.                     For K =0 to CmdLikObj.Command.CommandLinks.Count -1   
  60.                         If CmdLikObj.Command.CommandLinks.Item(K).Text = MenuPathStr(I) Then  
  61.                             Set CmdLikObjCmdLikObj = CmdLikObj.Command. CommandLinks.Item(K)  
  62.                         End If  
  63.                     Next              
  64.                 End If  
  65.             Next  
  66.         End If  
  67.     End If  
  68. End Function  
  69. RegisterUserFunc "SwfObject","C1Menu_Select","C1Menu_Select"  
  70.  
  71. Function FindSubMenuIndex(w_CommandLinks , SubMenuText)  
  72.    Index = -1  
  73.    For I = 0 to w_CommandLinks.Command.CommandLinks.Count -1   
  74.         If w_CommandLinks.Command.CommandLinks.Item(I).Text <> "-" then  
  75.             IndexIndex = Index +1  
  76.             If  w_CommandLinks.Command.CommandLinks.Item(I).Text =SubMenuText then  
  77.                 FindSubMenuIndex = Index  
  78.                 Exit Function  
  79.             End If  
  80.         End IF    
  81.    Next  
  82. End Function  

这样就可以实现对ComponentOne的MainMenu控件的菜单选择操作了:

  1. SwfWindow("New document").Activate  
  2. SwfWindow("New document").SwfObject("c1MainMenu1").C1Menu_Select "File;Open"  
  3. SwfWindow("New document").SwfObject("c1MainMenu1").C1Menu_Select "File;New;Word"  
  4. SwfWindow("New document").SwfObject("c1MainMenu1").C1Menu_Select "File;New;PDF"  

猜你喜欢

转载自www.cnblogs.com/gdg87813/p/10956348.html