DetectTable Property

Applies to TestComplete 15.68, last modified on October 03, 2024

Description

The DetectTable property returns the OCRTable object that represents recognized text data that resides at the specified position of the recognition area, in a tabular form. To capture the table data, TestComplete uses optical character recognition (OCR).

If the specified area does not contain the data that can be presented as a table, the property will post an error message to the test log.

Declaration

OCRTextDocumentObj.DetectTable(SelectionPreference, HasHeader)

Read-Only Property An OCRTable object
OCRTextDocumentObj An expression, variable or parameter that specifies a reference to an OCRTextDocument object
SelectionPreference [in]    Optional    Integer Default value: -10   
HasHeader [in]    Optional    Object    

Applies To

The property is applied to the following object:

Parameters

The property has the following parameters:

SelectionPreference

If the UI area contains several tables, you can use this parameter to specify which of them the property should return. The allowed parameter values are:

Constant Description
spNone The default value. The property will return the first table it finds.
spNearestToCenter The property will return the table that is the closest to the center of the recognition area.
spLeftMost The property will return the leftmost table.
spRightMost The property will return the rightmost table.
spTopMost The property will return the topmost table.
spBottomMost The property will return the bottommost table.
spLargest The property will return the largest table (the table that occupies the largest area).
spSmallest The property will return the smallest table (the table that occupies the smallest area).

Note: The table position is estimated relative to the recognition area to which the block belongs.

HasHeader

A Boolean value that specifies whether the table you want to capture has a header. If the parameter is omitted, the method will try to automatically determine if the table has a header.

Property Value

An OCRTable object that describes a table residing in the specified UI area.

If the specified area does not contain the data that can be presented in a tabular form, the property will post an error message to the test log.

Example

The code below shows how to recognize a table on a web page and post the table 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

OCRTextDocument Object
OCRTable Object
Optical Character Recognition

Highlight search results