Checking Tree View Items' State in Desktop Windows Applications

Applies to TestComplete 15.70, last modified on December 17, 2024

In your tests, you may need to check the current state of specific tree view items. For instance, you may need to verify that an item is expanded before capturing a picture of this item and its child items.

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.

To check the state of a particular tree view item, you can use various properties of the Win32TreeViewItem object that corresponds to this item. The following sections explains how to perform some common checks. Note that you can write the verification code manually, or use property checkpoints.

Checking if the Item is Selected

A Win32TreeViewItem object contains the Selected property, which lets you check if the corresponding tree view item is selected or not:

JavaScript, JScript

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

  // Obtain the TreeView control
  p = Sys.Process("TreeViewSample");
  TreeView = p.Window("TForm1", "*").Window("TTreeView");

  // Check if the "SubItem 1-2" item is selected
  Item = TreeView.wItems.Item("Item 1").Items.Item("Item 1-2");
  if (Item.Selected)
    Log.Message("The '|RootItem 1|SubItem 1-2' item is selected.")
  else
    Log.Message("The '|RootItem 1|SubItem 1-2' item is not selected.");
}

Python

def Main ():

  # Obtain the TreeView control
  p = Sys.Process("TreeViewSample")
  TreeView = p.Window("TForm1", "*").Window("TTreeView")

  # Check if the "SubItem 1-2" item is selected
  Item = TreeView.wItems.Item("Item 1").Items.Item("Item 1-2")
  if Item.Selected:
    Log.Message("The '|RootItem 1|SubItem 1-2' item is selected.")
  else:
    Log.Message("The '|RootItem 1|SubItem 1-2' item is not selected.")

VBScript

Sub Main
  Dim p, TreeView, Item

  ' Obtain the TreeView control
  Set p = Sys.Process("TreeViewSample")
  Set TreeView = p.Window("TForm1", "*").Window("TTreeView")

  ' Check if the "SubItem 1-2" item is selected
  Set Item = TreeView.wItems.Item("Item 1").Items.Item("Item 1-2")
  If Item.Selected Then
    Log.Message("The '|RootItem 1|SubItem 1-2' item is selected.")
  Else
    Log.Message("The '|RootItem 1|SubItem 1-2' item is not selected.")
  End If
End Sub

DelphiScript

procedure Main;
var p, TreeView, Item : OleVariant;
begin
  // Obtain the TreeView control
  p := Sys.Process('TreeViewSample');
  TreeView := p.Window('TForm1', '*').Window('TTreeView');

  // Check if the 'SubItem 1-2' item is selected
  Item := TreeView.wItems.Item['Item 1'].Items.Item['Item 1-2'];
  if Item.Selected then
    Log.Message('The ''|RootItem 1|SubItem 1-2'' item is selected.')
  else
    Log.Message('The ''|RootItem 1|SubItem 1-2'' item is not selected.');
end;

C++Script, C#Script

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

  // Obtain the TreeView control
  p = Sys["Process"]("TreeViewSample");
  TreeView = p["Window"]("TForm1", "*")["Window"]("TTreeView");

  // Check if the "SubItem 1-2" item is selected
  Item = TreeView["wItems"]["Item"]("Item 1")["Items"]["Item"]("Item 1-2");
  if (Item["Selected"])
    Log["Message"]("The '|RootItem 1|SubItem 1-2' item is selected.")
  else
    Log["Message"]("The '|RootItem 1|SubItem 1-2' item is not selected.");
}

In addition, the selected item’s path is stored in the tree view’s wSelection property. So, you can also determine whether a particular item is selected by comparing its “full path” with the wSelection property value:

JavaScript, JScript

function Main ()
{
  var p, TreeView;

  // Obtain the TreeView control
  p = Sys.Process("TreeViewSample");
  TreeView = p.Window("TForm1", "*").Window("TTreeView");

  // Check if the "SubItem 1-2" item is selected
  if (TreeView.wSelection == "|RootItem 1|SubItem 1-2")
    Log.Message("The '|RootItem 1|SubItem 1-2' item is selected.")
  else
    Log.Message("The '|RootItem 1|SubItem 1-2' item is not selected.");
}

Python

def Main ():
  
  # Obtain the TreeView control
  p = Sys.Process("TreeViewSample")
  TreeView = p.Window("TForm1", "*").Window("TTreeView")

  # Check if the "SubItem 1-2" item is selected
  if (TreeView.wSelection == "|RootItem 1|SubItem 1-2"):
    Log.Message("The '|RootItem 1|SubItem 1-2' item is selected.")
  else:
    Log.Message("The '|RootItem 1|SubItem 1-2' item is not selected.")

VBScript

Sub Main
  Dim p, TreeView

  ' Obtain the TreeView control
  Set p = Sys.Process("TreeViewSample")
  Set TreeView = p.Window("TForm1", "*").Window("TTreeView")

  ' Check if the "SubItem 1-2" item is selected
  If TreeView.wSelection = "|RootItem 1|SubItem 1-2" Then
    Log.Message("The '|RootItem 1|SubItem 1-2' item is selected.")
  Else
    Log.Message("The '|RootItem 1|SubItem 1-2' item is not selected.")
  End If
