Working With Third-Party Toolbars

Applies to TestComplete 14.71, last modified on April 22, 2021

While testing toolbar controls, you can use specific properties and methods of the corresponding program object to perform certain actions and obtain data stored in controls. You can call these methods and properties from your keyword tests, as well as from scripts. This topic describes how to work with the needed properties and methods from your scripts. However, when testing a control from your keyword test, you can use the same methods and properties calling them from keyword test operations. For more information, see Keyword Tests Basic Operations.

TestComplete provides full-scale support not only for standard Win32 toolbar controls, but also for .NET ToolStrip and WPF ToolBar controls. To work with these toolbar controls and their buttons, it uses the Win32ToolBar, StripToolBar and WPFToolBar, respectively.

In addition, TestComplete can recognize individual toolbar buttons in Java Swing applications. To work with Swing buttons, it uses the JButton object methods and properties. For instance, the JButton.ClickButton action lets you simulate button clicks, the Enabled property -- checks if the button is enabled, the Picture method -- obtains the button image, and so on. The following script demonstrates how you can click toolbar buttons in Java Swing applications:

Note: This sample script works with the Java Swing OrdersDemo sample that resides in the following folder:

<TestComplete Samples>\Desktop\Orders\Swing

If you do not have the sample, download the TestComplete Samples installation package from the https://support.smartbear.com/downloads/testcomplete/samples/ page of our website and run it.

To compile this example, run compile.bat kept in the example’s folder. To run the example, use run.bat in this folder.

JavaScript, JScript

function Main()
{
  var p, w, w1, ToolBar;

  // Obtain the Java process, the application window and toolbar
  p = Sys.Process("java");
  w = p.SwingObject("JfrmMain", "Orders - Untitled", 0, 1);
  ToolBar = w.SwingObject("JRootPane", "", 0).SwingObject("JLayeredPane", "", 0).SwingObject("JPanel", "", 0).SwingObject("JToolBar", "", 0);

  // Click the "New Order" button on the toolbar
  ToolBar.SwingObject("JButton", "", 4).ClickButton();

  // Create a new empty record
  w1 = p.SwingObject("JdlgOrders", "New order", -1, 1);
  w1.SwingObject("JRootPane", "", 0).SwingObject("JLayeredPane", "", 0).SwingObject("JPanel", "", 0).SwingObject("JPanel", "", 0).SwingObject("JButton", "Ok", 1).ClickButton();
}

Python

def Main():

  # Obtain the Java process, the application window and toolbar
  p = Sys.Process("java")
  w = p.SwingObject("JfrmMain", "Orders - Untitled", 0, 1)
  ToolBar = w.SwingObject("JRootPane", "", 0).SwingObject("JLayeredPane", "", 0).SwingObject("JPanel", "", 0).SwingObject("JToolBar", "", 0)

  # Click the "New Order" button on the toolbar
  ToolBar.SwingObject("JButton", "", 4).ClickButton()

  # Create a new empty record
  w1 = p.SwingObject("JdlgOrders", "New order", -1, 1)
  w1.SwingObject("JRootPane", "", 0).SwingObject("JLayeredPane", "", 0).SwingObject("JPanel", "", 0).SwingObject("JPanel", "", 0).SwingObject("JButton", "Ok", 1).ClickButton()

VBScript

Sub Main
  Dim p, w, w1, ToolBar

  ' Obtain the Java process, the application window and toolbar
  Set p = Sys.Process("java")
  Set w = p.SwingObject("JfrmMain", "Orders - Untitled", 0, 1)
  Set ToolBar = w.SwingObject("JRootPane", "", 0).SwingObject("JLayeredPane", "", 0).SwingObject("JPanel", "", 0).SwingObject("JToolBar", "", 0)

  ' Click the "New Order" button on the toolbar
  ToolBar.SwingObject("JButton", "", 4).ClickButton

  ' Create a new empty record
  Set w1 = p.SwingObject("JdlgOrders", "New order", -1, 1)
  w1.SwingObject("JRootPane", "", 0).SwingObject("JLayeredPane", "", 0).SwingObject("JPanel", "", 0).SwingObject("JPanel", "", 0).SwingObject("JButton", "Ok", 1).ClickButton
