Changing Toggle Button State

Applies to TestComplete 15.62, last modified on March 19, 2024
Simulating Touch Action

To touch a toggle button, use the Touch action of the Android ToggleButton object that TestComplete associates with that control. This action performs a single short touch on the control. The following example shows how to simulate a touch on the toggle button control:

JavaScript, JScript

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

  // Obtain the required ToggleButton
  var p = Mobile.Device().Process("com.example.myapp");
  var Toggle = p.RootLayout("").Layout("layoutTop").Layout("layout1").ToggleButton("toggle1");
  
  // Touch the ToggleButton
  Toggle.Touch();
  
  // Post the current ToggleButton state to the log
  Log.Message(Toggle.wState);
}

Python

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

  # Obtain the required ToggleButton
  p = Mobile.Device().Process("com.example.myapp")
  Toggle = p.RootLayout("").Layout("layoutTop").Layout("layout1").ToggleButton("toggle1")
  
  # Touch the ToggleButton
  Toggle.Touch()
  
  # Post the current ToggleButton state to the log
  Log.Message(Toggle.wState)

VBScript

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

  ' Obtain the required ToggleButton
  Set p = Mobile.Device.Process("com.example.myapp")
  Set Toggle = p.RootLayout("").Layout("layoutTop").Layout("layout1").ToggleButton("toggle1")
  
  ' Touch the ToggleButton
  Toggle.Touch()
  
  ' Post the current ToggleButton state to the log
  Call Log.Message(Toggle.wState)
End Sub

DelphiScript

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

  // Obtain the required ToggleButton
  p := Mobile.Device.Process('com.example.myapp');
  Toggle := p.RootLayout('').Layout('layoutTop').Layout('layout1').ToggleButton('toggle1');
  
  // Touch the ToggleButton
  Toggle.Touch();
  
  // Post the current ToggleButton state to the log
  Log.Message(Toggle.wState);
end;

C++Script, C#Script

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

  // Obtain the required ToggleButton
  var p = Mobile["Device"]["Process"]("com.example.myapp");
  var Toggle = p["RootLayout"]("")["Layout"]("layoutTop")["Layout"]("layout1")["ToggleButton"]("toggle1");
  
  // Touch the ToggleButton
  Toggle["Touch"]();
  
  // Post the current ToggleButton state to the log
  Log["Message"](Toggle["wState"]);
}

Simulating CheckButton Action

To specify a toggle button state after the touch, use the CheckButton action. This action performs a series of touches, which put the control to the state, specified by the State parameter. If the State parameter corresponds to the current toggle button state, CheckButton performs no action. The following example sets a toggle button to the checked state:

JavaScript, JScript

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

  // Obtain the required ToggleButton
  var p = Mobile.Device().Process("com.example.myapp");
  var Toggle = p.RootLayout("").Layout("layoutTop").Layout("layout1").ToggleButton("toggle1");
  
  // Set the ToggleButton to "on"
  Toggle.CheckButton(cbChecked);
  
  // Post the current ToggleButton state to the log
  Log.Message(Toggle.wState);
}

Python

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

  # Obtain the required ToggleButton
  p = Mobile.Device().Process("com.example.myapp")
  Toggle = p.RootLayout("").Layout("layoutTop").Layout("layout1").ToggleButton("toggle1")
  
  # Set the ToggleButton to "on"
  Toggle.CheckButton(cbChecked)
  
  # Post the current ToggleButton state to the log
  Log.Message(Toggle.wState)

VBScript

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

  ' Obtain the required ToggleButton
  Set p = Mobile.Device.Process("com.example.myapp")
  Set Toggle = p.RootLayout("").Layout("layoutTop").Layout("layout1").ToggleButton("toggle1")
  
  ' Set the ToggleButton to "on"
  Toggle.CheckButton(cbChecked)
  
  ' Post the current ToggleButton state to the log
  Call Log.Message(Toggle.wState)
End Sub