End Sub

DelphiScript

procedure Main;
var p, TreeView : OleVariant;
begin
  // Obtain the TreeView control
  p := Sys.Process('TreeViewSample');
  TreeView := p.Window('TForm1', '*').Window('TTreeView');

  // Check if the 'SubItem 1-2' item is selected
  if TreeView.wSelection = '|RootItem 1|SubItem 1-2' then
    Log.Message('The ''|RootItem 1|SubItem 1-2'' item is selected.')
  else
    Log.Message('The ''|RootItem 1|SubItem 1-2'' item is not selected.');
end;

C++Script, C#Script

function Main ()
{
  var p, TreeView;

  // Obtain the TreeView control
  p = Sys["Process"]("TreeViewSample");
  TreeView = p["Window"]("TForm1", "*")["Window"]("TTreeView");

  // Check if the "SubItem 1-2" item is selected
  if (TreeView["wSelection"] == "|RootItem 1|SubItem 1-2")
    Log["Message"]("The '|RootItem 1|SubItem 1-2' item is selected.")
  else
    Log["Message"]("The '|RootItem 1|SubItem 1-2' item is not selected.");
}

Checking if the Item is Expanded or Collapsed

If a tree view item has child items, it can be either expanded so that the child items are visible, or collapsed so that subitems are hidden. To check whether a particular tree view item is expanded or collapsed, you can use one of the following properties:

  • Win32TreeView.wExpanded - Returns the expanded state (true/false) of a tree view item specified by its full path.
  • Win32TreeViewItem.Expanded - Returns the expanded state of a tree view item that corresponds to the given Win32TreeViewItem object.

The following code snippet demonstrates how you can use these properties in scripts:

JavaScript, JScript

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

  // Obtain the TreeView control
  p = Sys.Process("TreeViewSample");
  TreeView = p.Window("TForm1", "*").Window("TTreeView");

  // Expand the item if it is collapsed
  if (! TreeView.wExpanded("|RootItem 1|SubItem 1-1"))
    TreeView.ExpandItem("|RootItem 1|SubItem 1-1")
  else
    Log.Message("The tree view item '|RootItem 1|SubItem 1-1' is already expanded.");

  // Collapse the item if it is expanded
  Item = TreeView.wItems.Item("RootItem 4");
  if (Item.Expanded)
    Item.Collapse()
  else
    Log.Message("The tree view item '|RootItem 4' is already collapsed.");
}

Python

def Main ():

  # Obtain the TreeView control
  p = Sys.Process("TreeViewSample")
  TreeView = p.Window("TForm1", "*").Window("TTreeView")

  # Expand the item if it is collapsed
  if not (TreeView.wExpanded("|RootItem 1|SubItem 1-1")):
    TreeView.ExpandItem("|RootItem 1|SubItem 1-1")
  else:
    Log.Message("The tree view item '|RootItem 1|SubItem 1-1' is already expanded.")

  # Collapse the item if it is expanded
  Item = TreeView.wItems.Item["RootItem 4"]
  if Item.Expanded:
    Item.Collapse()
  else :
    Log.Message("The tree view item '|RootItem 4' is already collapsed.")

VBScript

Sub Main
  Dim p, TreeView, Item

  ' Obtain the TreeView control
  Set p = Sys.Process("TreeViewSample")
  Set TreeView = p.Window("TForm1", "*").Window("TTreeView")

  ' Expand the item if it is collapsed
  If Not TreeView.wExpanded("|RootItem 1|SubItem 1-1") Then
    TreeView.ExpandItem("|RootItem 1|SubItem 1-1")
  Else
    Log.Message("The tree view item '|RootItem 1|SubItem 1-1' is already expanded.")
  End If

  ' Collapse the item if it is expanded
  Set Item = TreeView.wItems.Item("RootItem 4")
  If Item.Expanded Then
    Item.Collapse
  Else
    Log.Message("The tree view item '|RootItem 4' is already collapsed.")
  End If
End Sub

DelphiScript

procedure Main;
var p, TreeView, Item : OleVariant;
begin
  // Obtain the TreeView control
  p := Sys.Process('TreeViewSample');
  TreeView := p.Window('TForm1', '*').Window('TTreeView');

  // Expand an item if it is collapsed
  if not TreeView.wExpanded['|RootItem 1|SubItem 1-1'] then
    TreeView.ExpandItem('|RootItem 1|SubItem 1-1')
  else
    Log.Message('The tree view item ''|RootItem 1|SubItem 1-1'' is already expanded.');

  // Collapse an item if it is expanded
  Item := TreeView.wItems.Item['RootItem 4'];
  if Item.Expanded then
    Item.Collapse
  else
    Log.Message('The tree view item ''|RootItem 4'' is already collapsed.');
end;

