18.3 使用.NET插件扩展技术处理第三方控件

.NET的第三方控件包有很多,其中比较流行的有ComponentOne、DevComponents等,下面介绍如何利用.NET插件扩展开发包来处理这些第三方控件。

18.3.1  在Visual Studio中编写插件扩展代码

ComponentOne是.NET常用的第三方控件,其中的Toolbar控件在QTP中不能很好地支持录制和识别。在这种情况下,可以考虑使用QTP的.NET的插件扩展SDK开发一个插件扩展来支持该控件的录制和识别。

对于ComponentOne的ToolBar控件,我们可以采用QTP.NET插件扩展技术来处理,在开始之前,最好查阅ComponentOne的帮助文档,看看关于Toolbar控件的相关接口、类的描述。其中可以重点看看Command Links和Command的内容,因为Toolbar控件就是基于这两个核心的类实现的,在开发插件扩展时也需要使用到。

下面是在Visual Studio 2005中编写的插件扩展代码:

  1. using System;  
  2. using Mercury.QTP.CustomServer;  
  3. using System.Windows.Forms;  
  4. using QuickTestCustomServer_C1ToolBar;  
  5. using C1.Win.C1Command; // 引用ComponentOne的命令空间  
  6. namespace QuickTestCustomServer_C1ToolBar  
  7. {  
  8.     [ReplayInterface]  
  9.     public interface IC1ToolBarReplay  
  10.     {  
  11.         void C1ToolBar_Click(string text); // ToolBar单击事件  
  12.     }  
  13.     public class C1ToolBar :  
  14.         CustomServerBase,  
  15.         IC1ToolBarReplay  
  16.     {  
  17.         public C1ToolBar()  
  18.         {  
  19.         }  
  20.         // 监听Toolbar的单击事件  
  21.         public override void InitEventListener()  
  22.         {  
  23.             C1.Win.C1Command.C1ToolBar oControl =  
  24.                (C1.Win.C1Command.C1ToolBar)SourceControl;     
  25.             for (int i = 0; i oControl.CommandLinks.Count; i++)  
  26.             {  
  27.                 oControl.CommandLinks[i].Command.Click +=   
  28.             new C1.Win.C1Command.ClickEventHandler(this.oControl_ CommandClick);  
  29.             }  
  30.         }  
  31.         public override void ReleaseEventListener()  
  32.         {  
  33.         }  
  34.         // 把Toolbar单击事件录制下来  
  35.         private void oControl_CommandClick(object sender,  
  36.                                          C1.Win.C1Command.ClickEventArgs e)  
  37.         {  
  38.             C1.Win.C1Command.C1ToolBar oControl =  
  39.                       (C1.Win.C1Command.C1ToolBar)SourceControl;  
  40.             base.RecordFunction("C1ToolBar_Click",                                      RecordingMode.RECORD_SEND_LINE, e.CallerLink.Text);  
  41.         }  
  42.         // 回放录制的脚本  
  43.         public void C1ToolBar_Click(string text)  
  44.         {  
  45.             C1.Win.C1Command.C1ToolBar oControl =  
  46.                           (C1.Win.C1Command.C1ToolBar)SourceControl;  
  47.             // 查找Toolbar中指定的项  
  48.             for (int i = 0; i oControl.CommandLinks.Count; i++)  
  49.             {  
  50.                 // 如果找到  
  51.                 if (oControl.CommandLinks[i].Text == text)  
  52.                 {  
  53.                     if (oControl.CommandLinks[i].Command.IsParent == true)  
  54.                     {  
  55.                         System.Drawing.Rectangle oRect =  
  56. oControl.CommandLinks[i].Bounds;  
  57.                         int x = oRect.X + oRect.Width - 2;  
  58.                         int y = oRect.Y + oRect.Height/2;  
  59.                         // 单击找到的按钮的中间位置   
  60.                         base.MouseClick(x, y,  
  61.                                     MOUSE_BUTTON.LEFT_MOUSE_BUTTON);  
  62.                     }  
  63.                     else   
  64.                     {   
  65.                     System.Drawing.Rectangle oRect =  
  66. oControl.CommandLinks[i].Bounds;  
  67.                     int x = oRect.X + oRect.Width / 2;  
  68.                     int y = oRect.Y + oRect.Height / 2;  
  69.                     base.MouseClick(x, y, MOUSE_BUTTON.LEFT_MOUSE_BUTTON);  
  70.                     }  
  71.                     break;  
  72.                 }  
  73.             }  
  74.             base.ReplayReportStep("C1ToolBar_Click",                                                     EventStatus.EVENTSTATUS_GENERAL, text);  
  75.           }  
  76.     }  
  77. }  

18.3.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.C1ToolBar" 
    3. <CustomRecord
    4. <Component
    5. <Context>AUT</Context
    6. <DllName>D:QTP_dotNETComponentOneToolBar QuickTestCustomServer_C1ToolBarQuickTestCustomServer_C1ToolBarinQuickTestCustomServer_C1ToolBar.dll</DllName
    7. <TypeName>QuickTestCustomServer_C1ToolBar.C1ToolBar</TypeName
    8. </Component
    9. </CustomRecord
    10. <CustomReplay
    11. <Component
    12. <Context>AUT</Context
    13. <DllName>D:QTP_dotNETComponentOneToolBar QuickTestCustomServer_C1ToolBarQuickTestCustomServer_C1ToolBarinQuickTestCustomServer_C1ToolBar.dll</DllName
    14. <TypeName>QuickTestCustomServer_C1ToolBar.C1ToolBar</TypeName
    15. </Component
    16. </CustomReplay
    17. <!--<Settings
    18. <Parameter Name="sample name">sample value</Parameter
    19. </Settings> --
    20. </Control>

18.3.3  在QTP中使用插件扩展的代码

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

  1. SwfWindow("New document").Activate  
  2. SwfWindow("New document").SwfObject("SwfObject").C1ToolBar_Click "&New"  
  3. SwfWindow("New document").SwfObject("SwfObject").C1ToolBar_Click "E&xit"  
原文地址:https://www.cnblogs.com/gdg87813/p/10956336.html