Working With Third-Party Tree View Controls

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

While testing tree view 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 tree view controls, but also for WPF (XAML) TreeView controls and Java Swing JTree controls. To work with these controls, TestComplete uses the WPFTreeView and JTree objects, respectively. These objects are very similar to the Win32TreeView object -- they have the ClickItem action that simulates a click on a tree view item, the ExpandItem action that expands an item, the wItems property that provides access to individual items, and so on. So, you can work with WPF and Swing tree view controls in the same way as with standard Win32 tree views (for more information, see Working With Tree View Controls).

The following example works with the XAMLPad application, which is a part of the Windows SDK. Before running the script, you should launch XAMLPad and replace the editor contents with the following code:

XAML

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
  <TreeView>
    <TreeViewItem Header="Item 1">
      <TreeViewItem Header="Item 1-1">
        <TreeViewItem Header="Item 1-1-1"/>
        <TreeViewItem Header="Item 1-1-2"/>
      </TreeViewItem>
      <TreeViewItem Header="Item 1-2"/>
    </TreeViewItem>
    <TreeViewItem Header="Item 2">
      <TreeViewItem Header="Item 2-1"/>
      <TreeViewItem Header="Item 2-2">
        <TreeViewItem Header="Item 2-2-1"/>
      </TreeViewItem>
    </TreeViewItem>
    <TreeViewItem Header="Item 3"/>
    <TreeViewItem Header="Item 4">
      <TreeViewItem Header="Item 4-1"/>
      <TreeViewItem Header="Item 4-2"/>
    </TreeViewItem>
    <TreeViewItem Header="Item 5"/>
  </TreeView>
</Page>

This script interacts with the tree view control using the WPFTreeView object’s actions. In addition, the GetSelectedItem function also uses “native” properties of the tree view control to get information about the selected tree view item.

JavaScript

function Main()
{
  var p, w, TreeView;

  // Obtain the XAMLPad process, window and the TreeView control
  p = Sys.Process("XAMLPad");
  w = p.WPFObject("HwndSource: Window", "XamlPad");
  TreeView = w.FindChild("ClrClassName", "TreeView", 9);

  // Expand, collapse and click some items
  TreeView.ClickItem("|Item 1|Item 1-1|Item 1-1-1");
  TreeView.DblClickItem("|Item 4");
  TreeView.ExpandItem("|Item 2|Item 2-2");
  TreeView.ClickItem("|Item 2|Item 2-2|Item 2-2-1");
  TreeView.CollapseItem("|Item 1|Item 1-1");

  // Post the selected item's path to the log
  Log.Message("Selected item: " + GetSelectedItem(TreeView));
}

function GetSelectedItem (WPFTreeView)
{
  var item, str;

  // Obtain the selected item
  item = WPFTreeView.SelectedItem;

  // Compose the item's full path
  str = "";
  while (!strictEqual(item, null))
  {
    str = "|" + item.Header.OleValue + str;
    item = item.ParentTreeViewItem;
  }
  return str;
}

JScript

function Main()
{
  var p, w, TreeView;

  // Obtain the XAMLPad process, window and the TreeView control
  p = Sys.Process("XAMLPad");
  w = p.WPFObject("HwndSource: Window", "XamlPad");
  TreeView = w.FindChild("ClrClassName", "TreeView", 9);

  // Expand, collapse and click some items
  TreeView.ClickItem("|Item 1|Item 1-1|Item 1-1-1");
  TreeView.DblClickItem("|Item 4");
  TreeView.ExpandItem("|Item 2|Item 2-2");
  TreeView.ClickItem("|Item 2|Item 2-2|Item 2-2-1");
  TreeView.CollapseItem("|Item 1|Item 1-1");

  // Post the selected item's path to the log
  Log.Message("Selected item: " + GetSelectedItem(TreeView));
}

function GetSelectedItem (WPFTreeView)
{
  var item, str;

  // Obtain the selected item
  item = WPFTreeView.SelectedItem;

  // Compose the item's full path
  str = "";
  while (item != null)
  {
    str = "|" + item.Header.OleValue + str;
    item = item.ParentTreeViewItem;
  }
  return str;
}

Python

def Main():

  # Obtain the XAMLPad process, window and the TreeView control
  p = Sys.Process("XAMLPad")
  w = p.WPFObject("HwndSource: Window", "XamlPad")
  TreeView = w.FindChild("ClrClassName", "TreeView", 9)

  # Expand, collapse and click some items
  TreeView.ClickItem("|Item 1|Item 1-1|Item 1-1-1")
  TreeView.DblClickItem("|Item 4")
  TreeView.ExpandItem("|Item 2|Item 2-2")
  TreeView.ClickItem("|Item 2|Item 2-2|Item 2-2-1")
  TreeView.CollapseItem("|Item 1|Item 1-1")

  # Post the selected item's path to the log
  Log.Message("Selected item: " + GetSelectedItem(TreeView))

