Working With List Box Controls in Web Applications

Applies to TestComplete 15.45, last modified on December 01, 2022

TestComplete recognizes standard <select> elements with multiple selection support in web applications and on web pages as Web List Box objects. It can also recognize list box controls implemented with third-party libraries and frameworks.

In your tests, you can work with these elements using methods and properties of the Web List Box object. In addition, you can access attributes of the underlying web element and properties and methods provided to the object by a web browser in which the tested web page or web application is running.

Select single item of list box

In your tests, you often have to simulate selecting an item of a list box control on a web page. To select a single item, you can use the ClickItem and SelectItem methods.

The samples below show how to select a list box item by using the ClickItem method, in keyword tests and in script tests respectively.

In keyword tests

  1. Add the On-Screen Action operation to your keyword test. TestComplete will show the Operation Parameters wizard.

  2. In the wizard, specify the list box whose item you want to select. You can do it by pointing to the list box on the web page.

  3. Select the list box object’s method to call. To simulate selecting an item, use the ClickItem method.

  4. Specify either the index of the item to select or its caption.

    Note: The index must be zero-based.

  5. Click Finish to add the operation and close the wizard.

Selecting an item of a list box

Click the image to enlarge it.

In script tests

To simulate selecting items of a list box, use the WebListBoxObj.ClickItem(Item) method.

The script sample below shows how to simulate selecting a list box item on a web page:

Web (Cross-Platform)

JavaScript, JScript

function Sample_SelectItem()
{
  var url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html";
  Browsers.Item(btChrome).Run(url);

  var lb = Sys.Browser().Page(url).FindElement("#multi");

  // Select the "Carrot" item of the list box
  lb.ClickItem("Carrot");
}

Python

def Sample_SelectItem():
  url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html"
  Browsers.Item[btChrome].Run(url)

  lb = Sys.Browser().Page(url).FindElement("#multi")

  # Select the item "Carrot" of the list box
  lb.ClickItem("Carrot")

VBScript

Sub Sample_SelectItem()
  url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html"
  Browsers.Item(btChrome).Run(url)

  Set lb = Sys.Browser().Page(url).FindElement("#multi")

  ' Select the "Carrot" item of the list box
  lb.ClickItem("Carrot")
End Sub

DelphiScript

procedure Sample_SelectItem();
var url, lb;
begin  url := 'https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html';
  Browsers.Item(btChrome).Run(url);

  lb := Sys.Browser().Page(url).FindElement('#multi');

  // Select the "Carrot" item of the list box
  lb.ClickItem('Carrot');
end;

C++Script, C#Script

function Sample_SelectItem()
{
  var url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html";
  Browsers["Item"](btChrome)["Run"](url);

  var lb = Sys["Browser"]()["Page"](url)["FindElement"]("#multi");

  // Select the "Carrot" item of the list box
  lb["ClickItem"]("Carrot");
}

Web (Classic)

JavaScript, JScript

function Sample_SelectItem()
{
  var url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html";
  Browsers.Item(btChrome).Run(url);

  var propNames = new Array("ObjectType", "ObjectIdentifier");
  var propValues = new Array("Select", "multi");
  var lb = Sys.Browser().Page(url).FindChildEx(propNames, propValues, 15, true);
  // Select the "Carrot" item of the list box
  lb.ClickItem("Carrot");
}

Python

def Sample_SelectItem():
  url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html"
  Browsers.Item[btChrome].Run(url)

  propNames = ["ObjectType", "ObjectIdentifier"]
  propValues = ["Select", "multi"]
  lb = Sys.Browser().Page(url).FindChildEx(propNames, propValues, 15, True)

  # Select the item "Carrot" of the list box
  lb.ClickItem("Carrot")

VBScript

Sub Sample_SelectItem()
  url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html"
  Browsers.Item(btChrome).Run(url)

  propNames = Array("ObjectType", "ObjectIdentifier")
  propValues = Array("Select", "multi")
  Set lb = Sys.Browser().Page(url).FindChildEx(propNames, propValues, 15, True)

  ' Select the "Carrot" item of the list box
  lb.ClickItem("Carrot")
End Sub

DelphiScript

procedure Sample_SelectItem();
var url, propNames, propValues, lb;
begin  url := 'https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html';
  Browsers.Item(btChrome).Run(url);

  propNames := ['ObjectType', 'ObjectIdentifier'];
  propValues := ['Select', 'multi'];
  lb := Sys.Browser().Page(url).FindChildEx(propNames, propValues, 15, true);

  // Select the "Carrot" item of the list box
  lb.ClickItem('Carrot');
end;

C++Script, C#Script

