SandBar Toolbar Example
This example demonstrates how to implement .NET Add-in Extensibility for the Divelements Limited TD.SandBar.Toolbar control.
You can view the full source code of the final ToolBarSrv.cs class implementation in Understanding the ToolBarSrv.cs File.
A complete support set for the SandBar control, implemented both in C# and in Visual Basic, is located in <.NET_Add-in_Extensibility_SDK_installdir>\samples\WinFormsExtSample. You can use the files in this sample as an additional reference when performing this tutorial. For more information, see Using the .NET Add-in Extensibility Samples.
The Toolbar control appears as follows:
The Toolbar control is comprised of a variety of objects, such as:
ButtonItem objects, which represent buttons in the toolbar. ButtonItem objects contain images and no text. Each ButtonItem object has a unique tooltip.
DropDownMenuItem objects, which represent drop-down menus in the toolbar.
Both the ButtonItem object and the DropDownMenuItem object are derived from the ToolbarItemBase object.
When you implement a Custom Server for a custom control, you want UFT One to support recording and running the user's actions on the custom controls. When recording the test, your Custom Server listens to the control's events and handles the events to perform certain actions to add steps to the UFT One GUI test. When running the test, you simulate (replay) the same actions the user performed on that control.
For example, suppose you want to implement a user pressing a button on a custom toolbar. Before doing so, you must understand the toolbar control, its properties and methods, and understand how you can use them to implement the Custom Server.
Following are some of the SandBar ToolBar object's properties and events (methods are not visible in this image) as displayed in the Object Browser in Visual Studio:
As you can see in the image above, the ToolBar object has a property called Items that retrieves the collection of ToolbarItemBase objects assigned to the ToolBar control. You can also see that the ToolBar control has an event called ButtonClick. Your Custom Server can listen to the ButtonClick event to know when a button in the toolbar is clicked. However, this event does not indicate which specific button in the toolbar is clicked.
Now expand the ButtonItem object and review its properties, methods, and events:
As shown in the image above, the ButtonItem object is derived from the ToolbarItemBase object. You can see that the ToolbarItemBase object contains a ToolTipText property, but does not contain a Click event or method.
When you look at the custom toolbar object, the following possible implementation issues arise:
Solution: All of the ToolBar object's events are ToolBarItemEventArgs events that are derived from the EventArgs object:
The Item property indicates which toolbar item (button) raised the event. You can use that toolbar item's unique ToolTipText property to recognize which button was clicked and add that to the UFT One GUI test.
To do this, enter the following code in the Record events handlers section of the ToolBarSrv.cs file:
#region Record events handlers private void oControl_ButtonClick(object sender, TD.SandBar.ToolBarItemEventArgs e) { TD.SandBar.ToolBar oControl = (TD.SandBar.ToolBar)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.Item.ToolTipText); } #endregion
Now, each time you record a click on a button in the toolbar, a step is added to the test for the toolbar test object with the ClickButton method and the tooltip text of the button as its argument. For example:
SwfToolbar("MySandBar").ClickButton "Spelling and Grammar"
Solution: The ToolbarItemBase object has a property called ButtonBounds:
You can loop through all of the ToolbarItemBase objects until you find a ToolbarItemBase objects that has the same tooltip text as the ButtonItem object, find that ToolbarItemBase object's rectangle boundaries, calculate the middle of its boundaries, and click that point.
To do this, enter the following code in the Replay interface implementation section of the ToolBarSrv.cs file:
#region Replay interface implementation public void ClickButton(string text) { TD.SandBar.ToolBar oControl = (TD.SandBar.ToolBar)SourceControl; //Find the correct item in the toolbar according to // its tooltip text. for(int i=0; i<oControl.Items.Count; i++) { //Found the correct ButtonItem if(oControl.Items[i].ToolTipText == text) { //Retrieve the rectangle of the button's boundaries // and locate its center System.Drawing.Rectangle oRect = oControl.Items[i].ButtonBounds; 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; } } //Add the step to the report base.ReplayReportStep("ClickButton", EventStatus.EVENTSTATUS_GENERAL, text); } #endregion