使用QTP的.NET外掛擴充套件技術測試ComponentOne的ToolBar控制元件

TIB發表於2010-03-13

對於ComponentOneToolBar控制元件,我們可以採用QTP.NET外掛擴充套件技術來處理。下面是在VS.NET2005中編寫的外掛擴充套件程式碼:

using System;

using Mercury.QTP.CustomServer;

using System.Windows.Forms;

using QuickTestCustomServer_C1ToolBar;

using C1.Win.C1Command;

 

namespace QuickTestCustomServer_C1ToolBar

{

    [ReplayInterface]

    public interface IC1ToolBarReplay

    {

        #region Wizard generated sample code (commented)

        //          void  CustomMouseDown(int X, int Y);

        #endregion

        void C1ToolBar_Click(string text);

    }

    /// <summary>

    /// Summary description for C1ToolBar.

    /// </summary>

    public class C1ToolBar :

        CustomServerBase,

        IC1ToolBarReplay

    {

        // You shouldn't call Base class methods/properties at the constructor

        // since its services are not initialized yet.

        public C1ToolBar()

        {

            //

            // 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

                       

            //Delegate e = new C1.Win.C1Command.ClickEventHandler(this.oControl_Click);

            //AddHandler("Click", e);

 

            C1.Win.C1Command.C1ToolBar oControl = (C1.Win.C1Command.C1ToolBar)SourceControl;

 

            for (int i = 0; i < oControl.CommandLinks.Count; i++)

            {

                oControl.CommandLinks[i].Command.Click += new C1.Win.C1Command.ClickEventHandler(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.C1ToolBar oControl = (C1.Win.C1Command.C1ToolBar)SourceControl;

            //oControl.Click -= new

            //C1.Win.C1Command.ClickEventHandler(oControl_Click);

        }

 

        #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, C1.Win.C1Command.ClickEventArgs e)

        {

            C1.Win.C1Command.C1ToolBar oControl = (C1.Win.C1Command.C1ToolBar)SourceControl;

            //oControl.CommandLinks[0].State.ToString()

            base.RecordFunction("C1ToolBar_Click", RecordingMode.RECORD_SEND_LINE, e.CallerLink.Text);

            //C1.Win.C1Command.C1ToolBar oControl = (C1.Win.C1Command.C1ToolBar)SourceControl;

            //// Add a step in the test for the test object with the ClickButton method

            //// and the tooltip text as an argument

            //base.RecordFunction("ClickButton", RecordingMode.RECORD_SEND_LINE, e.CallerLink.Text);

        }

 

        #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 C1ToolBar_Click(string text)

        {

            //MessageBox.Show("Test!");

            C1.Win.C1Command.C1ToolBar oControl = (C1.Win.C1Command.C1ToolBar)SourceControl;

            //Find the correct item in the toolbar according to its tooltip text.

            for (int i = 0; i < oControl.CommandLinks.Count; i++)

            {

                //Found the correct ButtonItem

                if (oControl.CommandLinks[i].Text == text)

                {

                    //base.ReplayReportStep("Find",EventStatus.EVENTSTATUS_GENERAL,text);

                    if (oControl.CommandLinks[i].Command.IsParent == true)

                    {

                        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);

                    }

                    else

                    {

                    // Retrieve the rectangle of the button's boundaries and

                    // locate its center

                   

                    //System.Drawing.Rectangle oRect = oControl.CommandLinks[i].CheckRect;

                    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("C1ToolBar_Click", EventStatus.EVENTSTATUS_GENERAL, text);

          }

       

        #endregion

    }

}

 

 

把如下XML程式碼插入到QTP安裝目錄中的dat目錄下的SwfConfig.xml檔案中(注意修改DLL的檔案路徑):

<!-- Merge this XML content into file "<QuickTest Professional>/dat/SwfConfig.xml". -->

  <Control Type="C1.Win.C1Command.C1ToolBar" >

    <CustomRecord>

      <Component>

             <Context>AUT</Context>

        <DllName>D:/QTP_dotNET/ComponentOne/ToolBar/QuickTestCustomServer_C1ToolBar/QuickTestCustomServer_C1ToolBar/bin/QuickTestCustomServer_C1ToolBar.dll</DllName>

        <TypeName>QuickTestCustomServer_C1ToolBar.C1ToolBar</TypeName>

      </Component>

    </CustomRecord>

    <CustomReplay>

      <Component>

             <Context>AUT</Context>

        <DllName>D:/QTP_dotNET/ComponentOne/ToolBar/QuickTestCustomServer_C1ToolBar/QuickTestCustomServer_C1ToolBar/bin/QuickTestCustomServer_C1ToolBar.dll</DllName>

        <TypeName>QuickTestCustomServer_C1ToolBar.C1ToolBar</TypeName>

      </Component>

    </CustomReplay>

<!--<Settings>

       <Parameter Name="sample name">sample value</Parameter>

    </Settings> -->

  </Control>

 

 

這樣,啟動QTP之後,就可以直接使用QTP的錄製功能來錄製和產生ComponentOneToolBar控制元件指令碼了,例如:

SwfWindow("New document").Activate

SwfWindow("New document").SwfObject("SwfObject").C1ToolBar_Click "&New"

SwfWindow("New document").SwfObject("SwfObject").C1ToolBar_Click "E&xit"

 

相關文章