End Sub

DelphiScript

function Main;
var p, w, w1, ToolBar : OleVariant;
begin
  // Obtain the Java process, the application window and toolbar
  p := Sys.Process('java');
  w := p.SwingObject('JfrmMain', 'Orders - Untitled', 0, 1);
  ToolBar := w.SwingObject('JRootPane', '', 0).SwingObject('JLayeredPane', '', 0).SwingObject('JPanel', '', 0).SwingObject('JToolBar', '', 0);

  // Click the 'New Order' button on the toolbar
  ToolBar.SwingObject('JButton', '', 4).ClickButton;

  // Create a new empty record
  w1 := p.SwingObject('JdlgOrders', 'New order', -1, 1);
  w1.SwingObject('JRootPane', '', 0).SwingObject('JLayeredPane', '', 0).SwingObject('JPanel', '', 0).SwingObject('JPanel', '', 0).SwingObject('JButton', 'Ok', 1).ClickButton;
end;

C++Script, C#Script

function Main()
{
  var p, w, w1, ToolBar;

  // Obtain the Java process, the application window and toolbar
  p = Sys["Process"]("java");
  w = p["SwingObject"]("JfrmMain", "Orders - Untitled", 0, 1);
  ToolBar = w["SwingObject"]("JRootPane", "", 0)["SwingObject"]("JLayeredPane", "", 0)["SwingObject"]("JPanel", "", 0)["SwingObject"]("JToolBar", "", 0);

  // Click the "New Order" button on the toolbar
  ToolBar["SwingObject"]("JButton", "", 4)["ClickButton"]();

  // Create a new empty record
  w1 = p["SwingObject"]("JdlgOrders", "New order", -1, 1);
  w1["SwingObject"]("JRootPane", "", 0)["SwingObject"]("JLayeredPane", "", 0)["SwingObject"]("JPanel", "", 0)["SwingObject"]("JPanel", "", 0)["SwingObject"]("JButton", "Ok", 1)["ClickButton"]();
}

If your tested application uses custom toolbar components, TestComplete may not be able to recognize it. You can work with custom controls in black-box mode only, that is, you can simulate mouse clicks, record and play back low-level procedures, and so on. However, there are more effective approaches for working with custom toolbars:

  • You can select toolbar buttons using their access keys and shortcuts. To simulate keypresses, use the Keys action applied to the toolbar control or the Sys.Keys method. For more information on this approach, see Selecting Toolbar Buttons.

  • You can associate the class name of your custom toolbar control with the Toolbar type. In this case, TestComplete will try to handle this control as a standard Win32 toolbar and work with it via Win32ToolBar object. To learn how you can set this association, see Object Mapping for more information. Note, however, that this approach may not work if the toolbar control in your application does not respond to standard Windows messages or processes them improperly.

  • If your tested application is Open, you can work with toolbars using their internal properties and methods. By exploring the toolbar control in the Object Browser, you can find methods or properties that can be used to select toolbar buttons, check their type and state, and so on. You can also ask the tested application’s developers to learn which properties and methods can be used for this purpose.

  • If your custom toolbar control supports Microsoft Active Accessibility (MSAA), you can use the Microsoft Active Accessibility Support plugin to access toolbar buttons. To enable the MSAA engine and expose information about the toolbar control, you should specify the toolbar’s window class name (the WndClass property) in the project’s MSAA Options. For example, if you enable the “*” item, the MSAA engine will expose methods and properties of any existing window. For more information about testing applications that support Active Accessibility, see Using Microsoft Active Accessibility.

The example below demonstrates how you can work with a custom toolbar using the TestComplete MSAA subsystem. It enters formatted text into a Microsoft Word 2003 document, then copies it and pastes it in the end of the document.

Example

View prerequisites

JavaScript, JScript

