Simulating Physical Button Events

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

An Android device may have a number of physical buttons: Power, Volume, Home, Recent, Dial, Camera, and so on. Depending on the device type and model, the set of available buttons may vary greatly: from a single Power button to a full-fledged QWERTY keyboard. Yet, the majority of Android devices have from one to six physical buttons.

This topic describes how you can simulate physical button events with TestComplete.

Note: Depending on the device and on the activities running on it, the device may react to button pressing in a different manner or it may not react at all.

Simulating Actions Over Physical Buttons

With TestComplete, you can simulate actions over various physical buttons on your mobile device. It may be useful to:

  • Simulate button events on Android devices like netbooks, media players, set-top boxes and other devices having different sets of physical buttons.

  • Initiate specific actions or activities that are performed upon pressing certain physical buttons.

To simulate pressing any physical button, you can call the Device.PressButton method. The parameters of the method specify the physical button to be emulated (for the full list of buttons available for emulation, see Mobile Key Constants) and the type of action to simulate (press button, release button or press and release button). Additionally, to simulate pressing the Back and Home buttons, you can use the Device.PressBack and Device.PressHome methods respectively.

Note: These methods are asynchronous. This means that TestComplete does not pause the test when they are executed. For more information, see Possible Issues.

Simulating Physical Button Events in Keyword Tests

To simulate pressing physical buttons in keyword tests, call the Device.PressButton method with the desired parameters by using the Call Object Method, Run Code Snippet or Run Script Routine operations:

Simulating pressing physical buttons in a keyword test
Note: These operations are asynchronous. This means that TestComplete does not pause the test when they are executed. For more information, see Possible Issues.

Simulating Physical Button Events in Scripts

To simulate pressing physical buttons in scripts, call the Device.PressButton method. The script below demonstrates how to launch the Calculator application by pressing a dedicated button and press several Num Pad buttons:

JavaScript, JScript

function SimulatingPhysicalButtons()
{
  Mobile.SetCurrent("Nexus 7");
  Mobile.Device().PressButton(mbkCalculator);
  Delay(200);
  Mobile.Device().PressButton(mbkNumPad2);
  Mobile.Device().PressButton(mbkNumPadAdd);
  Mobile.Device().PressButton(mbkNumPad8);
  Mobile.Device().PressButton(mbkNumPadEquals);
}

Python

def SimulatingPhysicalButtons():
  Mobile.SetCurrent("Nexus 7");
  Mobile.Device().PressButton(mbkCalculator);
  Delay(200); 
  Mobile.Device().PressButton(mbkNumPad2);
  Mobile.Device().PressButton(mbkNumPadAdd);
  Mobile.Device().PressButton(mbkNumPad8);
  Mobile.Device().PressButton(mbkNumPadEquals);

VBScript

Sub SimulatingPhysicalButtons()
  Call Mobile.SetCurrent("Nexus 7")
  Call Mobile.Device.PressButton(mbkCalculator)
  Call Delay(200)
  Call Mobile.Device.PressButton(mbkNumPad2)
  Call Mobile.Device.PressButton(mbkNumPadAdd)
  Call Mobile.Device.PressButton(mbkNumPad8)
  Call Mobile.Device.PressButton(mbkNumPadEquals)
End Sub

DelphiScript

procedure SimulatingPhysicalButtons;
begin
  Mobile.SetCurrent('Nexus 7');
  Mobile.Device.PressButton(mbkCalculator);
  Delay(200);
  Mobile.Device.PressButton(mbkNumPad2);
  Mobile.Device.PressButton(mbkNumPadAdd);
  Mobile.Device.PressButton(mbkNumPad8);
  Mobile.Device.PressButton(mbkNumPadEquals);
end;

C++Script, C#Script

function SimulatingPhysicalButtons()
{
  Mobile["SetCurrent"]("Nexus 7");
  Mobile["Device"]["PressButton"](mbkCalculator);
  Delay(200);
  Mobile["Device"]["PressButton"](mbkNumPad2);
  Mobile["Device"]["PressButton"](mbkNumPadAdd);
  Mobile["Device"]["PressButton"](mbkNumPad8);
  Mobile["Device"]["PressButton"](mbkNumPadEquals);
}

Note: To simulate pressing the Back and Home buttons, TestComplete also provides the additional Device.PressBack and Device.PressHome methods respectively.

Recording Actions Over Physical Buttons

Currently, TestComplete cannot record pressing of physical buttons on the connected mobile device. To record a physical button event, you need to press one of the emulated buttons on the Mobile Screen window.

The buttons of the Mobile Screen footer represent the following most frequently used physical buttons: Back, Home, Recent, Volume Down, Volume Up and On/Off. TestComplete records pressing these buttons as calls to the Device.PressButton method.

Note: For pressing the Back and Home buttons, TestComplete records the calls to the Device.PressBack and Device.PressHome methods respectively.

A mobile device may emulate physical buttons (Back, Home, Recent, etc.) on its touch screen, so the Mobile Screen window will contain two sets of emulated physical buttons: one set on the device’s screen and another set in the window footer.

We recommend that you use buttons on the Mobile Screen footer to record actions over the device’s physical buttons. Otherwise, pressing screen-rendered buttons is interpreted as on-screen actions and is recorded as calls to the Touch method of the Device object.

See Also

Simulating User Actions Over Android Devices
Mobile Key Constants
Recording Gestures (Multi-Touch Events)
Simulating Basic Touch Actions Over Android Devices
Simulating Text Input on Android Devices

Highlight search results