OCRTable Object

Applies to TestComplete 15.45, last modified on December 01, 2022

Description

In TestComplete tests, you can use optical character recognition (OCR) to access tables rendered by a tested application, by their text contents. To do this, you can use any of the following:

The SearchArea can be an image or an onscreen object containing the area where the target table is located.

Both methods return the OCRTable object. Use the object and its properties to get the table data and to simulate user actions over the table. For information on the object’s properties, see the Members section below.

Members

Example

JavaScript, JScript

function Main()
{
  // Navigate to the tested web page
  var url = "https://www.w3schools.com/html/tryit.asp?filename=tryhtml_table_border";
  Browsers.Item("chrome").Run(url);

  // Get the page area containing the table
  var page = Sys.Browser("chrome").Page("*");
  var gridArea = page.FindChild("idStr", "iframeresult", 10);

  if (gridArea != null)
  {
    // Specify whether the table has a header
    var hasHeader = true;
    // Specify the preferable search area
    var searchPref = spNone;
    // Recognize the table content
    var table = OCR.Recognize(gridArea).DetectTable(searchPref, hasHeader);

    // Call the routine that posts table data to the test log
    PrintTable(table);

  }
  else
    Log.Warning("Cannot obtain the search area.");
}

function PrintTable(aTable)
{
  if (aTable != null)
  {
    Log.AppendFolder("Table contents:");
    for (var i = 0; i < aTable.RowCount; i++)
    {
      for (var j = 0; j < aTable.ColumnCount; j++)
      {
        Log.Message(aTable.Cell(i, j).Text);
      }
    }
    Log.PopLogFolder();
  }
}

Python

def Main():
  # Navigate to the test web page
  url = "https://www.w3schools.com/html/tryit.asp?filename=tryhtml_table_border"
  Browsers.Item["chrome"].Run(url)

  # Get the page area containing the table
  page = Sys.Browser("chrome").Page("*")
  gridArea = page.FindChild("idStr", "iframeresult", 10)

  if (gridArea != None):
    # Specify whether the table has a header
    hasHeader = True
    # Specify the preferable search area
    searchPref = spNone
    # Recognize the table contents
    table = OCR.Recognize(gridArea).DetectTable[searchPref, hasHeader]

    # Call the routine that post table data to the test log
    PrintTable(table)

  else:
    Log.Warning("Cannot obtain the search area.")

def PrintTable(aTable):
  if (aTable != None):
    Log.AppendFolder("Table contents:")
    for i in range (0, aTable.RowCount):
      for j in range (0, aTable.ColumnCount):
        Log.Message(aTable.Cell[i, j].Text)
    Log.PopLogFolder()

VBScript

Sub Main
  ' Navigate to the tested web page
  url = "https://www.w3schools.com/html/tryit.asp?filename=tryhtml_table_border"
  Browsers.Item("chrome").Run(url)

  ' Get the page area containing the table
  Set page = Sys.Browser("chrome").Page("*")
  Set gridArea = page.FindChild("idStr", "iframeresult", 10)

  If Not gridArea Is Nothing Then
    ' Specify whether the table has a header
    hasHeader = True
    ' Specify the preferable search area
    searchPref = spNone
    ' Recognize the table content
    Set table = OCR.Recognize(gridArea).DetectTable(searchPref, hasHeader)

    ' Call the routine that posts table data to the test log
    PrintTable(table)

  Else
    Log.Warning("Cannot obtain the search area.")
  End If
End Sub

Sub PrintTable(aTable)
  If Not aTable Is Nothing Then
    Log.AppendFolder("Table contents:")
    For i = 0 To aTable.RowCount - 1
      For j = 0 To aTable.ColumnCount - 1
        Log.Message(aTable.Cell(i, j).Text)
      Next
    Next
    Log.PopLogFolder
  End If
End Sub

DelphiScript

procedure PrintTable(aTable);
var i, j;
begin
  if aTable <> nil then
  begin
    Log.AppendFolder('Table contents:');
    for i := 0 to aTable.RowCount - 1 do
    begin
      for j := 0 to aTable.ColumnCount - 1 do
        Log.Message(aTable.Cell(i, j).Text);
    end;
    Log.PopLogFolder();
  
end;
end;
procedure Main();
var url, page, gridArea, hasHeader, searchPref, table;
begin
  // Navigate to the tested web page
  url := 'https://www.w3schools.com/html/tryit.asp?filename=tryhtml_table_border';
  Browsers.Item('chrome').Run(url);

  // Get the page area containing the table
  page := Sys.Browser('chrome').Page('*');
  gridArea := page.FindChild('idStr', 'iframeresult', 10);

  if gridArea <> nil then
  begin
    // Specify whether the table has a header
    hasHeader := true;
    // Specify the preferable search area
    searchPref := spNone;
    // Recognize the table content
    table := OCR.Recognize(gridArea).DetectTable(searchPref, hasHeader);

    // Call the routine that posts table data to the test log
    PrintTable(table);

  end
  else
    Log.Warning('Cannot obtain the search area.');
end;

C++Script, C#Script

function Main()
{
  // Navigate to the tested web page
  var url = "https://www.w3schools.com/html/tryit.asp?filename=tryhtml_table_border";
  Browsers["Item"]("chrome")["Run"](url);

  // Get the page area containing the table
  var page = Sys["Browser"]("chrome")["Page"]("*");
  var gridArea = page["FindChild"]("idStr", "iframeresult", 10);

  if (gridArea != null)
  {
    // Specify whether the table has a header
    var hasHeader = true;
    // Specify the preferable search area
    var searchPref = spNone;
    // Recognize the table content
    var table = OCR.Recognize(gridArea).DetectTable(searchPref, hasHeader);

    // Call the routine that posts table data to the test log
    PrintTable(table);

  }
  else
    Log["Warning"]("Cannot obtain the search area.");
}

function PrintTable(aTable)
{
  if (aTable != null)
  {
    Log["AppendFolder"]("Table contents:");
    for (var i = 0; i < aTable["RowCount"]; i++)
    {
      for (var j = 0; j < aTable["ColumnCount"]; j++)
      {
        Log["Message"](aTable["Cell"](i, j)["Text"]);
      }
    }
    Log["PopLogFolder"]();
  }
}

See Also

Optical Character Recognition

Highlight search results