Simulating User Actions on iOS Applications (Legacy)

Applies to TestComplete 15.46, last modified on January 09, 2023
The information below concerns legacy mobile tests that work with mobile devices connected to the local computer. For new mobile tests, we recommend using the newer cloud-compatible approach.

This topic explains how to simulate user actions like touches, swipes or keystrokes on tested iOS applications. It includes the following sections:

General Notes

  • In order for TestComplete to be able to simulate user actions on an iOS application, the application must be prepared for testing. See Preparing iOS Applications (Legacy).

  • To simulate user actions, you can use methods of tested objects that are wrappers for iOS controls and windows.

    You can also use methods and properties of the Device object that corresponds to your iOS device. In this case, user actions are simulated at screen coordinates.

    One more option is to use native methods and properties of objects of your tested iOS application. In your tests, you can call any public, protected or private members that TestComplete is able to access.

  • Important: the Device object’s methods as well as methods of test objects work only if a prepared iOS application is currently active on the device. Otherwise, user actions are not simulated; calling object methods will cause an error, calling Device’s methods will not perform any action.

Simulating Touch Actions

Every object in iOS applications can be touched (or “clicked”). To simulate a touch, use the Touch method that TestComplete provides for iOS test objects:

JavaScript, JScript

Mobile.SetCurrent("iPad John Smith");
Aliases.Device.processMyApp.window.view.navigationbar.buttonSave.Touch();

Python

Mobile.SetCurrent("iPad John Smith");
Aliases.Device.processMyApp.window.view.navigationbar.buttonSave.Touch();

VBScript

Call Mobile.SetCurrent("iPad John Smith")
Aliases.Device.processMyApp.window.view.navigationbar.buttonSave.Touch

DelphiScript

Mobile.SetCurrent('iPad John Smith');
Aliases.Device.processMyApp.window.view.navigationbar.buttonSave.Touch();

C++Script, C#Script

Mobile["SetCurrent"]("iPad John Smith");
Aliases["Device"]["processMyApp"]["window"]["view"]["navigationbar"]["buttonSave"]["Touch"]();

To call this method from a keyword test, use the Call Object Method or Run Code Snippet operation.

By default, Touch does not use parameters and simulates a touch at the center of the control. If necessary, you can specify control-related coordinates and “touch” the control at the desired point:

JavaScript, JScript

Mobile.SetCurrent("iPad John Smith");
Aliases.Device.processMyApp.window.view.textField.Touch(12, 5);

Python

Mobile.SetCurrent("iPad John Smith");
Aliases.Device.processMyApp.window.view.textField.Touch(12, 5);

VBScript

Call Mobile.SetCurrent("iPad John Smith")
Aliases.Device.processMyApp.window.view.textField.Touch 12, 5

DelphiScript

Mobile.SetCurrent('iPad John Smith');
Aliases.Device.processMyApp.window.view.textField.Touch(12, 5);

C++Script, C#Script

Mobile["SetCurrent"]("iPad John Smith");
Aliases["Device"]["processMyApp"]["window"]["view"]["textField"]["Touch"](12, 5);

You can also simulate touch events at screen coordinates. To do this, use the Device.Touch method.

JavaScript, JScript

Mobile.SetCurrent("iPad John Smith");
Aliases.Device.Touch(70, 113);

Python

Mobile.SetCurrent("iPad John Smith");
Aliases.Device.Touch(70, 113);

VBScript

Call Mobile.SetCurrent("iPad John Smith")
Aliases.Device.Touch 70, 113

DelphiScript

Mobile.SetCurrent('iPad John Smith');
Aliases.Device.Touch(70, 113);

C++Script, C#Script

Mobile["SetCurrent"]("iPad John Smith");
Aliases["Device"]["Touch"](70, 113);

To touch at specific screen coordinates from keyword tests, use the Device Touch operation.

Simulating Long Touch Actions

To simulate long touches, use the LongTouch method of iOS test objects. It is very similar to the Touch method described above. If you do not specify method parameters, it simulates a long touch at the center of the test object. If you use parameters, the touch will be simulated at the specified point within the control:

JavaScript, JScript

Mobile.SetCurrent("iPad John Smith");
Aliases.Device.processMyApp.window.tableview.tableviewcell.LongTouch();
Aliases.Device.processMyApp.window.tableview.tableviewcell.LongTouch(20, 5);

Python

Mobile.SetCurrent("iPad John Smith");
Aliases.Device.processMyApp.window.tableview.tableviewcell.LongTouch();
Aliases.Device.processMyApp.window.tableview.tableviewcell.LongTouch(20, 5);

VBScript

Call Mobile.SetCurrent("iPad John Smith")
Aliases.Device.processMyApp.window.tableview.tableviewcell.LongTouch
Aliases.Device.processMyApp.window.tableview.tableviewcell.LongTouch 20, 5

DelphiScript

Mobile.SetCurrent('iPad John Smith');
Aliases.Device.processMyApp.window.tableview.tableviewcell.LongTouch();
Aliases.Device.processMyApp.window.tableview.tableviewcell.LongTouch(20, 5);

