OCRTable Object

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

Description

Use the OCRTable object to access the table that TestComplete recognizes on the screen by using optical character recognition (OCR). To get the object in tests, use the OCR.Recognize(SearchArea).AsTable or OCR.Recognize(SearchArea).DetectTable properties.

Members

Example

The example below uses the DetectTable property to recognize tabular data on a web page and posts the recognized data to the test log:

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