Using Coordinates to Touch Table View Items

Applies to TestComplete 14.93, last modified on October 12, 2021

One of the most frequent actions you perform on a table view bar is pressing individual items. This topic explains various approaches that can be used to select the items in scripts:

Simulating Touches on Items

One of the common cases when working with the iOS TableView object is touching a specific point of an item. You may need this if a particular control is placed somewhere within the item and you cannot access it. Usually, it is preferable to work with the control directly as it hides the details of control implementation from tests and works regardless of the screen orientation and size.

To specify the point of the touch, use the TouchItemXY and LongTouchItemXY actions.

First two parameters specify an item within which a touch should be performed. To specify the item, you can use the section and item name or index. Note that it is possible to use wildcards (* and ?) or regular expressions to specify a section’s caption. An asterisk (*) corresponds to a string of any length (including an empty string), a question mark corresponds to any single character (including none). If you wish to specify an asterisk (*) as part of an item’s caption, you need to double it, because otherwise, it will be treated as a wildcard. To specify more complicated parts of a caption, use regular expressions. For more information, see Addressing Table View Items. Here is the example of how to touch an item from any section:

tableview.TouchItemXY("*", 1, 50, 30)

The example above performs the touch at the 50, 30 coordinates of the item. However, the position where you need to perform a touch may change depending on the screen orientation or screen size. You can make your test less screen-dependent by using the Width and Height properties of the control. For example, if the item size is 520, 320, you perform a touch on it in the following way:

JavaScript, JScript

function Test()
{
  // Select the mobile device
  Mobile.SetCurrent("iPhone");
  // Obtain the TableView object
  var p = Mobile.Device().Process("SampleApp");
  var tableview = p.Window().TableView();

  // Get the touch position
  var TouchPosX = tableview.Width/10;
  var TouchPosY = tableview.Height/10;

  // Touch the coordinates
  tableview.TouchItemXY("*", 1, TouchPosX, TouchPosY);
}

Python

def Test():
  # Select the mobile device
  Mobile.SetCurrent("iPhone")
  # Obtain the TableView object 
  p = Mobile.Device().Process("SampleApp")
  tableview = p.Window().TableView()

  # Get the touch position
  TouchPosX = tableview.Width/10
  TouchPosY = tableview.Height/10

  # Touch the coordinates
  tableview.TouchItemXY("*", 1, TouchPosX, TouchPosY)

VBScript

Sub Test()
  Dim p, tableview, TouchPosX, TouchPosY
  ' Select the mobile device
  Mobile.SetCurrent("iPhone")
  ' Obtain the TableView object
  Set p = Mobile.Device.Process("SampleApp")
  Set tableview = p.Window().TableView()

  ' Get the touch position
  TouchPosX = tableview.Width/10
  TouchPosY = tableview.Height/10

  ' Touch the coordinates
  Call tableview.TouchItemXY(0, 1, TouchPosX, TouchPosY)
End Sub

DelphiScript

procedure Test();
var
  p, tableview, TouchPosX, TouchPosY;
begin
  // Select the mobile device
  Mobile.SetCurrent('iPhone');
  // Obtain the TableView object
  p := Mobile.Device.Process('SampleApp');
  tableview := p.Window(0).TableView(0);

  // Get the touch position
  TouchPosX := tableview.Width/10;
  TouchPosY := tableview.Height/10;

  // Touch the coordinates
  tableview.TouchItemXY(0, 1, TouchPosX, TouchPosY);
end;

C++Script, C#Script

function Test()
{
  // Select the mobile device
  Mobile.SetCurrent("iPhone");
  // Obtain the TableView object
  var p = Mobile.Device.Process("SampleApp");
  var tableview = p.Window().TableView();

  // Get the touch position
  var TouchPosX = tableview.Width/10;
  var TouchPosY = tableview.Height/10;

  // Touch the coordinates
  tableview.TouchItemXY("*", 1, TouchPosX, TouchPosY);
}

Note: The 2 pixel offset can be ignored in most cases. If you need perfect accuracy, use the fraction, for example 10,4.

Simulating Actions From Keyword Tests

To touch specific points of items in table views from keyword tests, call the methods described above by using the On-Screen Action or Call Object Method operation. See Calling Object Methods.

See Also

Working With iOS Table View Controls
TouchItemXY Method (Specific to iOS TableView Controls)
LongTouchItemXY Action (Specific to iOS TableView Controls)

Highlight search results