6.13. Android Convenience API

6.13.1. GestureBuilder
[Important]Android-specific

The Android Convenience API is only available for the Squish for Android editions.

Here are some quick links to the Android Convenience API's functions:

String defaultDevice

This read-only property of squishinfo Object (Section 6.3.13) holds the Android device string that is selected in the IDE or passed as --device option to squishrunner

doubleTap(objectOrName);

doubleTap(objectOrName, x, y);

This function double taps on the specified objectOrName widget. The x and y coordinates are optional. If they are not specified, the double tap is made in the center of the widget. On the other hand, if the additional parameters are given, the double tap is made at position x and y (in the objectOrName widget's coordinates).

goBack(objectOrName);

This function taps the Android back button. The specified objectOrName can refer to any object that is visible, though Squish will record the object having the input focus.

gesture(objectOrName, touches);

This function plays a gesture. The specified objectOrName can refer to any object that is visible and serves for synchronization only. The specified touches refers to a GestureBuilder object, which can be retrieved using readGesture.

Image grabWidget(object);

This function takes a screenshot of the object view rectangle and returns it as an Image Object (Section 6.3.12).

hideKeyboard(object);

This function hides the soft keyboard when it is showing. The specified object can refer to any object that is visible. Note that waitForObject also implicitly hides the soft keyboard.

installEventHandler(eventName, handlerFunctionName);

This function installs a global event handler. The script function named in handlerFunctionName (which must be passed as a string, not as a function reference, except for Python, which supports passing a function reference, too), will be called when an event of the eventName type occurs.

The eventName can be the name of any of the following event types:

BannerShowed
This event occurs when an android.widget.Toast is shown
DialogOpened
This event occurs when an android.app.AlertDialog is shown

The function named in handlerFunctionName is called with one argument—the object that was shown.

[Note]Python-specific

In Python scripts, you can specify the callback function to invoke by passing an actual function or a lambda function.

For examples see How to Use Event Handlers (Section 5.10).

[Important]The AUT Must be Running

The installEventHandler function will only work if it is called after the AUT has been started (e.g., using the startApplication function).

longPress(objectOrName);

longPress(objectOrName, x, y);

This function taps by pressing 2s on the specified objectOrName widget. The x and y coordinates are optional. If they are not specified the tap is made in the center of the widget. On the other hand, if the additional parameters are given, the tap is made at position x and y (in the objectOrName widget's coordinates).

longPressAndDrag(objectOrName, dx, dy);

longPressAndDrag(objectOrName, x, y, dx, dy);

This function performs a touch drag operation, pressing 2s first then drag, of the specified objectOrName widget. The objectOrName widget is dragged by dx pixels horizontally and by dy pixels vertically. The x and y coordinates are optional. If they are not specified the initial press is made in the center of the widget. On the other hand, if the additional parameters are given, the initial press is made at position x and y (in the objectOrName widget's coordinates).

openMenu(objectOrName);

This function taps the Android menu button. The specified objectOrName can refer to any object that is visible, though Squish will record the object having the input focus.

Object nativeObject

Squish automatically adds a property called nativeObject to every Android object. The native object provides access to all the underlying object's methods and properties.

For examples of how to use the nativeObject see How to Use the nativeObject Property (Section 5.7.1).

readGesture(gesture-file);

This function opens a gesture file from a test suite directory and returns a GestureBuilder object. This can then be passed to gesture. The specified gesture-file refers to the filename.

startObserver();

This function should only be used when --no-autostart-observer (Section 7.4.6.2.6) is specified as launcher argument to androidobserver (Section 7.4.6.2) or when stopObserver has been called. When the observer is not running, the object recognition with popups, dropdowns and dialogs is limited or will not work at all.

stopObserver();

This function stops the observer. Use this function when a certain part in the app execution becomes very slow when running under Squish. See startObserver for enabling the observer again.

tapMenuItem(objectOrName);

This function performs a menu action. The specified objectOrName can refer to any object that is visible, though Squish will record the current Activity object.

[Note]Note
The application menu has to be open or else this function will fail.

tapObject(objectOrName);

tapObject(objectOrName, x, y);

This function taps on the specified objectOrName widget. The x and y coordinates are optional. If they are not specified, the tap is made in the center of the widget. On the other hand, if the additional parameters are given, the click is made at position x and y (in the objectOrName widget's coordinates).

touchAndDrag(objectOrName, x, y, dx, dy);

This function performs a touch drag operation. It initiates a drag of the specified objectOrName widget starting at position x and y. The objectOrName widget is dragged by dx pixels horizontally and by dy pixels vertically.

type(objectOrName, text);

This function types the specified text (as if the user had used the keyboard) into the objectOrName editable widget. If the text is surrounded by angle brackets (<>), it is interpreted as a key combination, e.g "<Ctrl+Return>". The input is case-sensitive, so type(object, "R") is different from type(object, "r"). (For a list of the supported special keys see the nativeType function's documentation.)

uninstallEventHandler(eventName, handlerFunctionName);

This function uninstalls an event handler that has been previously installed using installEventHandler.

6.13.1. GestureBuilder

Objects of this type hold the touch strokes information needed to replay gesture. An instance of this class is returned by readGesture. Strokes are defined by screen coordinates points, pressure and touch size.

For examples of how GestureBuilder objects can be used to manipulate the gesture information, see How to Use the GestureBuilder class (Section 5.7.2).

int GestureBuilder.areaWidth

The width of the area in which this gesture is defined. This will be the device or emulator screen width.

int GestureBuilder.areaHeight

The height of the area in which this gesture is defined. This will be the device or emulator screen height.

All GestureBuilder methods listed in the Gesture creation (Section 6.13.1.1) and Gesture manipulation (Section 6.13.1.2) section, return the GestureBuilder object itself, unless specified differently.

6.13.1.1. Gesture creation

This section lists the methods for manually creating a GestureBuilder object.

GestureBuilder(width, height, unit);

GestureBuilder(xml);

Two constructor functions for creating a GestureBuilder object. The width and height are the target screen size. The unit can be either 0 or 1, meaning respectively in pixels or millimeters. The constants GestureBuilder.Pixel and GestureBuilder.MilliMeter can be used as well.

The second constructor function constructs a GestureBuilder object by passing an string containing XML, which should be in the same format as the recorded gesture files.

Object GestureBuilder.addStroke(x, y);

Object GestureBuilder.addStroke(startTime, x, y);

Object GestureBuilder.addStroke(startTime, x, y, pressure);

Object GestureBuilder.addStroke(startTime, x, y, pressure, size);

Starts a new stroke. The whole movement of one finger or pen from touch down to releasing the screen is called a stroke. The touch down coordinate is (x, y). For the non-first stroke, a time offset can be specified in milliseconds using the startTime argument. Strokes cannot be disjointed in time, at least one finger or pen has to be down during the whole gesture. The maximum simultaneous touches is device dependent.

Finally, the pressure and size are relative messure values for respectively pen or finger pressure and size. These should be between 0.0 and 1.0 and when omitted defaults to 0.25.

Object GestureBuilder.curveTo(duration, controlX, controlY, endX, endy);

Object GestureBuilder.curveTo(duration, control1X, control1Y, control2X, control2Y, endX, endy);

Adds a bézier curve movement to the latest added stroke in duration milliseconds. The curve starts with the end coordinate of the last added movement or, if none added to the stroke, the stroke touch down coordinate. The end coordinate is specified with endX and endY. One or two so called control points can be used.

Object GestureBuilder.lineTo(duration, endX, endy);

Adds a line movement to the latest added stroke in duration milliseconds. The line starts with the end coordinate of the last added movement or, if none added to the stroke, the stroke touch down coordinate. The end coordinate is specified with endX and endY.

Object GestureBuilder.build();

Creates the gesture from the added strokes and movements. After calling this method, no strokes or movements can be added.

6.13.1.2. Gesture manipulation

Object GestureBuilder.accelerate(factor);

Changes stroke speed given a factor. A factor between 0.0 and 1.0 slows down the gesture, above 1.0 will speed it up.

Object GestureBuilder.rotate(degrees);

Object GestureBuilder.rotate(degrees, originX, originY);

Rotates the strokes. The degrees is the agle in degrees in a counter clockwise direction. The originX and originY define the origin of the rotate operation. If omitted, the area center is taken as origin.

Object GestureBuilder.scale(scale);

Object GestureBuilder.scale(scaleX, scaleY);

Object GestureBuilder.scale(scaleX, scaleY, originX, originY);

Changes the size of the strokes. The scaleX is the scale factor in the horizontal direction and scaleY in the vertical direction. The originX and originY define the origin of the scale operation. If omitted, the area center is taken as origin. When also scaleY is omitted, then the scaling is homogeneous in both directions.

Object GestureBuilder.translate(x, y);

Moves the strokes. The x and y specifies the movement. A positive value for x moves the strokes to the right and a positive value for y moves the strokes downwards.