18.3.4 测试ComponentOne的MainMenu控件(2)
把如下XML代码插入到QTP安装目录中的dat目录下的SwfConfig.xml文件中(注意修改DLL的文件路径):
- <!-- Merge this XML content into file "<QuickTest Professional>\dat\ SwfConfig.xml". -->
- <Control Type="C1.Win.C1Command.C1MainMenu" >
- <CustomRecord>
- <Component>
- <Context>AUT</Context>
- <DllName>D:\QTP_dotNET\ComponentOne\MainMenu\QuickTestCustomServer_C1MainMenu\QuickTestCustomServer_C1MainMenu\Bin\QuickTestCustomServer_C1MainMenu.dll</DllName>
- <TypeName>QuickTestCustomServer_C1MainMenu.C1MainMenu</TypeName>
- </Component>
- </CustomRecord>
- <CustomReplay>
- <Component>
- <Context>AUT</Context>
- <DllName>D:\QTP_dotNET\ComponentOne\MainMenu\QuickTestCustomServer_C1MainMenu\QuickTestCustomServer_C1MainMenu\Bin\QuickTestCustomServer_C1MainMenu.dll</DllName>
- <TypeName>QuickTestCustomServer_C1MainMenu.C1MainMenu</TypeName>
- </Component>
- </CustomReplay>
- <!--<Settings>
- <Parameter Name="sample name">sample value</Parameter>
- </Settings> -->
- </Control>
这样,启动QTP之后,就可以直接使用QTP的录制功能来录制和产生ComponentOne的MainMenu控件脚本了,例如:
- SwfWindow("New document").SwfObject("SwfObject").C1MainMenu_Select "&File" ‘可录制到主菜单的单击操作
但是这种方法还有些缺陷,就是仅录制到主菜单的单击操作,无法录制到下一级菜单的单击操作,为此,我们改用VBS脚本封装一个专门的菜单选择函数:
- Function C1Menu_Select(w_C1MainMenu , MenuPath )
- Dim MainIndex , CmdLikObj
- MainIndex = -1
- ' 解析菜单路径
- MenuPathStr = Split (MenuPath,";")
- ' 获取指定主菜单的位置
- For I =0 to w_C1MainMenu.Object.CommandLinks.Count -1
- If w_C1MainMenu.Object.CommandLinks.Item(I).Text = MenuPathStr(0) Then
- MainIndex = I
- Exit For
- End If
- Next
- If MainIndex >=0 Then
- CommandBound = w_C1MainMenu.Object.CommandLinks.Item(MainIndex).Bounds
- CommandBoundsArray = Split(CommandBound,",")
- Bounds_XArray = Split(CommandBoundsArray(0),"=")
- Bounds_YArray = Split(CommandBoundsArray(1),"=")
- X = Bounds_XArray(1)
- Y = Bounds_YArray(1)
- ' 单击主菜单项
- w_C1MainMenu.Click X,Y
- Set CmdLikObj = w_C1MainMenu.Object.CommandLinks.Item(MainIndex)
- Set DeviceReplay = CreateObject("Mercury.DeviceReplay")
- ' 如果只有一层子菜单
- If UBound(MenuPathStr) = 1 Then
- ' 查找指定子菜单项的Index位置
- SubMenuIndex = FindSubMenuIndex(CmdLikObj,MenuPathStr(1))
- For J =0 to SubMenuIndex
- DeviceReplay.PressKey 80 '通过按向下键移动到指定子菜单
- Next
- DeviceReplay.PressKey 28 '通过按回车键模拟单击菜单的动作
- Else ' 如有多层子菜单
- For I =1 to UBound(MenuPathStr)
- If I = 1 Then
- SubMenuIndex = FindSubMenuIndex(CmdLikObj,MenuPathStr(I))
- For J =0 to SubMenuIndex
- DeviceReplay.PressKey 80
- Next
- DeviceReplay.PressKey 77 ' 单击向右键打开下一层菜单
- For K =0 to CmdLikObj.Command.CommandLinks.Count -1
- If CmdLikObj.Command.CommandLinks.Item(K).Text = MenuPathStr(I) Then
- Set CmdLikObjCmdLikObj = CmdLikObj.Command. CommandLinks.Item(K)
- End If
- Next
- ElseIf I = UBound(MenuPathStr) Then
- SubMenuIndex = FindSubMenuIndex(CmdLikObj,MenuPathStr(I))
- For J =0 to SubMenuIndex-1
- DeviceReplay.PressKey 80
- Next
- DeviceReplay.PressKey 28
- Else
- SubMenuIndex = FindSubMenuIndex(CmdLikObj,MenuPathStr(I))
- For J =0 to SubMenuIndex-1
- DeviceReplay.PressKey 80
- Next
- DeviceReplay.PressKey 77
- For K =0 to CmdLikObj.Command.CommandLinks.Count -1
- If CmdLikObj.Command.CommandLinks.Item(K).Text = MenuPathStr(I) Then
- Set CmdLikObjCmdLikObj = CmdLikObj.Command. CommandLinks.Item(K)
- End If
- Next
- End If
- Next
- End If
- End If
- End Function
- RegisterUserFunc "SwfObject","C1Menu_Select","C1Menu_Select"
- Function FindSubMenuIndex(w_CommandLinks , SubMenuText)
- Index = -1
- For I = 0 to w_CommandLinks.Command.CommandLinks.Count -1
- If w_CommandLinks.Command.CommandLinks.Item(I).Text <> "-" then
- IndexIndex = Index +1
- If w_CommandLinks.Command.CommandLinks.Item(I).Text =SubMenuText then
- FindSubMenuIndex = Index
- Exit Function
- End If
- End IF
- Next
- End Function
这样就可以实现对ComponentOne的MainMenu控件的菜单选择操作了:
- SwfWindow("New document").Activate
- SwfWindow("New document").SwfObject("c1MainMenu1").C1Menu_Select "File;Open"
- SwfWindow("New document").SwfObject("c1MainMenu1").C1Menu_Select "File;New;Word"
- SwfWindow("New document").SwfObject("c1MainMenu1").C1Menu_Select "File;New;PDF"