Selecting Spinner Items

Applies to TestComplete 15.0, last modified on November 17, 2021

When working with a spinner control, you select items. A spinner control can be displayed as a drop-down menu or as a dialog window. If the tested application uses standard Android spinners, you can use specific properties and methods, provided by the Android Spinner object.

Using Internal Methods

The TouchItem and LongTouchItem actions allow you to simulate a touch or a long touch on any item specified by its index or caption. The following example simulates a touch on the item with the caption Item1 and a long touch on the third item in the spinner:

JavaScript, JScript

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

  // Obtain an application
  var app = Mobile.Device().Process("com.example.myapp");

  // Obtain a spinner
  var spinnerObj = app.RootLayout("").Layout("layout1").Spinner("spinner1");
 
  // Simulate a touch on an Item1
  spinnerObj.TouchItem("Item1");
 
  // Simulate a touch on an Item1
  spinnerObj.LongTouchItem(2);
}

Python

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

  # Obtain an application
  app = Mobile.Device().Process("com.example.myapp")

  # Obtain a spinner
  spinnerObj = app.RootLayout("").Layout("layout1").Spinner("spinner1")
 
  # Simulate a touch on an Item1
  spinnerObj.TouchItem("Item1")
 
  # Simulate a touch on an Item1
  spinnerObj.LongTouchItem(2)

VBScript

Sub Test()
  Dim app, spinnerObj

  ' Select an Android device
  Mobile.SetCurrent("MyDevice")

  ' Obtain an application
  Set app = Mobile.Device.Process("com.example.myapp")

  ' Obtain a spinner
 Set spinnerObj = app.RootLayout("").Layout("layout1").Spinner("spinner1")
 
 ' Simulate a touch over an Item1
 spinnerObj.TouchItem("Item1")
 
 ' Simulate a long touch over an Item1
 spinnerObj.LongTouchItem(2)
 
End Sub

DelphiScript

procedure Test();
var
  app, spinnerObj: OleVariant;
begin
  // Select an Android device
  Mobile.SetCurrent('MyDevice');

  // Obtain an application
  app := Mobile.Device.Process('com.example.myapp');

  // Obtain a spinner
  spinnerObj := app.RootLayout('').Layout('layout1').Spinner('spinner1');
 
  // Simulate a touch on an Item1
  spinnerObj.TouchItem('Item1');
 
  // Simulate a long touch on an Item1
  spinnerObj.LongTouchItem(2);
end;

C++Script, C#Script

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

  // Obtain an application
  var app = Mobile["Device"]["Process"]("com.example.myapp");

  // Obtain a spinner
  var spinnerObj = app["RootLayout"]("")["Layout"]("layout1")["Spinner"]("spinner1");

  // Simulate a touch on an Item1
  spinnerObj["TouchItem"]("Item1");

  // Simulate a long touch on an Item1
  spinnerObj["LongTouchItem"](2);
 
}

If an item's captions change according to the current context, it may be useful to specify items only by constant parts of their names. To specify arbitrary parts in item names, TestComplete lets you use wildcard characters (* and ?) or regular expressions. The asterisk (*) corresponds to a string of any length (including an empty string), the question mark corresponds to any single character (including none). To specify more complicated parts of a caption, use regular expressions.

If you wish to specify an asterisk (*) as part of an item’s caption, you should double it, because otherwise, it will be treated as a wildcard. Here is an example of how to select any item:

spinnerObj.TouchItem("*")

Simulating Actions From Keyword Tests

This topic explains how to select items of the spinner 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 Spinner Controls
Android Spinner Support
Getting Spinner Items' Text

Highlight search results