Dismissing an ActionSheet Control

Applies to TestComplete 15.40, last modified on July 05, 2022

Action sheet controls in iOS applications provide a user with a choice among a set of alternatives. Each alternative is represented by an action sheet's item. A user touches a corresponding item to select it. For information on how to select an action sheet item from tests, see Selecting an ActionSheet Control's Item.

This topic describes how to dismiss the action sheet control without selecting any items.

Touching a Cancel Item

To dismiss an action sheet, simulate a touch on its Cancel item. To do this, use the TouchItem method of the iOS ActionSheet test object that TestComplete associates with the control. This method simulates a single short touch on the specified item.

The following example demonstrates how to touch a Cancel item:

JavaScript, JScript

function Test()
{

  // Select the mobile device
  Mobile.SetCurrent("iPhone");
  // Obtain the ActionSheet object
  var p = Mobile.Device().Process("SampleApp");
  var actionSheet = p.Window(0).ActionSheet();

  // Touch the "Cancel" item
  actionSheet.TouchItem("Cancel");

}

Python

def Test():

  # Select the mobile device
  Mobile.SetCurrent("iPhone")
  # Obtain the ActionSheet object
  p = Mobile.Device().Process("SampleApp")
  actionSheet = p.Window(0).ActionSheet()

  # Touch the "Cancel" item
  actionSheet.TouchItem("Cancel")

VBScript

Sub Test

  ' Select the mobile device
  Mobile.SetCurrent("iPhone")
  ' Obtain the ActionSheet object
  Set p = Mobile.Device.Process("SampleApp")
  Set actionSheet = p.Window(0).ActionSheet

  ' Touch the "Cancel" item
  actionSheet.TouchItem("Cancel")

End Sub

DelphiScript

procedure Test();
var p, actionSheet;
begin

  // Select the mobile device
  Mobile.SetCurrent('iPhone');
  // Obtain the ActionSheet object
  p := Mobile.Device.Process('SampleApp');
  actionSheet := p.Window(0).ActionSheet();

  // Touch the "Cancel" item
  actionSheet.TouchItem('Cancel');

end;

C++Script, C#Script

function Test()
{

  // Select the mobile device
  Mobile["SetCurrent"]("iPhone");
  // Obtain the ActionSheet object
  var p = Mobile["Device"]["Process"]("SampleApp");
  var actionSheet = p["Window"](0)["ActionSheet"]();

  // Touch the "Cancel" item
  actionSheet["TouchItem"]("Cancel");

}

Simulating Touches Outside of the Control's Bounds

If the action sheet does not contain a Cancel item, to dismiss the action sheet, simulate a click outside of the action sheet control bounds.

To determine the control’s bounds, you can use the Top and Left properties or the ScreenTop and ScreenLeft properties of the corresponding iOS ActionSheet test object. The properties return the top-left corner coordinates of the control relatively to its parent window or to the device screen correspondingly.

Note: To check whether the ActionSheet control has a Cancel item or not, you can use the native cancelButtonIndex property of the control.

The following example demonstrates how to dismiss an action sheet by simulating a touch on the device screen above the action sheet:

JavaScript, JScript

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

  // Get the index of the ActionSheet's Cancel item
  var ind = actionSheet.cancelButtonIndex;
  if (ind < 0)
    // Dismiss the ActionSheet by touching the device screen
    Mobile.Device().Touch(actionSheet.ScreenTop - 2 , actionSheet.ScreenLeft);
  else
    // Dismiss the ActionSheet by touching the Cancel item
    actionSheet.TouchItem(ind);
}

Python

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

  # Get the index of the ActionSheet's Cancel item
  ind = actionSheet.cancelButtonIndex
  if (ind < 0):
    # Dismiss the ActionSheet by touching the device screen
    Mobile.Device().Touch(actionSheet.ScreenTop - 2 , actionSheet.ScreenLeft)
  else:
    # Dismiss the ActionSheet by touching the Cancel item
    actionSheet.TouchItem(ind)

VBScript

Sub Test
  ' Select the mobile device
  Mobile.SetCurrent("iPhone")
  ' Obtain the ActionSheet object
  Set p = Mobile.Device.Process("SampleApp")
  Set actionSheet = p.Window(0).ActionSheet

  ' Get the index of the ActionSheet's Cancel item
  ind = actionSheet.cancelButtonIndex
  If ind < 0 Then
    ' Dismiss the ActionSheet by touching the device screen
    Call Mobile.Device.Touch(actionSheet.ScreenTop - 2 , actionSheet.ScreenLeft)
  Else
    ' Dismiss the ActionSheet by touching the Cancel item
    actionSheet.TouchItem(ind)
  End If
End Sub

DelphiScript

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

  // Get the index of the ActionSheet's Cancel item
  ind := actionSheet.cancelButtonIndex;
  if ind < 0 then
    // Dismiss the ActionSheet by touching the device screen
    Mobile.Device.Touch(actionSheet.ScreenTop - 2 , actionSheet.ScreenLeft)
  else
    // Dismiss the ActionSheet by touching the Cancel item
    actionSheet.TouchItem(ind);
end;

C++Script, C#Script

function Test()
{
  // Select the mobile device
  Mobile["SetCurrent"]("iPhone");
  // Obtain the ActionSheet object
  var p = Mobile["Device"]["Process"]("SampleApp");
  var actionSheet = p["Window"](0)["ActionSheet"]();

  // Get the index of the ActionSheet's Cancel item
  var ind = actionSheet["cancelButtonIndex"];
  if (ind < 0)
    // Dismiss the ActionSheet by touching the device screen
    Mobile["Device"]["Touch"](actionSheet["ScreenTop"] - 2 , actionSheet["ScreenLeft"]);
  else
    // Dismiss the ActionSheet by touching the Cancel item
    actionSheet["TouchItem"](ind);
}

Dismissing ActionSheet Controls From Keyword Tests

To dismiss action sheet controls 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 ActionSheet Controls
Checking an ActionSheet Item's State
iOS ActionSheet Support

Highlight search results