Changing Switch State

Applies to TestComplete 15.70, last modified on December 17, 2024
Simulating Touch Action

To simulate a touch on a switch control, use the Touch action of the Android Switch 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 switch:

JavaScript, JScript

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

  // Obtain the required switch
  var p = Mobile.Device().Process("com.example.myapp");
  var Swtch = p.RootLayout("").Layout("layoutTop").Layout("layout2").Switch("switch1");
  
  // Touch the switch
  Swtch.Touch();
  
  // Post the current switch state to the log
  Log.Message(Swtch.wState);
}

Python

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

  # Obtain the required switch
  p = Mobile.Device().Process("com.example.myapp")
  Swtch = p.RootLayout("").Layout("layoutTop").Layout("layout2").Switch("switch1")
  
  # Touch the switch
  Swtch.Touch()
  
  # Post the current switch state to the log
  Log.Message(Swtch.wState)

VBScript

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

  ' Obtain the required switch
  Set p = Mobile.Device.Process("com.example.myapp")
  Set Swtch = p.RootLayout("").Layout("layoutTop").Layout("layout2").Switch("switch1")
  
  ' Touch the switch
  Swtch.Touch()
  
  ' Post the current switch state to the log
  Call Log.Message(Swtch.wState)
End Sub

DelphiScript

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

  // Obtain the required switch
  p := Mobile.Device.Process('com.example.myapp');
  Swtch := p.RootLayout('').Layout('layoutTop').Layout('layout2').Switch('switch1');
  
  // Touch the switch
  Swtch.Touch();
  
  // Post the current switch state to the log
  Log.Message(Swtch.wState);
end;

C++Script, C#Script

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

  // Obtain the required switch
  var p = Mobile["Device"]["Process"]("com.example.myapp");
  var Swtch = p["RootLayout"]("")["Layout"]("layoutTop")["Layout"]("layout2")["Switch"]("switch1");
  
  // Touch the switch
  Swtch["Touch"]();
  
  // Post the current switch state to the log
  Log["Message"](Swtch["wState"]);
}

Simulating CheckButton Action

To specify a switch 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 it corresponds to the current switch state, CheckButton performs no action. The following example sets a switch to the checked state and posts it to the log:

JavaScript, JScript

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

  // Obtain the required switch
  var p = Mobile.Device().Process("com.example.myapp");
  var Swtch = p.RootLayout("").Layout("layoutTop").Layout("layout2").Switch("switch1");
  
  // Set the switch to "on"
  Swtch.CheckButton(cbChecked);
  
  // Post the current switch state to the log
  Log.Message(Swtch.wState);
}

Python

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

  # Obtain the required switch
  p = Mobile.Device().Process("com.example.myapp")
  Swtch = p.RootLayout("").Layout("layoutTop").Layout("layout2").Switch("switch1")
  
  # Set the switch to "on"
  Swtch.CheckButton(cbChecked)
  
  # Post the current switch state to the log
  Log.Message(Swtch.wState)

VBScript

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

  ' Obtain the required switch
  Set p = Mobile.Device.Process("com.example.myapp")
  Set Swtch = p.RootLayout("").Layout("layoutTop").Layout("layout2").Switch("switch1")
  
  ' Set the switch to "on"
  Swtch.CheckButton(cbChecked)
  
  ' Post the current switch state to the log
  Call Log.Message(Swtch.wState)
End Sub

DelphiScript

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

  // Obtain the required switch
  p := Mobile.Device.Process('com.example.myapp');
  Swtch := p.RootLayout('').Layout('layoutTop').Layout('layout2').Switch('switch1');
  
  // Set the switch to "on"
  Swtch.CheckButton(cbChecked);
  
  // Post the current switch state to the log
  Log.Message(Swtch.wState);
end;

C++Script, C#Script

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

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

Assigning wState Property

To assign a value to a switch, use the wState property. Assigning of a value is not posted to the log. Here is an example of how to modify the state of the switch:

JavaScript, JScript

function Test()
{
  var p, Swtch;

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

  // Obtain the required switch
  p = Mobile.Device().Process("com.example.myapp");
  Swtch = p.RootLayout("").Layout("layoutTop").Layout("layout2").Switch("switch1");
  
  // Set the switch to "on"
  Swtch.wState = cbChecked;
  
  // Post the current switch state to the log
  Log.Message(Swtch.wState);

  // Set the switch to "off"
  Swtch.wState = cbUnchecked;
}

Python

def Test():

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

  # Obtain the required switch
  p = Mobile.Device().Process("com.example.myapp")
  Swtch = p.RootLayout("").Layout("layoutTop").Layout("layout2").Switch("switch1")
  
  # Set the switch to "on"
  Swtch.wState = cbChecked
  
  # Post the current switch state to the log
  Log.Message(Swtch.wState)

  # Set the switch to "off"
  Swtch.wState = cbUnchecked

VBScript

Sub Test()
  Dim p, Swtch

  ' Select the Android Device
  Call Mobile.SetCurrent("MyDevice")

  ' Obtain the required switch
  Set p = Mobile.Device.Process("com.example.myapp")
  Set Swtch = p.RootLayout("").Layout("layoutTop").Layout("layout2").Switch("switch1")
  
  ' Set the switch to "on"
  Swtch.wState = cbChecked
  
  ' Post the current switch state to the log
  Call Log.Message(Swtch.wState)

  ' Set the switch to "off"
  Swtch.wState = cbUnchecked
End Sub

DelphiScript

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

  // Obtain the required switch
  p := Mobile.Device.Process('com.example.myapp');
  Swtch := p.RootLayout('').Layout('layoutTop').Layout('layout2').Switch('switch1');
  
  // Set the switch to "on"
  Swtch.wState := cbChecked;
  
  // Post the current switch state to the log
  Log.Message(Swtch.wState);

  // Set the switch to "off"
  Swtch.wState := cbUnchecked;
end;

C++Script, C#Script

function Test()
{
  var p, Swtch

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

  // Obtain the required switch
  p = Mobile["Device"]["Process"]("com.example.myapp");
  Swtch = p["RootLayout"]("")["Layout"]("layoutTop")["Layout"]("layout2")["Switch"]("switch1");
  
  // Set the switch to "on"
  Swtch["wState"] = cbChecked;
  
  // Post the current switch state to the log
  Log["Message"](Swtch.wState);

  // Set the switch to "off"
  Swtch["wState"] = cbUnchecked;
}

Simulating Actions From Keyword Tests

This topic explains how to touch the switch 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 Switch Controls
Determining a Switch's State
CheckButton Action (Mobile Controls)
Touch Action (Mobile Objects)
wState Property (Mobile Controls)

Highlight search results