function Sample_SelectItem()
{
  var url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html";
  Browsers["Item"](btChrome)["Run"](url);

  var propNames = new Array("ObjectType", "ObjectIdentifier");
  var propValues = new Array("Select", "multi");
  var lb = Sys["Browser"]()["Page"](url)["FindChildEx"](propNames, propValues, 15, true);

  // Select the "Carrot" item of the list box
  lb["ClickItem"]("Carrot");
}

Select multiple items of list box

Typically, list box controls support selecting multiple items. To select multiple items in your tests, you can use the WebListBoxObj.Multiselect or WebListBoxObj.ClickItem method. The latter supports simulating clicking an item while having a keyboard key pressed.

The samples below show how to select multiple items of a list box on a web page, in keyword tests and in script tests respectively.

In keyword tests

  1. Add the On-Screen Action operation to your keyword test. TestComplete will show the Operation Parameters wizard.

  2. In the wizard, specify the list box whose item you want to select. You can do it by pointing to the list box on the web page.

  3. Select the list box object’s method to call. To simulate selecting multiple items, use the ClickItem or Multiselect method.

  4. If you are using the ClickItem method, specify the caption or the index of the list box item. Specify the keyboard key to be pressed when selecting the item. Typically, it can be the Ctrl key or the Shift key.

    If you are using the Multiselect method, specify the items to select. It can be either an array of item captions or a string containing the captions of items to select, separated with a delimiter character.

    Note: To learn which delimiter character to use, check the wListSeparator property of the Web List Box object.

  5. Click Finish to add the operation and close the wizard.

  6. If you are using the ClickItem method to select multiple items, repeat the steps above to add more operations that will simulate selecting other items.

Selecting multiple items of a list box

Click the image to enlarge it.

In script tests

The script sample below shows how to simulate selecting multiple list box items by using the ClickItem and Multiselect methods:

Web (Cross-Platform)

JavaScript, JScript

function Sample_MultiSelectItems()
{
  var url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html";
  Browsers.Item(btChrome).Run(url);

  var lb = Sys.Browser().Page(url).FindElement("#multi");

  // Select the first item and the "Potato" item of the list box
  // by using the ClickItem method
  lb.ClickItem(0, skCtrl);
  lb.ClickItem("Potato", skCtrl);

  // Select the "Banana" and " Cherry" items of the list box
  // by using the Multiselect method
  lb.Multiselect("Banana;Cherry");

}

Python

def Sample_MultiSelectItems():
  url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html"
  Browsers.Item[btChrome].Run(url)

  lb = Sys.Browser().Page(url).FindElement("#multi")

  # Select the first item and the item "Potato" of the list box
  # by using the ClickItem method
  lb.ClickItem(0, skCtrl)
  lb.ClickItem("Potato", skCtrl)

  # Select the "Banana" and " Cherry" items of the list box
  # by using the Multiselect method
  lb.Multiselect("Banana;Cherry")

VBScript

Sub Sample_MultiSelectItems()
  url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html"
  Browsers.Item(btChrome).Run(url)

  Set lb = Sys.Browser().Page(url).FindElement("#multi")

  ' Select the first item and the "Potato" item of the list box
  ' by using the ClickItem method
  Call lb.ClickItem(0, skCtrl)
  Call lb.ClickItem("Potato", skCtrl)

  ' Select the "Banana" and " Cherry" items of the list box
  ' by using the Multiselect method
  lb.Multiselect("Banana;Cherry")

End Sub

DelphiScript

procedure Sample_MultiSelectItems();
var url, lb;
begin  url := 'https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html';
  Browsers.Item(btChrome).Run(url);

  lb := Sys.Browser().Page(url).FindElement('#multi');

  //Select the first item and the "Potato" item of the list box
  // by using the ClickItem method
  lb.ClickItem(0, skCtrl);
  lb.ClickItem('Potato', skCtrl);

  // Select the "Banana" and " Cherry" items of the list box
  // by using the Multiselect method
  lb.Multiselect('Banana;Cherry');

end;

C++Script, C#Script

function Sample_MultiSelectItems()
{
  var url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html";
  Browsers["Item"](btChrome)["Run"](url);

  var lb = Sys["Browser"]()["Page"](url)["FindElement"]("#multi");

  // Select the first item and the "Potato" item of the list box
  // by using the ClickItem method
  lb["ClickItem"](0, skCtrl);
  lb["ClickItem"]("Potato", skCtrl);

  // Select the "Banana" and " Cherry" items of the list box
  // by using the Multiselect method
  lb["Multiselect"]("Banana;Cherry");

}

Web (Classic)

JavaScript, JScript

