AsTable Property

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

Description

The AsTable property returns the OCRTable object that presents the recognized text data 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 return an empty table.

Declaration

OCRTextDocumentObj.AsTable(HasHeader)

Read-Only Property An OCRTable object
OCRTextDocumentObj An expression, variable or parameter that specifies a reference to an OCRTextDocument object
HasHeader [in]    Required    Boolean    

Applies To

The property is applied to the following object:

Parameters

The property has the following parameter:

HasHeader

Specifies whether the table you want to capture has a header.

Property Value

An OCRTable object that describes a table that matches the specified area.

If the specified area does not contain the data that can be presented in a tabular form, the property will return an empty table.

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;
    // Recognize the table content
    var table = OCR.Recognize(gridArea).DetectTable(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
    # Recognize the table contents
    table = OCR.Recognize(gridArea).AsTable[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
    ' Recognize the table content
    Set table = OCR.Recognize(gridArea).AsTable(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;
    // Recognize the table content
    table := OCR.Recognize(gridArea).DetectTable(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;
    // Recognize the table content
    var table = OCR.Recognize(gridArea).AsTable(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