Changing the Switch State

Applies to TestComplete 14.40, last modified on April 22, 2021
Changing the Current State

To change the current state of a switch control, simulate a single touch action over the control. This will explicitly change the initial state of the control to the opposite state: if the switch was "on" it will become "off" and vice versa.

To simulate a touch on a switch control, use the Touch action of the iOS Switch object that TestComplete associates with that control. The following example demonstrates how to simulate a touch on a switch:

JavaScript, JScript

function Test()
{
  // Select the mobile device
  Mobile.SetCurrent("iPhone");
  // Obtain the switch object
  var p = Mobile.Device().Process("SampleApp");
  var Switch = p.Window(0).Switch(0);
  
  // Touch the switch
  Switch.Touch();
}

Python

def Test():
  # Select the mobile device
  Mobile.SetCurrent("iPhone")
  # Obtain the switch object
  p = Mobile.Device().Process("SampleApp")
  Switch = p.Window(0).Switch(0)
  
  # Touch the switch
  Switch.Touch()

VBScript

Sub Test()
  Dim p, Switch
  ' Select the mobile device
  Mobile.SetCurrent("iPhone")
  ' Obtain the switch object
  Set p = Mobile.Device.Process("SampleApp")
  Set Switch = p.Window(0).Switch(0)
  
  ' Touch the switch
  Switch.Touch()
End Sub

DelphiScript

procedure Test();
var
  p, Switch;
begin
  // Select the mobile device
  Mobile.SetCurrent('iPhone');
  // Obtain the switch object
  p := Mobile.Device.Process('SampleApp');
  Switch := p.Window(0).Switch(0);
  
  // Touch the switch
  Switch.Touch();
end;

C++Script, C#Script

function Test()
{
  // Select the mobile device
  Mobile["SetCurrent"]("iPhone");
  // Obtain the switch object
  var p = Mobile["Device"].Process("SampleApp");
  var Switch = p["Window"](0)["Switch"](0);
  
  // Touch the switch
  Switch["Touch"]();
}

Setting the Desired State

To set a switch control to the desired state, use the Switch action. This action compares the current state of the control and the desired state specified via the input parameter: if the state mismatches, the action performs a single touch over the control; otherwise, it performs no action. The following example sets a switch to the checked state and posts information about the state to the log:

JavaScript, JScript

function Test()
{
  // Select the mobile device
  Mobile.SetCurrent("iPhone");
  // Obtain the switch object
  var p = Mobile.Device().Process("SampleApp");
  var Switch = p.Window(0).Switch(0);
  
  // Set the switch to the "on" state
  Switch.Switch(true);
}

Python

def Test():
  # Select the mobile device
  Mobile.SetCurrent("iPhone")
  # Obtain the switch object
  p = Mobile.Device().Process("SampleApp")
  Switch = p.Window(0).Switch(0)
  
  # Set the switch to the "on" state
  Switch.Switch(True)

VBScript

Sub Test()
  Dim p, Switch
  ' Select the mobile device
  Mobile.SetCurrent("iPhone")
  ' Obtain the switch object
  Set p = Mobile.Device.Process("SampleApp")
  Set Switch = p.Window(0).Switch(0)
  
  ' Set the switch to the "on" state
  Switch.Switch(True)
End Sub

DelphiScript

procedure Test();
var
  p, Switch;
begin
  // Select the mobile device
  Mobile.SetCurrent('iPhone');
  // Obtain the switch object
  p := Mobile.Device.Process('SampleApp');
  Switch := p.Window(0).Switch(0);
  
  // Set the switch to the "on" state
  Switch.Switch(true);
end;

C++Script, C#Script

function Test()
{
  // Select the mobile device
  Mobile["SetCurrent"]("iPhone");
  // Obtain the switch object
  var p = Mobile["Device"].Process("SampleApp");
  var Switch = p["Window"](0)["Switch"](0);
  
  // Set the switch to the "on" state
  Switch["Switch"](true);
}

Assigning a Value to the wState Property

To assign a switch value directly, use the wState property. Assigning the wState value is reflected in the test log. Below is an example of how to modify the state of a switch:

JavaScript, JScript

function Test()
{
  // Select the mobile device
  Mobile.SetCurrent("iPhone");
  // Obtain the switch object
  var p = Mobile.Device().Process("SampleApp");
  var Switch = p.Window(0).Switch(0);
  
  // Set the switch to the "on" state
  Switch.wState = true;
}

Python

def Test():
  # Select the mobile device
  Mobile.SetCurrent("iPhone")
  # Obtain the switch object
  p = Mobile.Device().Process("SampleApp")
  Switch = p.Window(0).Switch(0)
  
  # Set the switch to the "on" state
  Switch.wState = True

VBScript

Sub Test()
  Dim p, Switch
  ' Select the mobile device
  Mobile.SetCurrent("iPhone")
  ' Obtain the switch object
  Set p = Mobile.Device.Process("SampleApp")
  Set Switch = p.Window(0).Switch(0)
  
  ' Set the switch to the "on" state
  Switch.wState = True
End Sub

DelphiScript

procedure Test();
var
  p, Switch;
begin
  // Select the mobile device
  Mobile.SetCurrent('iPhone');
  // Obtain the switch object
  p := Mobile.Device.Process('SampleApp');
  Switch := p.Window(0).Switch(0);
  
  // Set the switch to the "on" state
  Switch.wState := true;
end;

C++Script, C#Script

function Test()
{
  // Select the mobile device
  Mobile["SetCurrent"]("iPhone");
  // Obtain the switch object
  var p = Mobile["Device"].Process("SampleApp");
  var Switch = p["Window"](0)["Switch"](0);
  
  // Set the switch to the "on" state
  Switch["wState"] = true;
}

Changing the Switch State in Keyword Tests

You can change the state of a switch in keyword tests by using the On-Screen Action or Call Object Method operation. To do this, call the Touch or Switch action, or change the value of the wState property.

See Also

Working With iOS Switch Controls
Determining a Switch's State
Switch Action (Specific to iOS Switch Controls)
Touch Action (Mobile Objects)
wState Property (Mobile Controls)

Highlight search results