function Sample_MultiSelectItems()
{
  var url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html";
  Browsers.Item(btChrome).Run(url);

  var propNames = new Array("ObjectType", "ObjectIdentifier");
  var propValues = new Array("Select", "multi");
  var lb = Sys.Browser().Page(url).FindChildEx(propNames, propValues, 15, true);
  // Select the first item and the "Potato" item of the list box
  // by using the ClickItem method
  lb.ClickItem(0, skCtrl);
  lb.ClickItem("Potato", skCtrl);

  // Select the "Banana" and " Cherry" items of the list box
  // by using the Multiselect method
  lb.Multiselect("Banana;Cherry");

}

Python

def Sample_MultiSelectItems():
  url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html"
  Browsers.Item[btChrome].Run(url)

  propNames = ["ObjectType", "ObjectIdentifier"]
  propValues = ["Select", "multi"]
  lb = Sys.Browser().Page(url).FindChildEx(propNames, propValues, 15, True)

  # Select the first item and the item "Potato" of the list box
  # by using the ClickItem method
  lb.ClickItem(0, skCtrl)
  lb.ClickItem("Potato", skCtrl)

  # Select the "Banana" and " Cherry" items of the list box
  # by using the Multiselect method
  lb.Multiselect("Banana;Cherry")

VBScript

Sub Sample_MultiSelectItems()
  url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html"
  Browsers.Item(btChrome).Run(url)

  propNames = Array("ObjectType", "ObjectIdentifier")
  propValues = Array("Select", "multi")
  Set lb = Sys.Browser().Page(url).FindChildEx(propNames, propValues, 15, True)

  'Select the first item and the "Potato" item of the list box
  ' by using the ClickItem method
  Call lb.ClickItem(0, skCtrl)
  Call lb.ClickItem("Potato", skCtrl)

  ' Select the "Banana" and " Cherry" items of the list box
  ' by using the Multiselect method
  lb.Multiselect("Banana;Cherry")


End Sub

DelphiScript

procedure Sample_MultiSelectItems();
var url, propNames, propValues, lb;
begin  url := 'https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html';
  Browsers.Item(btChrome).Run(url);

  propNames := ['ObjectType', 'ObjectIdentifier'];
  propValues := ['Select', 'multi'];
  lb := Sys.Browser().Page(url).FindChildEx(propNames, propValues, 15, true);

  // Select the first item and the "Potato" item of the list box
  // by using the ClickItem method
  lb.ClickItem(0, skCtrl);
  lb.ClickItem('Potato', skCtrl);

  // Select the "Banana" and " Cherry" items of the list box
  // by using the Multiselect method
  lb.Multiselect('Banana;Cherry');


end;

C++Script, C#Script

function Sample_MultiSelectItems()
{
  var url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html";
  Browsers["Item"](btChrome)["Run"](url);

  var propNames = new Array("ObjectType", "ObjectIdentifier");
  var propValues = new Array("Select", "multi");
  var lb = Sys["Browser"]()["Page"](url)["FindChildEx"](propNames, propValues, 15, true);

  // Select the first item and the "Potato" item of the list box
  // by using the ClickItem method
  lb["ClickItem"](0, skCtrl);
  lb["ClickItem"]('Potato', skCtrl);

  // Select the "Banana" and " Cherry" items of the list box
  // by using the Multiselect method
  lb["Multiselect"]('Banana;Cherry');

}

Get number of list box items

In your tests, you may need to get the number of items that a list box contains. To do this, you can use the WebListBoxObj.wItemCount property.

To compare the number of items against an expected value, you can add a property checkpoint to your test. You can also store the number of items to a variable for future use.

The samples below show how to get the number of list box items and store it to a variable, in keyword tests and in script tests respectively.

In keyword tests

To get the number of items and store it to a variable, you can use the Set Variable Value operation. For example:

  1. Create a variable that will store the number of items. It can be either a keyword variable or a project or project suite variable.

  2. In your keyword test, add the Set Variable Value operation. TestComplete will open the Operation Parameter wizard.

  3. On the first page of the wizard, select the created variable.

  4. On the next page of the wizard, in the Mode drop-down list, select Object Property, and then click the ellipsis button in the Value edit box.

  5. In the resulting dialog, click Onscreen Object and then point to the combo box control on the web page.

  6. On the next page of the wizard, select the wItemCount property.

  7. Click Finish to complete adding the operation.

Storing the number of list box items to a variable

Click the image to enlarge it.

In script tests

To get the number of list box items, use the WebListBoxObj.wItemCount property. For example, the sample script below shows how to get the number of items and post it to the test log:

Web (Cross-Platform)

JavaScript, JScript

