Retrieving TreeView Items. Example in Desktop Windows Applications

Applies to TestComplete 15.69, last modified on November 13, 2024

The following code demonstrates how you can work with TTreeView items (from the Borland VCL) from a TestComplete script using the methods and properties of the Win32TreeView object. This example assumes that the application displays a form with a TTreeView control on it. First, SaveItems retrieves all tree view items and saves them as text under the specified filename. Then, SaveItems compares this file with another one (the name is hard coded) and reports whether the tree view contents have changed directly to the test log.

To start testing, run the Main routine in TestComplete.

JavaScript

function Main()
{
  var p, w;

  p = Sys.Process("Project1");
  w = p.Window("TForm1", "Form1");
  SaveItems(w.Window("TTreeView", "TreeView1"), "C:\\Work\\Test1.txt");
}

function SaveItems(TV,FileName)
{
  Log.Message("TreeView Items:");
  aqFile.WriteToTextFile(FileName, "TreeView Items:" + "\r\n", aqFile.ctANSI, true);
  SaveChildItems(TV.wItems, 0, FileName);

  // Compares the generated file with the stored file
  if (! Files.Compare(FileName, "PreviousItems.txt"))
    Log.Error("TreeView has changed")
  else
    Log.Message("OK");
}

function SaveChildItems(ItemColl, Depth, FileName)
{
  var s, indent;

  for (let i=0; i<ItemColl.Count; i++)
  {
    // Forms an indent to denote subitems
    indent = "";
    for (let j=0; j<Depth; j++)
      indent += " ";
    // Obtains item caption
    s = indent + ItemColl.Item(i).Text;
    Log.Message(s);
    // Writes item caption to file
    aqFile.WriteToTextFile(FileName, s + "\r\n", aqFile.ctANSI);
    // Verifies whether the item has subitems and recursively calls the procedure on success
    if (!strictEqual(ItemColl.Item(i).Items, null))
      SaveChildItems(ItemColl.Item(i).Items, Depth+1, FileName)
  }
}

JScript

function Main()
{
  var p, w;

  p = Sys.Process("Project1");
  w = p.Window("TForm1", "Form1");
  SaveItems(w.Window("TTreeView", "TreeView1"), "C:\\Work\\Test1.txt");
}

function SaveItems(TV,FileName)
{
  Log.Message("TreeView Items:");
  aqFile.WriteToTextFile(FileName, "TreeView Items:" + "\r\n", aqFile.ctANSI, true);
  SaveChildItems(TV.wItems, 0, FileName);

  // Compares the generated file with the stored file
  if (! Files.Compare(FileName, "PreviousItems.txt"))
    Log.Error("TreeView has changed")
  else
    Log.Message("OK");
}

function SaveChildItems(ItemColl, Depth, FileName)
{
  var i, j, s, indent;

  for (i=0; i<ItemColl.Count; i++)
  {
    // Forms an indent to denote subitems
    indent = "";
    for (j=0; j<Depth; j++)
      indent += " ";
    // Obtains item caption
    s = indent + ItemColl.Item(i).Text;
    Log.Message(s);
    // Writes item caption to file
    aqFile.WriteToTextFile(FileName, s + "\r\n", aqFile.ctANSI);
    // Verifies whether the item has subitems and recursively calls the procedure on success
    if (ItemColl.Item(i).Items != null)
      SaveChildItems(ItemColl.Item(i).Items, Depth+1, FileName)
  }
}

Python

def Main():

  p = Sys.Process("Project1")
  w = p.Window("TForm1", "Form1")
  SaveItems(w.Window("TTreeView", "TreeView1"), "C:\\Work\\Test1.txt")

def SaveItems(TV,FileName):

  Log.Message("TreeView Items:")
  aqFile.WriteToTextFile(FileName, "TreeView Items:" + "\r\n", aqFile.ctANSI, True)
  SaveChildItems(TV.wItems, 0, FileName)

  # Compares the generated file with the stored file
  if not Files.Compare(FileName, "PreviousItems.txt"):
    Log.Error("TreeView has changed")
  else:
    Log.Message("OK")

def SaveChildItems(ItemColl, Depth, FileName):

  for i in range(0, ItemColl.Count):
    # Forms an indent to denote subitems
    indent = ""
    for j in range(0, Depth):
       indent += " "
    # Obtains item caption
    s = indent + ItemColl.Item[i].Text
    Log.Message(s)
    # Writes item caption to file 
    aqFile.WriteToTextFile(FileName, s + "\r\n", aqFile.ctANSI)
    # Verifies whether the item has subitems and recursively calls the procedure on success
    if (ItemColl.Item[i].Items != None):
      SaveChildItems(ItemColl.Item[i].Items, Depth+1, FileName)

VBScript

