Retrieving ListView Items. Example

Applies to TestComplete 14.10, last modified on June 5, 2019

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

To start testing, run the Main routine in TestComplete.

JavaScript, JScript

function Main()
{
  var p, w;

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

  SaveItems (w.Window("TListView", "listView1"), "c:\\Work\\Test1.txt");
}

function SaveItems(LV , FileName)
{
  var i,s;

  Log.Message("ListView Items:");
  aqFile.WriteToTextFile(FileName, "ListView Items:" + "\r\n", aqFile.ctANSI, true);

  // Iterates through the items collection
  for (i=0; i<LV.wItemCount; i++)
  {
    // Obtains item caption
    s = LV.wItem(i,0);
    Log.Message(s);
    // Writes item caption to file
    aqFile.WriteToTextFile(FileName, s + "\r\n", aqFile.ctANSI);
  }

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

Python

def Main():

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

  SaveItems (w.Window("TListView", "listView1"), "c:\\Work\\Test1.txt")

def SaveItems(LV , FileName):

  Log.Message("ListView Items:")
  aqFile.WriteToTextFile(FileName, "ListView Items:" + "\r\n", aqFile.ctANSI, True)

  # Iterates through the items collection 
  for i in range(0, LV.wItemCount-1):
    # Obtains item caption 
    s = LV.wItem[i,0]
    Log.Message(s)
    # Writes item caption to file 
    aqFile.WriteToTextFile(FileName, s + "\r\n", aqFile.ctANSI)

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

VBScript

Sub Main
  Dim p, w

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

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

Sub SaveItems(LV, FileName)
  Log.Message("ListView Items:")
  Call aqFile.WriteToTextFile(FileName, "ListView Items:" & vbNewLine, aqFile.ctANSI, True)

  ' Iterates through the items collection
  For i=0 To LV.wItemCount-1
    ' Obtains item caption
    s = LV.wItem(i,0)
    Log.Message(s)
    ' Writes item caption to file
    Call aqFile.WriteToTextFile(FileName, s & vbNewLine, aqFile.ctANSI)
  Next 

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

DelphiScript

procedure SaveItems(LV, FileName : OleVariant);
var i, s : OleVariant;
begin 
  Log.Message('ListView Items:');
  aqFile.WriteToTextFile(FileName, 'ListView Items:' + #13#10, aqFile.ctANSI, true);

  // Iterates through the items collection
  for i:=0 to LV.wItemCount-1 do 
  begin 
    // Obtains item caption
    s := LV.wItem[i,0];
    Log.Message(s);
    // Writes item caption to file
    aqFile.WriteToTextFile(FileName, s + #13#10, aqFile.ctANSI);
  end;

  // Compares the generated file with the stored file
  if not Files.Compare(FileName, 'ListViewContents.txt') then 
    Log.Error('ListView has changed')
  else 
    Log.Message('OK');
end;

procedure Main;
var p, w: OleVariant;
begin 
  p := Sys.Process('Project1');
  w := p.Window('TForm1', 'Form1');

  SaveItems (w.Window('TListView', 'listView1'), 'c:\Work\Test1.txt');
end;

C++Script, C#Script

function Main()
{
  var p, w;

  p = Sys["Process"]("Project1");
  w = p["Window"]("TForm1", "Form1");

  SaveItems (w["Window"]("TListView", "listView1"), "c:\\Work\\Test1.txt");
}

function SaveItems(LV , FileName)
{
  var i,s;

  Log["Message"]("ListView Items:");
  aqFile["WriteToTextFile"](FileName, "ListView Items:" + "\r\n", aqFile.ctANSI, true);

  // Iterates through the items collection
  for (i=0; i<LV["wItemCount"]; i++)
  {
    // Obtains item caption
    s = LV["wItem"](i,0);
    Log["Message"](s);
    // Writes item caption to file
    aqFile.WriteToTextFile(FileName, s + "\r\n", aqFile.ctANSI);
  }

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

See Also

Working With List View Controls

Highlight search results