Getting Spinner Items' Text

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

When testing an application that uses spinner controls, you may need to obtain spinner items’ text. This topic describes how to do this.

Using Internal Methods

The Android Spinner object that corresponds to the tested spinner control provides a set of properties that allow you to get the text of the spinner control's individual items.

  • You can obtain the text of any item specified by its index via the wItem property. The following code snippet demonstrates how to get an item's text:

    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");
     
      // Obtain the text of the first item
      var text = spinnerObj.wItem(0);
     
      // Post the item's text to the log
      Log.Message(text);
    }

    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")
     
      # Obtain the text of the first item
      text = spinnerObj.wItem[0]
     
      # Post the item's text to the log
      Log.Message(text)

    VBScript

    Sub Test()
      Dim app, spinnerObj, text

      ' 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")
     
      ' Obtain the text of the first item
      text = spinnerObj.wItem(0)
     
      ' Post the item's text to the log
      Log.Message(text)
    End Sub

    DelphiScript

    procedure Test();
    var
      app, spinnerObj, text: 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');
     
      // Obtain the text of the first item
      text := spinnerObj.wItem(0);
     
      // Post the item's text to the log
      Log.Message(text);
    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");
     
      // Obtain the text of the first item
      var text = spinnerObj["wItem"](0);
     
      // Post the item's text to the log
      Log["Message"](text);
    }

  • When you need to get a text of the currently selected item, you can determine its index via the wSelectedItem property and then obtain the item's text via the wItem property. Here is an example:

    JavaScript, JScript

    function Main()
    {
      // 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");
        
      // Obtain the selected item's text by its index
      var ItemIndex = spinnerObj.wSelectedItem;
      var SelItem = ComboBox.wItem(ItemIndex);
      Log.Message(SelItem);
    }

    Python

    def Main(): 
      # 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")
        
      # Obtain the selected item's text by its index
      ItemIndex = spinnerObj.wSelectedItem
      SelItem = ComboBox.wItem[ItemIndex]
      Log.Message(SelItem)

    VBScript

    Sub Main
      Dim app, spinnerObj, ItemIndex, SelItem

      ' 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")
        
      ' Obtain the selected item's text by its index
      ItemIndex = spinnerObj.wSelectedItem
      SelItem = spinnerObj.wItem(ItemIndex)
      Log.Message(SelItem)
    End Sub

    DelphiScript

    procedure Main();
    var
      app, spinnerObj, ItemIndex, SelItem: 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');
        
      // Obtain the selected item's text by its index
      ItemIndex := spinnerObj.wSelectedItem;
      SelItem := spinnerObj.wItem(ItemIndex);
      Log.Message(SelItem);
    end;

    C++Script, C#Script

    function Main()
    {
      // 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");
        
      // Obtain the selected item's text by its index
      var ItemIndex = spinnerObj["wSelectedItem"];
      var SelItem = spinnerObj["wItem"](ItemIndex);
      Log["Message"](SelItem);
    }

  • If you want to get the text of all the items displayed in the spinner control, you can determine the number of items using the wItemCount property and then iterate through them in a loop:

    JavaScript, JScript

    function Main()
    {
      // 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");
      
      // Obtain the item's count
      var count = spinnerObj.wItemCount;
      
      // Obtain the text of each item and post it to the log
      for (i = 0; i < count; i++)
      {
        TextItem = spinnerObj.wItem(i);
        Log.Message(TextItem);
      }
    }

    Python

    def Main():
      # 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")
      
      # Obtain the item's count
      count = spinnerObj.wItemCount
      
      # Obtain the text of each item and post it to the log 
      for i in range(0, count-1):
        TextItem = spinnerObj.wItem[i]
        Log.Message(TextItem)

    VBScript

    Sub Main
      Dim app, spinnerObj, count, TextItem

      ' 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")
      
      ' Obtain the item's count
      count = spinnerObj.wItemCount
      
      ' Obtain the text of each item and post it to the log
      For i = 0 To count-1
        TextItem = spinnerObj.wItem(i)
        Log.Message(TextItem)
      Next
    End Sub

    DelphiScript

    procedure Main();
    var
      app, spinnerObj, ItemIndex, SelItem: 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');
      
      // Obtain the item's count
      count := spinnerObj.wItemCount;
      
      // Obtain the text of each item and post it to the log
      for i := 0 to count -1 do
      begin
        TextItem := spinnerObj.wItem(i);
        Log.Message(TextItem);
      end;
    end;

    C++Script, C#Script

    function Main()
    {
      // 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");
      
      // Obtain the item's count
      var count = spinnerObj["wItemCount"];
      
      // Obtain the text of each item and post it to the log
      for (i = 0; i < count; i++)
      {
        TextItem = spinnerObj["wItem"](i);
        Log["Message"](TextItem);
      }
    }

Simulating Actions From Keyword Tests

This topic explains how to get item’s text of the spinner control in scripts. You can use the described properties 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
wItem Property (Mobile Controls)
wItemCount Property (Mobile Controls)
wSelectedItem Property (Mobile Controls)

Highlight search results