function Sample_GetItemCount()
{
  var url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html";
  Browsers.Item(btChrome).Run(url);

  var lb = Sys.Browser().Page(url).FindElement("#multi");

  // Post the number of list box items to the test log
  Log.Message(aqString.Format("The total number of the list box items: %i", lb.wItemCount));
}

Python

def Sample_GetItemCount():
  url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html"
  Browsers.Item[btChrome].Run(url)

  lb = Sys.Browser().Page(url).FindElement("#multi")

  # Post the number of list box items to the test log
  Log.Message(aqString.Format("The total number of list box items: %i", lb.wItemCount))

VBScript

Sub Sample_GetItemCount()
  url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html"
  Browsers.Item(btChrome).Run(url)

  Set lb = Sys.Browser().Page(url).FindElement("#multi")

  ' Post the number of list box items to the test log
  Log.Message(aqString.Format("The total number of the list box items: %i", lb.wItemCount))
End Sub

DelphiScript

procedure Sample_GetItemCount();
var url, lb;
begin  url := 'https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html';
  Browsers.Item(btChrome).Run(url);

  lb := Sys.Browser().Page(url).FindElement('#multi');

  // Post the number of list box items to the test log
  Log.Message(aqString.Format('The total number of the list box items: %i', lb.wItemCount));
end;

C++Script, C#Script

function Sample_GetItemCount()
{
  var url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html";
  Browsers["Item"](btChrome)["Run"](url);

  var lb = Sys["Browser"]()["Page"](url)["FindElement"]("#multi");

  // Post the number of list box items to the test log
  Log["Message"](aqString["Format"]("The total number of the list box items: %i", lb["wItemCount"]));
}

Web (Classic)

JavaScript, JScript

function Sample_GetItemCount()
{
  var url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html";
  Browsers.Item(btChrome).Run(url);

  var propNames = new Array("ObjectType", "ObjectIdentifier");
  var propValues = new Array("Select", "multi");
  var lb = Sys.Browser().Page(url).FindChildEx(propNames, propValues, 15, true);
  // Post the number of list box items to the test log
  Log.Message(aqString.Format("The total number of the list box items: %i", lb.wItemCount));
}

Python

def Sample_GetItemCount():
  url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html"
  Browsers.Item[btChrome].Run(url)

  propNames = ["ObjectType", "ObjectIdentifier"]
  propValues = ["Select", "multi"]
  lb = Sys.Browser().Page(url).FindChildEx(propNames, propValues, 15, True)

  # Post the number of list box items to the test log
  Log.Message(aqString.Format("The total number of list box items: %i", lb.wItemCount))

VBScript

Sub Sample_GetItemCount()
  url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html"
  Browsers.Item(btChrome).Run(url)

  propNames = Array("ObjectType", "ObjectIdentifier")
  propValues = Array("Select", "multi")
  Set lb = Sys.Browser().Page(url).FindChildEx(propNames, propValues, 15, True)

  ' Post the number of list box items to the test log
  Log.Message(aqString.Format("The total number of the list box items: %i", lb.wItemCount))
End Sub

DelphiScript

procedure Sample_GetItemCount();
var url, propNames, propValues, lb;
begin  url := 'https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html';
  Browsers.Item(btChrome).Run(url);

  propNames := ['ObjectType', 'ObjectIdentifier'];
  propValues := ['Select', 'multi'];
  lb := Sys.Browser().Page(url).FindChildEx(propNames, propValues, 15, true);

  // Post the number of list box items to the test log
  Log.Message(aqString.Format('The total number of the list box items: %i', lb.wItemCount));
end;

C++Script, C#Script

function Sample_GetItemCount()
{
  var url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html";
  Browsers["Item"](btChrome)["Run"](url);

  var propNames = new Array("ObjectType", "ObjectIdentifier");
  var propValues = new Array("Select", "multi");
  var lb = Sys["Browser"]()["Page"](url)["FindChildEx"](propNames, propValues, 15, true);

  // Post the number of list box items to the test log
  Log["Message"](aqString["Format"]("The total number of the list box items: %i", lb["wItemCount"]));
}

Get list box items

To get individual items of a list box, you can use the wItem property that returns the caption of the item specified by its index. To compare the obtained value against an expected value, you can use property checkpoints. You can also store the obtained item to a variable for later use.

The samples below show how to get the caption of the first item of a list box and store it to a variable, in keyword tests and in script tests respectively.