C++Script, C#Script

Mobile["SetCurrent"]("iPad John Smith");
Aliases["Device"]["processMyApp"]["window"]["tableview"]["tableviewcell"]["LongTouch"]();
Aliases["Device"]["processMyApp"]["window"]["tableview"]["tableviewcell"]["LongTouch"](20, 5);

To simulate long touches at screen coordinates, use the TouchAndHold method of the Device object. It uses three parameters. The first two specify coordinates of the touch, and the third one specifies the needed delay (by default, 1000 milliseconds):

JavaScript, JScript

Mobile.SetCurrent("iPad John Smith");
// Default delay
Aliases.Device.TouchAndHold(20, 70);
// 2 sec delay
Aliases.Device.TouchAndHold(20, 70, 2000);

Python

Mobile.SetCurrent("iPad John Smith");
# Default delay
Aliases.Device.TouchAndHold(20, 70);
# 2 sec delay
Aliases.Device.TouchAndHold(20, 70, 2000);

VBScript

Call Mobile.SetCurrent("iPad John Smith")
' Default delay
Aliases.Device.TouchAndHold 20, 70
' 2 sec delay
Aliases.Device.TouchAndHold 20, 70, 2000

DelphiScript

Mobile.SetCurrent('iPad John Smith');
// Default delay
Aliases.Device.TouchAndHold(20, 70);
// 2 sec delay
Aliases.Device.TouchAndHold(20, 70, 2000);

C++Script, C#Script

Mobile["SetCurrent"]("iPad John Smith");
// Default delay
Aliases["Device"]["TouchAndHold"](20, 70);
// 2 sec delay
Aliases["Device"]["TouchAndHold"](20, 70, 2000);

To call the described methods from keyword tests, use the Call Object Method or Run Code Snippet operation.

Simulating Keystrokes

To simulate keystrokes on a control, you can use the SetText and Keys methods of the appropriate wrapper test object. To simulate keystrokes on the entire device screen, use the Device.Keys method. The latter will send keys to the currently active control.

JavaScript, JScript

Mobile.SetCurrent("iPad John Smith");
Aliases.Device.processMyApp.window.textField.SetText("!hello");
Aliases.Device.processMyApp.window.textField.Keys("!hello");
Aliases.Device.Keys("!hello");

Python

Mobile.SetCurrent("iPad John Smith");
Aliases.Device.processMyApp.window.textField.SetText("!hello");
Aliases.Device.processMyApp.window.textField.Keys("!hello");
Aliases.Device.Keys("!hello");

VBScript

Call Mobile.SetCurrent("iPad John Smith")
Call Aliases.Device.processMyApp.window.textField.SetText("!hello")
Call Aliases.Device.processMyApp.window.textField.Keys("!hello")
Call Aliases.Device.Keys("!hello")

DelphiScript

Mobile.SetCurrent('iPad John Smith');
Aliases.Device.processMyApp.window.textField.SetText('!hello');
Aliases.Device.processMyApp.window.textField.Keys('!hello');
Aliases.Device.Keys('!hello');

C++Script, C#Script

Mobile["SetCurrent"]("iPad John Smith");
Aliases["Device"]["processMyApp"]["window"]["textField"]["SetText"]("!hello");
Aliases["Device"]["processMyApp"]["window"]["textField"]["Keys"]("!hello");
Aliases["Device"]["Keys"]("!hello");

iOS 13:

The Keys action might not work if the iOS TextField control is set to disable text copying (secureTextEntry = true) and if an input string contains symbols from a language that differs from the keyboard language. In such cases, use the SetText action instead of Keys.

To simulate keystrokes from keyword tests, call these methods with the Call Object Method or Run Code Snippet operation.

Simulating Swipe and Drag Actions

Swipe actions are used to scroll through the contents or navigate between screens, windows and views. Drag actions are used to move items within containers.

To simulate these actions, use the Device.Swipe and Device.Drag methods. Note that these methods work only if there is a prepared iOS application which is currently active on the device:

JavaScript, JScript

Mobile.SetCurrent("iPad John Smith");
Aliases.Device.Swipe(650, 100, 600, 500, 10, 50);
Aliases.Device.Drag(50, 70, 300, 150, 500);

Python

Mobile.SetCurrent("iPad John Smith");
Aliases.Device.Swipe(650, 100, 600, 500, 10, 50);
Aliases.Device.Drag(50, 70, 300, 150, 500);

VBScript

Call Mobile.SetCurrent("iPad John Smith")
Call Aliases.Device.Swipe(650, 100, 600, 500, 10, 50)
Call Aliases.Device.Drag(50, 70, 300, 150, 500)

DelphiScript

Mobile.SetCurrent('iPad John Smith');
Aliases.Device.Swipe(650, 100, 600, 500, 10, 50);
Aliases.Device.Drag(50, 70, 300, 150, 500);

C++Script, C#Script

Mobile["SetCurrent"]("iPad John Smith");
Aliases["Device"]["Swipe"](650, 100, 600, 500, 10, 50);
Aliases["Device"]["Drag"](50, 70, 300, 150, 500);