Sub Main
  Dim p, w

  Set p = Sys.Process("Project1")
  Set w = p.Window("TForm1", "Form1")

  Call SaveItems (w.Window("TTreeView", "TreeView1"), "C:\Work\Test1.txt")
End Sub

Sub SaveItems(TV, FileName)
  Log.Message("TreeView Items:")
  Call aqFile.WriteToTextFile(FileName, "TreeView Items:" & vbNewLine, aqFile.ctANSI, True)
  Call SaveChildItems(TV.wItems, 0, FileName)

  ' Compares the generated file with the stored file
  If Not Files.Compare(FileName, "PreviousItems.txt") Then
    Log.Error("TreeView has changed")
  Else
    Log.Message("OK")
  End If
End Sub

Sub SaveChildItems(ItemColl, Depth, FileName)
  Dim i, j, indent, s

  For i = 0 To ItemColl.Count-1
    ' Forms an indent to denote subitems
    indent = ""
    For j = 0 To Depth-1
      indent = indent & " "
    Next
  ' Obtains item caption
  s = indent + ItemColl.Item(i).Text
  Log.Message(s)
  ' Writes item caption to file
  Call aqFile.WriteToTextFile(FileName, s & vbNewLine, aqFile.ctANSI)
    ' Verifies whether the item has subitems and recursively calls the procedure on success
    If Not (ItemColl.Item(i).Items Is Nothing) Then
      Call SaveChildItems(ItemColl.Item(i).Items, Depth+1, FileName)
    End If
  Next
End Sub

DelphiScript

procedure SaveItems(TV, FileName: OleVariant); forward;
procedure SaveChildItems(ItemColl, Depth, FileName: OleVariant); forward;

procedure Main;
var p, w: OleVariant;
begin
  p := Sys.Process('Project1');
  w := p.Window('TForm1', 'Form1');
  SaveItems(w.Window('TTreeView', 'TreeView1'),'C:\Work\Test1.txt');
end;

procedure SaveItems(TV, FileName: OleVariant);
begin
  Log.Message('TreeView Items:');
  aqFile.WriteToTextFile(FileName, 'TreeView Items:' + #13#10, aqFile.ctANSI, true);
  SaveChildItems(TV.wItems, 0, FileName);
  // Compares the generated file with the stored file
  if not Files.Compare(FileName, 'PreviousItems.txt') then
    Log.Error('TreeView has changed')
  else
    Log.Message('OK');
end;

procedure SaveChildItems(ItemColl, Depth, FileName: OleVariant);
var i, j, s, indent: OleVariant;
begin
  for i:=0 to ItemColl.Count-1 do
  begin
    // Forms an indent to denote subitems
    indent := '';
    for j:=0 to Depth-1 do
      indent := indent + ' ';
    // Obtains item caption
    s := indent + ItemColl.Item[i].Text;
    Log.Message(s);
    // Writes item caption to file
    aqFile.WriteToTextFile(FileName, s + #13#10, aqFile.ctANSI);
    // Verifies whether the item has subitems and recursively calls the procedure on success
    if ItemColl.Item[i].Items <> nil then
      SaveChildItems(ItemColl.Item[i].Items, Depth+1, FileName);
  end;
end;

C++Script, C#Script

function Main()
{
  var p, w;

  p = Sys["Process"]("Project1");
  w = p["Window"]("TForm1", "Form1");
  SaveItems(w["Window"]("TTreeView", "TreeView1"), "C:\\Work\\Test1.txt");
}

function SaveItems(TV,FileName)
{
  Log["Message"]("TreeView Items:");
  aqFile["WriteToTextFile"](FileName, "TreeView Items:" + "\r\n", aqFile.ctANSI, true);
  SaveChildItems(TV["wItems"], 0, FileName);

  // Compares the generated file with the stored file
  if (! Files["Compare"](FileName, "PreviousItems.txt"))
    Log["Error"]("TreeView has changed")
  else
    Log["Message"]("OK");
}

function SaveChildItems(ItemColl, Depth, FileName)
{
  var i, j, s, indent;

  for (i=0; i<ItemColl["Count"]; i++)
  {
    // Forms an indent to denote subitems
    indent = "";
    for (j=0; j<Depth; j++)
      indent += " ";
    // Obtains item caption
    s = indent + ItemColl["Item"](i).Text;
    Log["Message"](s);
    // Writes item caption to file
    aqFile.WriteToTextFile(FileName, s + "\r\n", aqFile.ctANSI);
    // Verifies whether the item has subitems and recursively calls the procedure on success
    if (ItemColl["Item"](i)["Items"] != null)
      SaveChildItems(ItemColl["Item"](i)["Items"], Depth+1, FileName)
  }
}

See Also

Working With Tree View Controls in Desktop Windows Applications

Highlight search results