Touching a Button

Applies to TestComplete 14.10, last modified on June 5, 2019
Simulating Touch or TouchButton Actions

To touch a button, use the Touch or TouchButton actions of the Android Button object that TestComplete associates with that control. These actions simulate a single short touch on the button. The following example demonstrates how to perform a touch via these actions:

JavaScript, JScript

function Test()
{
  // Select the Android device
  Mobile.SetCurrent("MyDevice");

  // Obtain the button object
  var p = Mobile.Device().Process("com.example.myapp");
  var Button = p.RootLayout("").Layout("layoutTop").Layout("layout1").Button("button1");
  
  // Touch the button
  Button.Touch();
}

Python

def Test():
  # Select the Android device
  Mobile.SetCurrent("MyDevice")

  # Obtain the button object
  p = Mobile.Device().Process("com.example.myapp")
  Button = p.RootLayout("").Layout("layoutTop").Layout("layout1").Button("button1")
  
  # Touch the button
  Button.Touch()

VBScript

Sub Test()
  ' Select the Android device
  Mobile.SetCurrent("MyDevice")

  ' Obtain the button object
  Set p = Mobile.Device.Process("com.example.myapp")
  Set Button = p.RootLayout("").Layout("layoutTop").Layout("layout1").Button("button1")
  
  ' Touch the button
  Call Button.Touch()
End Sub

DelphiScript

procedure Test();
var
  p, Button : OleVariant;
begin
  // Select the Android device
  Mobile.SetCurrent('MyDevice');

  // Obtain the button object
  p := Mobile.Device.Process('com.example.myapp');
  Button := p.RootLayout('').Layout('layoutTop').Layout('layout1').Button('button1');
  
  // Touch the button
  Button.Touch();
end;

C++Script, C#Script

function Test()
{
  // Select the Android device
  Mobile["SetCurrent"]("MyDevice");

  // Obtain the button object
  var p = Mobile["Device"]["Process"]("com.example.myapp");
  var Button = p["RootLayout"]("")["Layout"]("layoutTop")["Layout"]("layout1")["Button"]("button1");
  
  // Touch the button
  Button["Touch"]();
}

Note: You can only touch a button if it is enabled. To get information on how to determine a button’s state, see the Checking a Button's State topic.
Simulating LongTouch or LongTouchButton Actions

To long touch a button, use the LongTouch or LongTouchButton actions. These actions simulate a single long touch on the button. The following example demonstrates how to perform a long touch via these actions:

JavaScript, JScript

function Test()
{
  // Select the Android device
  Mobile.SetCurrent("MyDevice");

  // Obtain the button object
  var p = Mobile.Device().Process("com.example.myapp");
  var Button = p.RootLayout("").Layout("layoutTop").Layout("layout1").Button("button1");
  
  // Longtouch the button
  Button.LongTouch();
}

Python

def Test():
  # Select the Android device
  Mobile.SetCurrent("MyDevice")

  # Obtain the button object
  p = Mobile.Device().Process("com.example.myapp")
  Button = p.RootLayout("").Layout("layoutTop").Layout("layout1").Button("button1")
  
  # Longtouch the button
  Button.LongTouch()

VBScript

Sub Test()
  ' Select the Android device
  Mobile.SetCurrent("MyDevice")

  ' Obtain the button object
  Set p = Mobile.Device.Process("com.example.myapp")
  Set Button = p.RootLayout("").Layout("layoutTop").Layout("layout1").Button("button1")
  
  ' Longtouch the button
  Call Button.LongTouch()
End Sub

DelphiScript

procedure Test();
var  p, Button : OleVariant;
begin
  // Select the Android device
  Mobile.SetCurrent('MyDevice');

  // Obtain the button object
  p := Mobile.Device.Process('com.example.myapp');
  Button := p.RootLayout('').Layout('layoutTop').Layout('layout1').Button('button1');
  
  // Longtouch the button
  Button.LongTouch();
end;

C++Script, C#Script

function Test()
{
  // Select the Android device
  Mobile["SetCurrent"]("MyDevice");

  // Obtain the button object
  var p = Mobile["Device"]["Process"]("com.example.myapp");
  var Button = p["RootLayout"]("")["Layout"]("layoutTop")["Layout"]("layout1")["Button"]("button1");
  
  // Longtouch the button
  Button["LongTouch"]();
}

Simulating Actions From Keyword Tests

This topic explains how to simulate the touch on the button control in scripts. You can use the described actions in keyword tests too. To do this, use the On-Screen Action or the Call Object Method operations.

See Also

Working With Android Button Controls
Checking a Button's State
Touch Action (Mobile Objects)
TouchButton Action (Mobile Controls)

Highlight search results