You can also simulate a drag action within the control itself using the Drag action associated with the control. Note that the coordinates are relative to the control, not to the mobile device's screen.

To simulate swipes and drags in keyword tests, call these methods with the Call Object Method or Run Code Snippet operation.

Simulating Custom Events

In your tests you may need to simulate arbitrary actions that cannot be performed with any of the methods and operations described above. For example, you may need to “draw” a custom shape on the device’s screen with a fingertip.

You can simulate custom actions using the following TestComplete scripting methods:

Method Description
Device.TouchPress Simulates pressing the device screen at the specified point.
Device.Move Simulates a movement on the device screen.
Device.TouchRelease Simulates releasing the device screen at the specified point.
aqUtils.Delay Simulates a delay in milliseconds.

Using these methods, you can create custom actions or simulate standard actions, such as Drag, Touch, Long Press and others.

Below are some script examples that demonstrate using these methods to simulate various actions. If you want to simulate these actions from keyword tests, use the Run Code Snippet operation or create several Call Object Method operations that invoke the needed methods consequently.

  • Example 1

    To simulate a custom drag action, call the Device.TouchPress and Device.TouchRelease methods consequently. Call the TouchPress method at the position of the item to be dragged, and then TouchRelease at the destination coordinates:

    JavaScript, JScript

    Mobile.SetCurrent("iPad John Smith");
    Aliases.Device.TouchPress(50, 70);
    Aliases.Device.TouchRelease(300, 150);

    Python

    Mobile.SetCurrent("iPad John Smith");
    Aliases.Device.TouchPress(50, 70);
    Aliases.Device.TouchRelease(300, 150);

    VBScript

    Call Mobile.SetCurrent("iPad John Smith")
    Call Aliases.Device.TouchPress(50, 70)
    Call Aliases.Device.TouchRelease(300, 150)

    DelphiScript

    Mobile.SetCurrent('iPad John Smith');
    Aliases.Device.TouchPress(50, 70);
    Aliases.Device.TouchRelease(300, 150);

    C++Script, C#Script

    Mobile["SetCurrent"]("iPad John Smith");
    Aliases["Device"]["TouchPress"](50, 70);
    Aliases["Device"]["TouchRelease"](300, 150);

    Note that the X and Y parameters of the TouchPress and TouchRelease methods are dissimilar.

  • Example 2

    To simulate custom shape actions, call Device.TouchPress, several Device.Move methods and then Device.TouchRelease:

    JavaScript, JScript

    Mobile.SetCurrent("iPad John Smith");
    Aliases.Device.TouchPress(250, 270);
    Aliases.Device.Move(270, 270);
    Aliases.Device.Move(290, 290);
    Aliases.Device.Move(240, 240);
    Aliases.Device.TouchRelease(240, 240);

    Python

    Mobile.SetCurrent("iPad John Smith");
    Aliases.Device.TouchPress(250, 270);
    Aliases.Device.Move(270, 270);
    Aliases.Device.Move(290, 290);
    Aliases.Device.Move(240, 240);
    Aliases.Device.TouchRelease(240, 240);

    VBScript

    Call Mobile.SetCurrent("iPad John Smith")
    Call Aliases.Device.TouchPress(250, 270)
    Call Aliases.Device.Move(270, 270)
    Call Aliases.Device.Move(290, 290)
    Call Aliases.Device.Move(240, 240)
    Call Aliases.Device.TouchRelease(240, 240)

    DelphiScript

    Mobile.SetCurrent('iPad John Smith');
    Aliases.Device.TouchPress(250, 270);
    Aliases.Device.Move(270, 270);
    Aliases.Device.Move(290, 290);
    Aliases.Device.Move(240, 240);
    Aliases.Device.TouchRelease(240, 240);

    C++Script, C#Script

    Mobile["SetCurrent"]("iPad John Smith");
    Aliases["Device"]["TouchPress"](250, 270);
    Aliases["Device"]["Move"](270, 270);
    Aliases["Device"]["Move"](290, 290);
    Aliases["Device"]["Move"](240, 240);
    Aliases["Device"]["TouchRelease"](240, 240);

Known Issues

If your iOS application is playing animation while your test is simulating user actions, TestComplete does not detect the animation playback and continues simulating user actions. If the control over which the test is to simulate user actions is not available during the animation playback, the test fails.

If you have issues with simulating user actions and they are caused by animation playback, you can do any of the following:

  • In your test, add a delay before the user action that TestComplete fails to simulate because the needed object is not available while the animation is playing. TestComplete will pause the test run until the specified timeout elapses. To learn more, see Delaying Test Execution.

— or —

— or —

  • If you use Name Mapping in your test, configure mapping criteria for the problematic object to use the VisibleOnScreen property. In this case, your test will wait until the object becomes visible before simulating user actions over it.

See Also

Testing iOS Applications (Legacy)
Testing iOS Applications - Overview (Legacy)
Creating Tests for iOS Applications (Legacy)

Highlight search results