In keyword tests

  1. Create a variable that will store the caption. It can be either a keyword variable or a project or project suite variable.

  2. In your keyword test, add the Set Variable Value operation. TestComplete will open the Operation Parameter wizard.

  3. On the first page of the wizard, select the created variable.

  4. On the next page of the wizard, in the Mode drop-down list, select Object Property, and then click the ellipsis button in the Value edit box.

  5. In the resulting dialog, click Onscreen Object and then point to the list box control on the web page.

  6. On the next page of the wizard, select the wItem property. Then, click param and enter the index of the item. In our example, the index of the first item is 0.

    Note: To get the total number of list box items, use the wItemCount property.

  7. Click Finish to complete adding the operation.

Storing the caption of a list box item to a variable

Click the image to enlarge it.

In script tests

The sample script below shows how to get the caption of the first item of a list box and post it to the test log:

Web (Cross-Platform)

JavaScript, JScript

function Sample_GetItem()
{
  var url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html";
  Browsers.Item(btChrome).Run(url);

  var lb = Sys.Browser().Page(url).FindElement("#multi");

  // Post the caption of the first list box item to the test log
  if (lb.wItemCount > 0)
    Log.Message(lb.wItem(0));
}

Python

def Sample_GetItem():
  url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html"
  Browsers.Item[btChrome].Run(url)

  lb = Sys.Browser().Page(url).FindElement("#multi")

  # Post the caption of the first list box item to the test log
  if (lb.wItemCount > 0):
    Log.Message(lb.wItem[0]);

VBScript

Sub Sample_GetItem()
  url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html"
  Browsers.Item(btChrome).Run(url)

  Set lb = Sys.Browser().Page(url).FindElement("#multi")

  ' Post the caption of the first list box item to the test log
  If lb.wItemCount > 0 Then
    Log.Message(lb.wItem(0))
  End If
End Sub

DelphiScript

procedure Sample_GetItem();
var url, lb;
begin  url := 'https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html';
  Browsers.Item(btChrome).Run(url);

  lb := Sys.Browser().Page(url).FindElement('#multi');

  // Post the caption of the first list box item to the test log
  if lb.wItemCount > 0 then
    Log.Message(lb.wItem(0));
end;

C++Script, C#Script

function Sample_GetItem()
{
  var url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html";
  Browsers["Item"](btChrome)["Run"](url);

  var lb = Sys["Browser"]()["Page"](url)["FindElement"]("#multi");

  // Post the caption of the first list box item to the test log
  if (lb["wItemCount"] > 0)
    Log["Message"](lb["wItem"](0));
}

Web (Classic)

JavaScript, JScript

function Sample_GetItem()
{
  var url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html";
  Browsers.Item(btChrome).Run(url);

  var propNames = new Array("ObjectType", "ObjectIdentifier");
  var propValues = new Array("Select", "multi");
  var lb = Sys.Browser().Page(url).FindChildEx(propNames, propValues, 15, true);
  // Post the caption of the first list box item to the test log
  if (lb.wItemCount > 0)
    Log.Message(lb.wItem(0));
}

Python

def Sample_GetItem():
  url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html"
  Browsers.Item[btChrome].Run(url)

  propNames = ["ObjectType", "ObjectIdentifier"]
  propValues = ["Select", "multi"]
  lb = Sys.Browser().Page(url).FindChildEx(propNames, propValues, 15, True)

  # Post the caption of the first list box item to the test log
  if (lb.wItemCount > 0):
    Log.Message(lb.wItem[0])

VBScript

Sub Sample_GetItem()
  url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html"
  Browsers.Item(btChrome).Run(url)

  propNames = Array("ObjectType", "ObjectIdentifier")
  propValues = Array("Select", "multi")
  Set lb = Sys.Browser().Page(url).FindChildEx(propNames, propValues, 15, True)

  ' Post the caption of the first list box item to the test log
  If lb.wItemCount > 0 Then
    Log.Message(lb.wItem(0))
  End If
End Sub

DelphiScript

procedure Sample_GetItem();
var url, propNames, propValues, lb;
begin  url := 'https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html';
  Browsers.Item(btChrome).Run(url);

  propNames := ['ObjectType', 'ObjectIdentifier'];
  propValues := ['Select', 'multi'];
  lb := Sys.Browser().Page(url).FindChildEx(propNames, propValues, 15, true);

  // Post the caption of the first list box item to the test log
  if lb.wItemCount > 0 then
    Log.Message(lb.wItem(0));
end;

C++Script, C#Script

function Sample_GetItem()
{
  var url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html";
  Browsers["Item"](btChrome)["Run"](url);

  var propNames = new Array("ObjectType", "ObjectIdentifier");
  var propValues = new Array("Select", "multi");
  var lb = Sys["Browser"]()["Page"](url)["FindChildEx"](propNames, propValues, 15, true);

  // Post the caption of the first list box item to the test log
  if (lb["wItemCount"] > 0)
    Log["Message"](lb["wItem"](0));
}