DelphiScript

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

  // Obtain the required ToggleButton
  p := Mobile.Device.Process('com.example.myapp');
  Toggle := p.RootLayout('').Layout('layoutTop').Layout('layout1').ToggleButton('toggle1');
  
  // Set the ToggleButton to "on"
  Toggle.CheckButton(cbChecked);
  
  // Post the current ToggleButton state to the log
  Log.Message(Toggle.wState);
end;

C++Script, C#Script

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

  // Obtain the required ToggleButton
  var p = Mobile["Device"]["Process"]("com.example.myapp");
  var Toggle = p["RootLayout"]("")["Layout"]("layoutTop")["Layout"]("layout1")["ToggleButton"]("toggle1");
  
  // Set the ToggleButton to "on"
  Toggle["CheckButton"](cbChecked);
  
  // Post the current ToggleButton state to the log
  Log["Message"](Toggle["wState"]);
}

Assigning wState Property

To set the state of a toggle button, assign the needed value to the wState property. Assigning of a value is not posted to the log. The following example modifies the state of a toggle button:

JavaScript, JScript

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

  // Obtain the required toggle button
  var p = Mobile.Device().Process("com.example.myapp");
  var Toggle = p.RootLayout("").Layout("layoutTop").Layout("layout1").ToggleButton("toggle1");
  
  // Set the toggle button to "on"
  Toggle.wState = cbChecked;
  
  // Post the current toggle button state to the log
  Log.Message(Toggle.wState);

  // Set the ToggleButton to "off"
  Toggle.wState = cbUnchecked;
}

Python

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

  # Obtain the required toggle button
  p = Mobile.Device().Process("com.example.myapp")
  Toggle = p.RootLayout("").Layout("layoutTop").Layout("layout1").ToggleButton("toggle1")
  
  # Set the toggle button to "on"
  Toggle.wState = cbChecked
  
  # Post the current toggle button state to the log
  Log.Message(Toggle.wState)

  # Set the ToggleButton to "off"
  Toggle.wState = cbUnchecked

VBScript

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

  ' Obtain the required toggle button
  Set p = Mobile.Device.Process("com.example.myapp")
  Set Toggle = p.RootLayout("").Layout("layoutTop").Layout("layout1").ToggleButton("toggle1")
  
  ' Set the toggle button to "on"
  Toggle.wState = cbChecked
  
  ' Post the current toggle button state to the log
  Call Log.Message(Toggle.wState)

  ' Set the ToggleButton to "off"
  Toggle.wState = cbUnchecked
End Sub

DelphiScript

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

  // Obtain the required toggle button
  p := Mobile.Device.Process('com.example.myapp');
  Toggle := p.RootLayout('').Layout('layoutTop').Layout('layout1').ToggleButton('toggle1');
  
  // Set the toggle button to "on"
  Toggle.wState := cbChecked;
  
  // Post the current toggle button state to the log
  Log.Message(Toggle.wState);

  // Set the ToggleButton to "off"
  Toggle.wState := cbUnchecked;
end;

C++Script, C#Script

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

  // Obtain the required toggle button
  p = Mobile["Device"]["Process"]("com.example.myapp");
  Toggle = p["RootLayout"]("")["Layout"]("layoutTop")["Layout"]("layout1")["ToggleButton"]("toggle1");
  
  // Set the toggle button to "on"
  Toggle["wState"] = cbChecked;
  
  // Post the current ToggleButton state to the log
  Log["Message"](Toggle.wState);

  // Set the toggle button to "off"
  Toggle["wState"] = cbUnchecked;
}

Simulating Actions From Keyword Tests

This topic explains how to change the state of the toggle button control in scripts. You can use the described methods and properties in keyword tests too. To do this, use the On-Screen Action or the Call Object Method operations.

See Also

Working With Android Toggle Button Controls
Determining a Toggle Button's State
CheckButton Action (Mobile Controls)
Touch Action (Mobile Objects)
wState Property (Mobile Controls)

Highlight search results