function Test()
{
  var p, edit, toolbar, tbStandard, tbFormatting;

  // Obtain the Word process, edit area and toolbars
  p = Sys.Process("WINWORD");
  edit = p.Form("*").Window("_WwF").Panel("*").Panel("Microsoft Word Document");
  toolbar = p.Form("*").Panel("MsoDockTop");
  tbStandard = toolbar.ToolBar("Standard");
  tbFormatting = toolbar.ToolBar("Formatting");

  // Enter text using different styles
  tbFormatting.Button("Bold").Click();
  edit.Keys("Hello, ");
  tbFormatting.Button("Italic").Click();
  edit.Keys("world!!");

  // Copy and paste the text
  edit.Keys("![Home]");
  tbStandard.Button("Copy").Click();
  edit.Keys("^[End][Enter]");
  tbStandard.Button("Paste").Click();
}

Python

def Test():

  # Obtain the Word process, edit area and toolbars
  p = Sys.Process("WINWORD")
  edit = p.Form("*").Window("_WwF").Panel("*").Panel("Microsoft Word Document")
  toolbar = p.Form("*").Panel("MsoDockTop")
  tbStandard = toolbar.ToolBar("Standard")
  tbFormatting = toolbar.ToolBar("Formatting")

  # Enter text using different styles
  tbFormatting.Button("Bold").Click()
  edit.Keys("Hello, ")
  tbFormatting.Button("Italic").Click()
  edit.Keys("world!!")

  # Copy and paste the text
  edit.Keys("![Home]")
  tbStandard.Button("Copy").Click()
  edit.Keys("^[End][Enter]")
  tbStandard.Button("Paste").Click()

VBScript

Sub Test
  Dim p, edit, toolbar, tbStandard, tbFormatting

  ' Obtain the Word process, edit area and toolbars
  Set p = Sys.Process("WINWORD")
  Set edit = p.Form("*").Window("_WwF").Panel("*").Panel("Microsoft Word Document")
  Set toolbar = p.Form("*").Panel("MsoDockTop")
  Set tbStandard = toolbar.ToolBar("Standard")
  Set tbFormatting = toolbar.ToolBar("Formatting")

  ' Enter text using different styles
  tbFormatting.Button("Bold").Click
  edit.Keys("Hello, ")
  tbFormatting.Button("Italic").Click
  edit.Keys("world!!")

  ' Copy and paste the text
  edit.Keys("![Home]")
  tbStandard.Button("Copy").Click
  edit.Keys("^[End][Enter]")
  tbStandard.Button("Paste").Click
End Sub

DelphiScript

procedure Test;
var p, edit, toolbar, tbStandard, tbFormatting;
begin
  // Obtain the Word process, edit area and toolbars
  p := Sys.Process('WINWORD');
  edit := p.Form('*').Window('_WwF').Panel('*').Panel('Microsoft Word Document');
  toolbar := p.Form('*').Panel('MsoDockTop');
  tbStandard := toolbar.ToolBar('Standard');
  tbFormatting := toolbar.ToolBar('Formatting');

  // Enter text using different styles
  tbFormatting.Button('Bold').Click;
  edit.Keys('Hello, ');
  tbFormatting.Button('Italic').Click;
  edit.Keys('world!!');

  // Copy and paste the text
  edit.Keys('![Home]');
  tbStandard.Button('Copy').Click;
  edit.Keys('^[End][Enter]');
  tbStandard.Button('Paste').Click;
end;

C++Script, C#Script

function Test()
{
  var p, edit, toolbar, tbStandard, tbFormatting;

  // Obtain the Word process, edit area and toolbars
  p = Sys["Process"]("WINWORD");
  edit = p["Form"]("*")["Window"]("_WwF")["Panel"]("*")["Panel"]("Microsoft Word Document");
  toolbar = p["Form"]("*")["Panel"]("MsoDockTop");
  tbStandard = toolbar["ToolBar"]("Standard");
  tbFormatting = toolbar["ToolBar"]("Formatting");

  // Enter text using different styles
  tbFormatting["Button"]("Bold")["Click"]();
  edit["Keys"]("Hello, ");
  tbFormatting["Button"]("Italic")["Click"]();
  edit["Keys"]("world!!");

  // Copy and paste the text
  edit["Keys"]("![Home]");
  tbStandard["Button"]("Copy")["Click"]();
  edit["Keys"]("^[End][Enter]");
  tbStandard["Button"]("Paste")["Click"]();
}

See Also

Working With Toolbars
Using Microsoft Active Accessibility

Highlight search results