Get selected items

List box controls can support selecting several items at once. To get the list of all items selected in a list box, you can use the wSelectedItems property. It returns the captions of selected items separated by the delimiter character. To compare the list of selected items against an expected value, you can use property checkpoints. You can also store the list to a variable for later use or post it to the test log.

The samples below show how to get the list of selected items and store them to a variable, in keyword tests and in script tests respectively.

In keyword tests

  1. Create a variable that will store the caption. It can be either a keyword variable or a project or project suite variable.

  2. In your keyword test, add the Set Variable Value operation. TestComplete will open the Operation Parameter wizard.

  3. On the first page of the wizard, select the created variable.

  4. On the next page of the wizard, in the Mode drop-down list, select Object Property, and then click the ellipsis button in the Value edit box.

  5. In the resulting dialog, click Onscreen Object and then point to the list box control on the web page.

  6. On the next page of the wizard, select the wSelectedItems property.

  7. Click Finish to complete adding the operation.

Storing the list of selected items to a variable

Click the image to enlarge it.

In script tests

The sample script below shows how to get the list of items selected in a list box and post them to the test log:

Web (Cross-Platform)

JavaScript, JScript

function Sample_GetSelectedItems()
{
  var url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html";
  Browsers.Item(btChrome).Run(url);

  var lb = Sys.Browser().Page(url).FindElement("#multi");

  // Select several items in the list box  lb.Multiselect("Banana;Cherry;Potato");

  // Get the list of selected items
  var items = lb.wSelectedItems;
  var delimiter = lb.wListSeparator;
  aqString.ListSeparator = delimiter;

  // Post the selected items to the test log
  for (var i = 0; i < aqString.GetListLength(items); i++)
  {
    Log.Message(aqString.GetListItem(items, i));
  }
}

Python

def Sample_GetSelectedItems():
  url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html"
  Browsers.Item[btChrome].Run(url)

  lb = Sys.Browser().Page(url).FindElement("#multi")

  # Select several items in the list box
  lb.Multiselect("Banana;Cherry;Potato")

  # Get the list of selected items
  items = lb.wSelectedItems
  delimiter = lb.wListSeparator
  aqString.ListSeparator = delimiter

  # Post selected items to the test log
  for i in range (0, aqString.GetListLength(items) - 1):
    Log.Message(aqString.GetListItem(items, i))

VBScript

Sub Sample_GetSelectedItems()
  url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html"
  Browsers.Item(btChrome).Run(url)

  Set lb = Sys.Browser().Page(url).FindElement("#multi")

  ' Select several items in the list box  lb.Multiselect("Banana;Cherry;Potato")

  ' Get the list of selected items
  items = lb.wSelectedItems
  delimiter = lb.wListSeparator
  aqString.ListSeparator = delimiter

  ' Post the selected items to the test log
  For i = 0 To aqString.GetListLength(items) - 1
    Log.Message(aqString.GetListItem(items, i))
  Next
End Sub

DelphiScript

procedure Sample_GetSelectedItems();
var url, lb, items, delimiter, i;
begin  url := 'https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html';
  Browsers.Item(btChrome).Run(url);

  lb := Sys.Browser().Page(url).FindElement('#multi');

  // Select several items in the list box  lb.Multiselect("Banana;Cherry;Potato");

  // Get the list of selected items
  items := lb.wSelectedItems;
  delimiter := lb.wListSeparator;
  aqString.ListSeparator := delimiter;

  // Post the selected items to the test log
  for i := 0 to aqString.GetListLength(items) - 1 do
    Log.Message(aqString.GetListItem(items, i));
end;

C++Script, C#Script

function Sample_GetSelectedItems()
{
  var url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html";
  Browsers["Item"](btChrome)["Run"](url);

  var lb = Sys["Browser"]()["Page"](url)["FindElement"]("#multi");

  // Select several items in the list box  lb["Multiselect"]("Banana;Cherry;Potato");

  // Get the list of selected items
  var items = lb["wSelectedItems"];
  var delimiter = lb["wListSeparator"];
  aqString["ListSeparator"] = delimiter;

  // Post the selected items to the test log
  for (var i = 0; i < aqString["GetListLength"](items); i++)
  {
    Log["Message"](aqString["GetListItem"](items, i));
  }
}

Web (Classic)

JavaScript, JScript