def GetSelectedItem (WPFTreeView):
  
  # Obtain the selected item
  item = WPFTreeView.SelectedItem

  # Compose the item's full path
  str = ""
  while (item != None):
    str = "|" + item.Header.OleValue + str
    item = item.ParentTreeViewItem
  return str

VBScript

Sub Main
  Dim p, w, TreeView

  ' Obtain the XAMLPad process, window and the TreeView control
  Set p = Sys.Process("XAMLPad")
  Set w = p.WPFObject("HwndSource: Window", "XamlPad")
  Set TreeView = w.FindChild("ClrClassName", "TreeView", 9)

  ' Expand, collapse and click some items
  TreeView.ClickItem("|Item 1|Item 1-1|Item 1-1-1")
  TreeView.DblClickItem("|Item 4")
  TreeView.ExpandItem("|Item 2|Item 2-2")
  TreeView.ClickItem("|Item 2|Item 2-2|Item 2-2-1")
  TreeView.CollapseItem("|Item 1|Item 1-1")
  ' Post the selected item's path to the log

  Log.Message("Selected item: " & GetSelectedItem(TreeView))
End Sub

Function GetSelectedItem (WPFTreeView)
  Dim item, str

  ' Obtain the selected item
  Set item = WPFTreeView.SelectedItem

  ' Compose the item's full path
  str = ""
  While Not (item Is Nothing)
    str = "|" & item.Header.OleValue & str
    Set item = item.ParentTreeViewItem
  Wend
  GetSelectedItem = str
End Function

DelphiScript

function GetSelectedItem (WPFTreeView);
var item, str : OleVariant;
begin
  // Obtain the selected item
  item := WPFTreeView.SelectedItem;

  // Compose the item's full path
  str := '';
  while item <> nil do
  begin
    str := '|' + item.Header.OleValue + str;
    item := item.ParentTreeViewItem;
  end;
  Result := str;
end;

procedure Main;
var p, w, TreeView : OleVariant;
begin
  // Obtain the XAMLPad process, window and the TreeView control
  p := Sys.Process('XAMLPad');
  w := p.WPFObject('HwndSource: Window', 'XamlPad');
  TreeView := w.FindChild('ClrClassName', 'TreeView', 9);

  // Expand, collapse and click some items
  TreeView.ClickItem('|Item 1|Item 1-1|Item 1-1-1');
  TreeView.DblClickItem('|Item 4');
  TreeView.ExpandItem('|Item 2|Item 2-2');
  TreeView.ClickItem('|Item 2|Item 2-2|Item 2-2-1');
  TreeView.CollapseItem('|Item 1|Item 1-1');

  // Post the selected item's path to the log
  Log.Message('Selected item: ' + GetSelectedItem(TreeView));
end;

C++Script, C#Script

function Main()
{
  var p, w, TreeView;

  // Obtain the XAMLPad process, window and the TreeView control
  p = Sys["Process"]("XAMLPad");
  w = p["WPFObject"]("HwndSource: Window", "XamlPad");
  TreeView = w["FindChild"]("ClrClassName", "TreeView", 9);

  // Expand, collapse and click some items
  TreeView["ClickItem"]("|Item 1|Item 1-1|Item 1-1-1");
  TreeView["DblClickItem"]("|Item 4");
  TreeView["ExpandItem"]("|Item 2|Item 2-2");
  TreeView["ClickItem"]("|Item 2|Item 2-2|Item 2-2-1");
  TreeView["CollapseItem"]("|Item 1|Item 1-1");

  // Post the selected item's path to the log
  Log["Message"]("Selected item: " + GetSelectedItem(TreeView));
}

function GetSelectedItem (WPFTreeView)
{
  var item, str;

  // Obtain the selected item
  item = WPFTreeView["SelectedItem"];

  // Compose the item's full path
  str = "";
  while (item != null)
  {
    str = "|" + item["Header"]["OleValue"] + str;
    item = item["ParentTreeViewItem"];
  }
  return str;
}

If your tested application uses custom tree view components, TestComplete may not be able to recognize them. 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 tree views:

  • You can navigate in the tree view and select its items from the keyboard. To simulate keypresses, use the Keys action applied to the tree view control or the Sys.Keys method. For more information on this approach, see Selecting Tree View Items.

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

  • If your application is Open, you can work with tree view controls using their internal properties and methods. By exploring the tree view control in the Object Browser, you can find methods or properties that can be used to select tree view items, obtain their data, 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 the tested application supports Microsoft Active Accessibility (MSAA), you use the Microsoft Active Accessibility Support plugin to expose information about the tested application’s controls. For more information, see Using Microsoft Active Accessibility.

  • You can take advantage of the UI Automation technology to expose information about the tested application’s controls. See Using Microsoft UI Automation Technology.

  • To retrieve labels of tree view items, you can capture the tree view image and use the OCR feature. OCR also lets you determine the coordinates of a specific item within the tree view by the item’s label.

For more information about retrieving data stored in custom controls, see Interacting With Non-Compatible Application Objects.

See Also

Working With Tree View Controls
Object Mapping
Interacting With Non-Compatible Application Objects

Highlight search results