18.3.4 测试ComponentOne的MainMenu控件(1)
对于如图18.10所示的ComponentOne的MainMenu控件,同样采用QTP的.NET插件扩展技术来处理。下面是在Visual Studio 2005中编写的插件扩展代码:
![]() |
图18.10 ComponentOne的MainMenu控件 |
- using System;
- using Mercury.QTP.CustomServer;
- using System.Windows.Forms;
- namespace QuickTestCustomServer_C1MainMenu
- {
- [ReplayInterface]
- public interface IC1MainMenuReplay
- {
- #region Wizard generated sample code (commented)
- // void CustomMouseDown(int X, int Y);
- #endregion
- void C1MainMenu_Select(string Memu);
- }
- /// <summary>
- /// Summary description for C1MainMenu.
- /// </summary>
- public class C1MainMenu :
- CustomServerBase,
- IC1MainMenuReplay
- {
- // You shouldn't call Base class methods/properties at the constructor
- // since its services are not initialized yet.
- public C1MainMenu()
- {
- //
- // TODO: Add constructor logic here
- //
- }
- #region IRecord override Methods
- #region Wizard generated sample code (commented)
- /* /// <summary>
- /// To change Window messages filter implement this method.
- /// The default implementation is to get only Control's window messages.
- /// </summary>
- public override WND_MsgFilter GetWndMessageFilter()
- {
- return WND_MsgFilter.WND_MSGS;
- }
- /// <summary>
- /// To catch window messages you should implement this method.
- /// Please note: This method is called just in case the CustomServer is running
- /// under QuickTest process.
- /// </summary>
- public override RecordStatus OnMessage(ref Message tMsg)
- {
- // TODO: Add OnMessage implementation.
- return RecordStatus.RECORD_HANDLED;
- }
- */
- #endregion
- /// <summary>
- /// In case you extend Record process, you should add your Events handlers
- /// in order to listen to Control's Events.
- /// </summary>
- public override void InitEventListener()
- {
- #region Wizard generated sample code (commented)
- /* // Notice, You can add as many handlers as you need.
- // Adding OnMouseDown handler.
- Delegate e = new System.Windows.Forms.MouseEventHandler(this.OnMouseDown);
- // Adds an event handler as the first handler of the event.
- // The first argument is the name of the event for which to listen.
- // You must provide an event that the control supports.
- // Use the .NET Spy to obtain the list of events supported by the control.
- // The second argument is the event handler delegate.
- AddHandler("MouseDown", e);
- */
- #endregion
- C1.Win.C1Command.C1MainMenu oControl = (C1.Win.C1Command. C1MainMenu)SourceControl;
- oControl.MouseDown += new MouseEventHandler(this.oControl_ CommandClick);
- }
- /// <summary>
- /// At the end of the Record process this method is called by QuickTest to release
- /// all the handlers the user added in InitEventListener method.
- /// Please note: Handlers added via QuickTest methods are released by QuickTest infrastructure.
- /// </summary>
- public override void ReleaseEventListener()
- {
- //C1.Win.C1Command.C1MainMenu oControl = (C1.Win.C1Command. C1MainMenu)SourceControl;
- //oControl.Click -= new C1.Win.C1Command.ClickEventHandler (this.oControl_CommandClick);
- }
- #endregion
- #region Record events handlers
- #region Wizard generated sample code (commented)
- /* public void OnMouseDown(object sender, System.Windows. Forms.MouseEventArgs e)
- {
- // Record line in QTP.
- if(e.Button == System.Windows.Forms.MouseButtons.Left)
- {
- RecordFunction( "CustomMouseDown", RecordingMode.RECORD_SEND_LINE, e.X, e.Y);
- }
- }
- */
- #endregion
- private void oControl_CommandClick(object sender, MouseEventArgs e)
- {
- C1.Win.C1Command.C1MainMenu oControl = (C1.Win.C1Command.C1MainMenu)SourceControl;
- string selectedMenu = "";
- for (int i = 0; i < oControl.CommandLinks.Count; i++)
- {
- if (((oControl.CommandLinks[i].Bounds.X + oControl. CommandLinks[i].Bounds.Width) >= e.X) & (e.X >= oControl.CommandLinks[i]. Bounds.X))
- {
- if (((oControl.CommandLinks[i].Bounds.Y + oControl. CommandLinks[i].Bounds.Height) >= e.Y) & (e.Y >= oControl.CommandLinks[i]. Bounds.Y))
- {
- selectedMenu = oControl.CommandLinks[i].Text;
- break;
- }
- }
- }
- base.RecordFunction("C1MainMenu_Select", RecordingMode.RECORD_SEND_LINE, selectedMenu);
- }
- #endregion
- #region Replay interface implementation
- #region Wizard generated sample code (commented)
- /* public void CustomMouseDown(int X, int Y)
- {
- MouseClick(X, Y, MOUSE_BUTTON.LEFT_MOUSE_BUTTON);
- }
- */
- #endregion
- public void C1MainMenu_Select(string text)
- {
- C1.Win.C1Command.C1MainMenu oControl = (C1.Win.C1Command.C1MainMenu)SourceControl;
- for (int i = 0; i < oControl.CommandLinks.Count; i++)
- {
- if (oControl.CommandLinks[i].Text == text)
- {
- System.Drawing.Rectangle oRect = oControl. CommandLinks[i].Bounds;
- int x = oRect.X + oRect.Width / 2;
- int y = oRect.Y + oRect.Height / 2;
- //Click the middle of the button item
- base.MouseClick(x, y, MOUSE_BUTTON.LEFT_MOUSE_BUTTON);
- break;
- }
- }
- base.ReplayReportStep("C1MainMenu_Select", EventStatus.EVENTSTATUS_GENERAL, text);
- }
- #endregion
- }
- }