function Sample_GetSelectedItems()
{
  var url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html";
  Browsers.Item(btChrome).Run(url);

  var propNames = new Array("ObjectType", "ObjectIdentifier");
  var propValues = new Array("Select", "multi");
  var lb = Sys.Browser().Page(url).FindChildEx(propNames, propValues, 15, true);
  // Select several items in the list box  lb.Multiselect("Banana;Cherry;Potato");

  // Get the list of selected items
  var items = lb.wSelectedItems;
  var delimiter = lb.wListSeparator;
  aqString.ListSeparator = delimiter;

  // Post the selected items to the test log
  for (var i = 0; i < aqString.GetListLength(items); i++)
  {
    Log.Message(aqString.GetListItem(items, i));
  }
}

Python

def Sample_GetSelectedItems():
  url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html"
  Browsers.Item[btChrome].Run(url)

  propNames = ["ObjectType", "ObjectIdentifier"]
  propValues = ["Select", "multi"]
  lb = Sys.Browser().Page(url).FindChildEx(propNames, propValues, 15, True)

  # Select several items in the list box
  lb.Multiselect("Banana;Cherry;Potato")

  # Get the list of selected items
  items = lb.wSelectedItems
  delimiter = lb.wListSeparator
  aqString.ListSeparator = delimiter

  # Post selected items to the test log
  for i in range (0, aqString.GetListLength(items) - 1):
    Log.Message(aqString.GetListItem(items, i))

VBScript

Sub Sample_GetSelectedItems()
  url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html"
  Browsers.Item(btChrome).Run(url)

  propNames = Array("ObjectType", "ObjectIdentifier")
  propValues = Array("Select", "multi")
  Set lb = Sys.Browser().Page(url).FindChildEx(propNames, propValues, 15, True)

  ' Select several items in the list box  lb.Multiselect("Banana;Cherry;Potato")

  ' Get the list of selected items
  items = lb.wSelectedItems
  delimiter = lb.wListSeparator
  aqString.ListSeparator = delimiter

  ' Post the selected items to the test log
  For i = 0 To aqString.GetListLength(items) - 1
    Log.Message(aqString.GetListItem(items, i))
  Next
End Sub

DelphiScript

procedure Sample_GetSelectedItems();
var url, propNames, propValues, lb, items, delimiter, i;
begin  url := 'https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html';
  Browsers.Item(btChrome).Run(url);

  propNames := ['ObjectType', 'ObjectIdentifier'];
  propValues := ['Select', 'multi'];
  lb := Sys.Browser().Page(url).FindChildEx(propNames, propValues, 15, true);

  // Select several items in the list box  lb.Multiselect("Banana;Cherry;Potato");

  // Get the list of selected items
  items := lb.wSelectedItems;
  delimiter := lb.wListSeparator;
  aqString.ListSeparator := delimiter;

  // Post the selected items to the test log
  for i := 0 to aqString.GetListLength(items) - 1 do
    Log.Message(aqString.GetListItem(items, i));
end;

C++Script, C#Script

function Sample_GetSelectedItems()
{
  var url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html";
  Browsers["Item"](btChrome)["Run"](url);

  var propNames = new Array("ObjectType", "ObjectIdentifier");
  var propValues = new Array("Select", "multi");
  var lb = Sys["Browser"]()["Page"](url)["FindChildEx"](propNames, propValues, 15, true);

  // Select several items in the list box  lb["Multiselect"]("Banana;Cherry;Potato");

  // Get the list of selected items
  var items = lb["wSelectedItems"];
  var delimiter = lb["wListSeparator"];
  aqString["ListSeparator"] = delimiter;

  // Post the selected items to the test log
  for (var i = 0; i < aqString["GetListLength"](items); i++)
  {
    Log["Message"](aqString["GetListItem"](items, i));
  }
}

Check if list box item is selected

To check if an item of a list box is selected, you can create a property checkpoint that would verify the wSelected property. To perform various actions depending on whether an item is selected, you can use the if … then … else statements.

The samples below show how to check if the list box item is selected, in keyword tests and in script tests respectively.

In keyword tests

To check if an item of a list box is selected and simulate various actions depending on the result, you can use the If … Then and Else operations:

  1. Add the If … Then operation to your test.

  2. Specify the condition that will get the state of the list box item and compare it against the expected value:

    1. In the Value 1 column of the Operation Parameters dialog, click the ellipsis button.

    2. In the resulting Edit Value dialog, set the mode to Object Property.

    3. Set the value to Onscreen Object.

    4. Point to the list box on the web page.

    5. To get the list state, select the wSelected property. Then, click param and enter the index of the item. In our example, we will check the state of the first item in the list. Its index is 0.

    6. Click OK to close the dialog.

    7. In the Value 2 column, enter the baseline value against which the list state will be compared: true for selected and false for not selected.

    8. Click OK to save the changes and close the Operation Parameters dialog.

  3. To specify operations to run if the list item has the expected state, add them as child items of the If … Then operation.

  4. To specify the operation to run otherwise, add the Else operation right after the If … Then operation, and then add the needed operations as child items of the Else operation.