C++Script, C#Script

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

  // Obtain the TreeView control
  p = Sys["Process"]("TreeViewSample");
  TreeView = p["Window"]("TForm1", "*")["Window"]("TTreeView");

  // Expand the item if it is collapsed
  if (! TreeView["wExpanded"]("|RootItem 1|SubItem 1-1"))
    TreeView["ExpandItem"]("|RootItem 1|SubItem 1-1")
  else
    Log["Message"]("The tree view item '|RootItem 1|SubItem 1-1' is already expanded.");

  // Collapse the item if it is expanded
  Item = TreeView["wItems"]["Item"]("RootItem 4");
  if (Item["Expanded"])
    Item["Collapse"]()
  else
    Log["Message"]("The tree view item '|RootItem 4' is already collapsed.");
}

Checking if the Item has Child Items

To access child items of a particular tree view item, you can use the Win32TreeViewItem.Items property. If an item has child items, this property returns a TreeViewItemCollection object that provides access to child items. Otherwise, if the item is terminal and does not have child items, this property returns null (null in JavaScript, JScript, C#Script and C++Script, None in Python, Nothing in VBScript, nil in DelphiScript). So, you can check if an item has child items by comparing its Items property to null:

JavaScript

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

  // Obtain a tree view item
  p = Sys.Process("TreeViewSample");
  TreeView = p.Window("TForm1", "*").Window("TTreeView");
  Item = TreeView.wItems.Item("RootItem 1").Items.Item("SubItem 1-2");

  // Check if the item has child items
  if (!strictEqual(Item.Items, null))
    Log.Message ("The tree item '|RootItem1|SubItem 1-2' has " + Item.Items.Count + " child item(s).")
  else
    Log.Message ("The tree item '|RootItem1|SubItem 1-2' does not have child items.");
}

JScript

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

  // Obtain a tree view item
  p = Sys.Process("TreeViewSample");
  TreeView = p.Window("TForm1", "*").Window("TTreeView");
  Item = TreeView.wItems.Item("RootItem 1").Items.Item("SubItem 1-2");

  // Check if the item has child items
  if (Item.Items != null)
    Log.Message ("The tree item '|RootItem1|SubItem 1-2' has " + Item.Items.Count + " child item(s).")
  else
    Log.Message ("The tree item '|RootItem1|SubItem 1-2' does not have child items.");
}

Python

def Main():

  # Obtain a tree view item
  p = Sys.Process("TreeViewSample")
  TreeView = p.Window("TForm1", "*").Window("TTreeView")
  Item = TreeView.wItems.Item("RootItem 1").Items.Item["SubItem 1-2"]

  # Check if the item has child items
  if (Item.Items != None):
    Log.Message ("The tree item '|RootItem1|SubItem 1-2' has " + Item.Items.Count + " child item(s).")
  else:
    Log.Message ("The tree item '|RootItem1|SubItem 1-2' does not have child items.")

VBScript

Sub Main_CheckItemChildItems
  Dim p, TreeView, Item

  ' Obtain a tree view item
  Set p = Sys.Process("TreeViewSample")
  Set TreeView = p.Window("TForm1", "*").Window("TTreeView")
  Set Item = TreeView.wItems.Item("RootItem 1").Items.Item("SubItem 1-2")

  ' Check if the item has child items
  If Not (Item.Items Is Nothing) Then
    Log.Message ("The tree item '|RootItem1|SubItem 1-2' has " & Item.Items.Count & " child item(s).")
  Else
    Log.Message ("The tree item '|RootItem1|SubItem 1-2' does not have child items.")
  End If
End Sub

DelphiScript

procedure Main;
var p, TreeView, Item : OleVariant;
begin
  // Obtain a tree view item
  p := Sys.Process('TreeViewSample');
  TreeView := p.Window('TForm1', '*').Window('TTreeView');
  Item := TreeView.wItems.Item['RootItem 1'].Items.Item['SubItem 1-2'];

  // Check if the item has child items
  if Item.Items <> nil then
    Log.Message ('The tree item ''|RootItem1|SubItem 1-2'' has ' + aqConvert.VarToStr(Item.Items.Count) + ' child item(s).')
  else
    Log.Message ('The tree item ''|RootItem1|SubItem 1-2'' does not have child items.');
end;

C++Script, C#Script

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

  // Obtain a tree view item
  p = Sys["Process"]("TreeViewSample");
  TreeView = p["Window"]("TForm1", "*")["Window"]("TTreeView");
  Item = TreeView["wItems"]["Item"]("RootItem 1")["Items"]["Item"]("SubItem 1-2");

  // Check if the item has child items
  if (Item["Items"] != null)
    Log["Message"]("The tree item '|RootItem1|SubItem 1-2' has " + Item["Items"]["Count"] + " child item(s).")
  else
    Log["Message"]("The tree item '|RootItem1|SubItem 1-2' does not have child items.");
}

See Also

Working With Tree View Controls in Desktop Windows Applications
Addressing Tree View Items in Desktop Windows Applications
Getting Tree View Item's Images in Desktop Windows Applications
Win32TreeViewItem Object

Highlight search results