使用QTP的.NET插件扩展技术测试ComponentOne的ToolBar控件

xiaoxiao2021-03-01  30

对于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"

相关资源:微信小程序源码-合集1.rar
转载请注明原文地址: https://www.6miu.com/read-3450172.html

最新回复(0)