18.3.4 测试ComponentOne的MainMenu控件(1)

18.3.4  测试ComponentOne的MainMenu控件(1)

对于如图18.10所示的ComponentOne的MainMenu控件,同样采用QTP的.NET插件扩展技术来处理。下面是在Visual Studio 2005中编写的插件扩展代码:

 
图18.10  ComponentOne的MainMenu控件
  1. using System;  
  2. using Mercury.QTP.CustomServer;  
  3. using System.Windows.Forms;  
  4. namespace QuickTestCustomServer_C1MainMenu  
  5. {  
  6.     [ReplayInterface]  
  7.     public interface IC1MainMenuReplay  
  8.     {  
  9.         #region Wizard generated sample code (commented)  
  10.         //      void  CustomMouseDown(int X, int Y);  
  11.         #endregion  
  12.  
  13.         void C1MainMenu_Select(string Memu);  
  14.     }  
  15.     /// <summary
  16.     /// Summary description for C1MainMenu.  
  17.     /// </summary
  18.     public class C1MainMenu :  
  19.         CustomServerBase,  
  20.         IC1MainMenuReplay  
  21.     {  
  22.         // You shouldn't call Base class methods/properties at the constructor  
  23.         // since its services are not initialized yet.  
  24.         public C1MainMenu()  
  25.         {  
  26.             //  
  27.             // TODO: Add constructor logic here  
  28.             //  
  29.         }  
  30.  
  31.         #region IRecord override Methods  
  32.         #region Wizard generated sample code (commented)  
  33.         /*      /// <summary
  34.         /// To change Window messages filter implement this method.  
  35.         /// The default implementation is to get only Control's window messages.  
  36.         /// </summary
  37.         public override WND_MsgFilter GetWndMessageFilter()  
  38.         {  
  39.             return WND_MsgFilter.WND_MSGS;  
  40.         }  
  41.  
  42.         /// <summary
  43.         /// To catch window messages you should implement this method.  
  44.         /// Please note: This method is called just in case the CustomServer is running  
  45.         /// under QuickTest process.  
  46.         /// </summary
  47.         public override RecordStatus OnMessage(ref Message tMsg)  
  48.         {  
  49.             // TODO:  Add OnMessage implementation.  
  50.             return RecordStatus.RECORD_HANDLED;  
  51.         }  
  52. */  
  53.         #endregion  
  54.         /// <summary
  55.         /// In case you extend Record process, you should add your Events handlers  
  56.         /// in order to listen to Control's Events.  
  57.         /// </summary
  58.         public override void InitEventListener()  
  59.         {  
  60.             #region Wizard generated sample code (commented)  
  61.             /*          // Notice, You can add as many handlers as you need.  
  62.             // Adding OnMouseDown handler.  
  63.             Delegate  e = new System.Windows.Forms.MouseEventHandler(this.OnMouseDown);  
  64.  
  65.             // Adds an event handler as the first handler of the event.  
  66.             // The first argument is the name of the event for which to listen.  
  67.             // You must provide an event that the control supports.  
  68.             // Use the .NET Spy to obtain the list of events supported by the control.  
  69.             // The second argument is the event handler delegate.   
  70.  
  71.             AddHandler("MouseDown", e);  
  72. */  
  73.             #endregion  
  74.  
  75.             C1.Win.C1Command.C1MainMenu oControl = (C1.Win.C1Command. C1MainMenu)SourceControl;  
  76.             oControl.MouseDown += new MouseEventHandler(this.oControl_ CommandClick);  
  77.  
  78.         }  
  79.  
  80.         /// <summary
  81.         /// At the end of the Record process this method is called by QuickTest to release   
  82.         /// all the handlers the user added in InitEventListener method.  
  83.         /// Please note: Handlers added via QuickTest methods are released by QuickTest infrastructure.  
  84.         /// </summary
  85.         public override void ReleaseEventListener()  
  86.         {  
  87.             //C1.Win.C1Command.C1MainMenu oControl = (C1.Win.C1Command. C1MainMenu)SourceControl;  
  88.             //oControl.Click -= new C1.Win.C1Command.ClickEventHandler (this.oControl_CommandClick);  
  89.         }  
  90.  
  91.         #endregion  
  92.  
  93.         #region Record events handlers  
  94.         #region Wizard generated sample code (commented)  
  95.         /*      public void OnMouseDown(object sender, System.Windows. Forms.MouseEventArgs  e)  
  96.         {  
  97.             // Record line in QTP.  
  98.             if(e.Button == System.Windows.Forms.MouseButtons.Left)  
  99.             {             
  100.                 RecordFunction( "CustomMouseDown", RecordingMode.RECORD_SEND_LINE, e.X, e.Y);  
  101.             }  
  102.         }  
  103. */  
  104.         #endregion  
  105.  
  106.         private void oControl_CommandClick(object sender, MouseEventArgs e)  
  107.         {                  
  108.  
  109.             C1.Win.C1Command.C1MainMenu oControl = (C1.Win.C1Command.C1MainMenu)SourceControl;  
  110.             string selectedMenu = "";  
  111.             for (int i = 0; i oControl.CommandLinks.Count; i++)  
  112.             {  
  113.                 if (((oControl.CommandLinks[i].Bounds.X + oControl. CommandLinks[i].Bounds.Width) >= e.X) & (e.X >= oControl.CommandLinks[i]. Bounds.X))  
  114.                 {  
  115.                     if (((oControl.CommandLinks[i].Bounds.Y + oControl. CommandLinks[i].Bounds.Height) >= e.Y) & (e.Y >= oControl.CommandLinks[i]. Bounds.Y))  
  116.                     {  
  117.                         selectedMenu = oControl.CommandLinks[i].Text;  
  118.                         break;  
  119.                     }  
  120.                 }  
  121.             }  
  122.             base.RecordFunction("C1MainMenu_Select", RecordingMode.RECORD_SEND_LINE, selectedMenu);  
  123.         }   
  124.  
  125.         #endregion  
  126.  
  127.         #region Replay interface implementation  
  128.         #region Wizard generated sample code (commented)  
  129.         /*      public void CustomMouseDown(int X, int Y)  
  130.         {  
  131.             MouseClick(X, Y, MOUSE_BUTTON.LEFT_MOUSE_BUTTON);  
  132.         }  
  133. */  
  134.         #endregion  
  135.  
  136.         public void C1MainMenu_Select(string text)   
  137.         {  
  138.             C1.Win.C1Command.C1MainMenu oControl = (C1.Win.C1Command.C1MainMenu)SourceControl;  
  139.             for (int i = 0; i oControl.CommandLinks.Count; i++)  
  140.             {  
  141.                 if (oControl.CommandLinks[i].Text == text)  
  142.                 {  
  143.                    
  144.                     System.Drawing.Rectangle oRect = oControl. CommandLinks[i].Bounds;  
  145.                     int x = oRect.X + oRect.Width / 2;  
  146.                     int y = oRect.Y + oRect.Height / 2;  
  147.                     //Click the middle of the button item   
  148.                     base.MouseClick(x, y, MOUSE_BUTTON.LEFT_MOUSE_BUTTON);  
  149.                       
  150.                     break;  
  151.                 }  
  152.             }  
  153.             base.ReplayReportStep("C1MainMenu_Select", EventStatus.EVENTSTATUS_GENERAL, text);  
  154.           }   
  155.         #endregion  
  156.     }  
  157. }  
原文地址:https://www.cnblogs.com/gdg87813/p/10956344.html