Checking if a list item is selected

Click the image to enlarge it.

In script tests

Web (Cross-Platform)

JavaScript, JScript

function Sample_CheckSelected()
{
  var url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html";
  Browsers.Item(btChrome).Run(url);

  var lb = Sys.Browser().Page(url).FindElement("#multi");

  if (lb.wSelected(0))
    Log.Message("The first item of the list box is selected.");
  else
    Log.Message("The first item is not selected.");
}

Python

def Sample_CheckSelected():
  url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html"
  Browsers.Item[btChrome].Run(url)

  lb = Sys.Browser().Page(url).FindElement("#multi")

  if (lb.wSelected[0]):
    Log.Message("The first item of the list box is selected.")
  else:
    Log.Message("The first item is not selected.")

VBScript

Sub Sample_CheckSelected()
  url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html"
  Browsers.Item(btChrome).Run(url)

  Set lb = Sys.Browser().Page(url).FindElement("#multi")

  If lb.wSelected(0) Then
    Log.Message("The first item of the list box is selected.")
  Else
    Log.Message("The first item is not selected.")
  End If
End Sub

DelphiScript

procedure Sample_CheckSelected();
var url, lb;
begin  url := 'https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html';
  Browsers.Item(btChrome).Run(url);

  lb := Sys.Browser().Page(url).FindElement('#multi');

  if (lb.wSelected(0)) then
    Log.Message('The first item of the list box is selected.')
  else
    Log.Message('The first item is not selected.');
end;

C++Script, C#Script

function Sample_CheckSelected()
{
  var url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html";
  Browsers["Item"](btChrome)["Run"](url);

  var lb = Sys["Browser"]()["Page"](url)["FindElement"]("#multi");

  if (lb["wSelected"](0))
    Log["Message"]("The first item of the list box is selected.");
  else
    Log["Message"]("The first item is not selected.");
}

Web (Classic)

JavaScript, JScript

function Sample_CheckSelected()
{
  var url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html";
  Browsers.Item(btChrome).Run(url);

  var propNames = new Array("ObjectType", "ObjectIdentifier");
  var propValues = new Array("Select", "multi");
  var lb = Sys.Browser().Page(url).FindChildEx(propNames, propValues, 15, true);
  if (lb.wSelected(0))
    Log.Message("The first item of the list box is selected.");
  else
    Log.Message("The first item is not selected.");
}

Python

def Sample_CheckSelected():
  url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html"
  Browsers.Item[btChrome].Run(url)

  propNames = ["ObjectType", "ObjectIdentifier"]
  propValues = ["Select", "multi"]
  lb = Sys.Browser().Page(url).FindChildEx(propNames, propValues, 15, True)

  if (lb.wSelected[0]):
    Log.Message("The first item of the list box is selected.")
  else:
    Log.Message("The first item is not selected.")

VBScript

Sub Sample_CheckSelected()
  url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html"
  Browsers.Item(btChrome).Run(url)

  propNames = Array("ObjectType", "ObjectIdentifier")
  propValues = Array("Select", "multi")
  Set lb = Sys.Browser().Page(url).FindChildEx(propNames, propValues, 15, True)

  If lb.wSelected(0) Then
    Log.Message("The first item of the list box is selected.")
  Else
    Log.Message("The first item is not selected.")
  End If
End Sub

DelphiScript

procedure Sample_CheckSelected();
var url, propNames, propValues, lb;
begin  url := 'https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html';
  Browsers.Item(btChrome).Run(url);

  propNames := ['ObjectType', 'ObjectIdentifier'];
  propValues := ['Select', 'multi'];
  lb := Sys.Browser().Page(url).FindChildEx(propNames, propValues, 15, true);

  if lb.wSelected(0) then
    Log.Message('The first item of the list box is selected.')
  else
    Log.Message('The first item is not selected.');

end;

C++Script, C#Script

function Sample_CheckSelected()
{
  var url = "https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Learn/Forms/Other_form_controls/_sample_.Multiple_choice_select_box.html";
  Browsers["Item"](btChrome)["Run"](url);

  var propNames = new Array("ObjectType", "ObjectIdentifier");
  var propValues = new Array("Select", "multi");
  var lb = Sys["Browser"]()["Page"](url)["FindChildEx"](propNames, propValues, 15, true);

  if (lb["wSelected"](0))
    Log["Message"]("The first item of the list box is selected.");
  else
    Log["Message"]("The first item is not selected.");
}

See Also

Working With Application Objects and Controls
Web and RIA Testing

Highlight search results