Contenuto principale

choose

Class: matlab.uitest.TestCase
Namespace: matlab.uitest

Perform choose gesture on UI component

Description

Components

choose(testCase,comp,option) performs a choose gesture on the UI component comp using the specified option.

example

choose(testCase,compNoOpts) performs a choose gesture on a UI component that does not require additional information, such as a tab or a tree node. For example, use this syntax to choose a specific tab, but use the previous syntax to select a particular tab from a tab group.

Table Cells

choose(testCase,uit,indices) performs a choose gesture on the uneditable table cell specified by indices within the table UI component uit.

example

choose(testCase,uit,indices,option) performs a choose gesture on the editable table cell specified by indices. The method uses option to modify the contents of the cell.

Multiple Elements

choose(testCase,compsm,elements,SelectionMode=mode) uses the specified selection mode to select multiple elements within the component compsm. For example, choose(testCase,uit,[1 1; 3 3],SelectionMode="contiguous") selects the cells with indices (1, 1) and (3, 3) and all cells in between these cells from the specified table.

example

Input Arguments

expand all

Test case, specified as a matlab.uitest.TestCase object.

Component under test, specified as a UI component object that supports a choose gesture. This table shows the components that support choose gestures.

Supported ComponentTypical Creation Function
Button groupuibuttongroup
Check boxuicheckbox
Discrete knobuiknob
Drop-down listuidropdown
Knobuiknob
List boxuilistbox
Radio buttonuiradiobutton
Slideruislider
State buttonuibutton
Switch (rocker, slider, toggle)uiswitch
Tab groupuitabgroup
Toggle buttonuitogglebutton
Toggle tooluitoggletool

Option to choose within the UI component, specified as a value that depends on the type of component being tested. For example, if the component is a switch, option can be a text or numeric value from the Items property of the switch. If the component is a check box or toggle tool, option can be a logical value. For a table UI component with editable cells, option can be a logical value or a drop-down item corresponding to the data contained in the cell.

When a component has an Items property, option can be the value of an element in Items or the index to an element in Items. For example, for a default discrete knob, you can choose the Medium knob value by specifying option as either "Medium" or 3.

Component with no options, specified as a Tab or TreeNode object. This table shows the components that support a choose gesture and do not require additional information.

Supported ComponentTypical Creation Function
Tabuitab
Tree nodeuitreenode

Target table UI component, specified as a Table object. Table UI components are created using the uitable function.

Indices of the table cell to select, specified as a 1-by-2 numeric vector. The first and second elements of the vector correspond to the row and column indices of the table cell, respectively.

Example: [1 2]

Component from which to select multiple elements, specified as a ListBox or Table object. This table shows the components that support using a choose gesture that selects multiple elements.

Supported ComponentTypical Creation Function
List boxuilistbox
Tableuitable

Example: uilistbox(Multiselect="on")

Example: uitable(Data=randi(100,10,3))

Component elements to select, specified as a numeric array, string vector, or cell vector of character vectors. The type and shape of elements depend on the component and selection mode.

ComponentSelection Mode
DiscontiguousContiguous
List Box

An N-element numeric vector, string vector, or cell vector of character vectors, where N is the number of list box items to select. You can specify either the names or the indices of the items.

A two-element numeric vector, string vector, or cell vector of character vectors that specifies the range of list box items to select. You can specify either the names or the indices of the first and last items in the range. The method performs a choose gesture on the specified items as well as all items between them.

Table

An N-by-2 numeric matrix, where N is the number of table cells to select. Each matrix row corresponds to the row and column indices of a cell to select.

A 2-by-2 numeric matrix that specifies the boundaries of the block of table cells to select. Each matrix row corresponds to the row and column indices of a cell. The method performs a choose gesture on the specified cells as well as all cells between them.

Example: [1 3 4] (discontiguous selection of three list box items)

Example: ["Item 1" "Item 4"] (discontiguous selection of two list box items, or contiguous selection of four list box items)

Example: [2 3; 2 4; 5 1] (discontiguous selection of three table cells)

Example: [1 1; 3 3] (discontiguous selection of two table cells, or contiguous selection of nine table cells)

Selection mode, specified as one of the following values. This input controls how multiple elements are selected within a component:

  • "discontiguous" — The method performs a choose gesture on only the elements specified in the elements input argument. An example of discontiguous selection is clicking a table cell, holding Ctrl (Command on macOS systems), and then clicking one or more noncontiguous cells.

  • "contiguous" — The method performs a choose gesture on the elements specified in the elements input argument and all elements between them. An example of contiguous selection is clicking a table cell, holding Shift, and then clicking another cell to select all the cells between them.

Data Types: string | char

Attributes

Sealedtrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

Create a discrete knob.

knob = uiknob("discrete");

A figure with a discrete knob. The knob value is Off.

Create an interactive test case and choose the High knob value. An animated blue dot indicates the programmatic choose gesture.

testCase = matlab.uitest.TestCase.forInteractiveUse;
testCase.choose(knob,"High")

A figure with a discrete knob. The knob value is High.

View the value of the Items property of the knob.

knob.Items
ans =

  1×4 cell array

    {'Off'}    {'Low'}    {'Medium'}    {'High'}

Choose the Low knob value by index. The knob moves from High to Low.

testCase.choose(knob,2)

A figure with a discrete knob. The knob value is Low.

Create a slider.

s = uislider;

Create an interactive test case and verify that the value of the slider button is 0.

testCase = matlab.uitest.TestCase.forInteractiveUse;
testCase.verifyEqual(s.Value,0)
Verification passed.

Choose a new slider value and verify that the slider value changes. Because the method simulates a user manipulating the component to an arbitrarily precisioned value, it is a best practice to use a tolerance to compare the actual and expected slider values.

expVal = 50;
testCase.choose(s,expVal)
testCase.verifyEqual(s.Value,expVal,AbsTol=1)
Verification passed.

Create a figure with two tabs.

fig = uifigure;
group = uitabgroup(fig);
tab1 = uitab(group,Title="Tab #1");
tab2 = uitab(group,Title="Tab #2");

Create an interactive test case and verify that the selected tab title contains the substring "#1".

testCase = matlab.uitest.TestCase.forInteractiveUse;
testCase.verifySubstring(group.SelectedTab.Title,"#1")
verification passed.

Select the second tab and verify the change in selection.

testCase.choose(group,"Tab #2")
testCase.verifyEqual(group.SelectedTab,tab2)
Verification passed.

Create a table UI component that contains text and logical values.

name = {'Smith'; 'Chang'; 'Green'};
address = { ...
    sprintf('456 Elm Avenue\nApt. 789\nNew York, NY 67890'); ...
    sprintf('123 Maple Street\nBoston, MA 12345'); ...
    sprintf('789 Oak Lane\nLos Angeles, CA 23456')};
eligibility = {true; true; false};

fig = uifigure;
uit = uitable( ...
    Parent=fig, ...
    Position=[20 20 400 200], ...
    Data=[name address eligibility], ...
    ColumnName=["Last Name" "Address" "Eligible"], ...
    ColumnEditable=[false false true]);

Figure contains an object of type uitable.

Create a test case for interactive testing and select the table cell with indices (2, 2). The choose method makes a selection by pressing at the center of the cell.

testCase = matlab.uitest.TestCase.forInteractiveUse;
testCase.choose(uit,[2 2])

Figure contains an object of type uitable.

Clear the check box in the table cell with indices (1, 3) by selecting that cell. Because the specified table cell contains a logical value, the choose method interacts with the check box instead of pressing at the center of the cell.

testCase.choose(uit,[1 3])

Figure contains an object of type uitable.

Create a table UI component that displays a 10-by-3 array of random integers.

fig = uifigure;
uit = uitable(fig,Data=randi(100,10,3));

Create an interactive test case and select the cells with indices (1, 1) and (3, 3).

testCase = matlab.uitest.TestCase.forInteractiveUse;
testCase.choose(uit,[1 1; 3 3])

Figure contains an object of type uitable.

Now, select the cells with indices (1, 1) and (3, 3) and all the cells between them.

testCase.choose(uit,[1 1; 3 3],SelectionMode="contiguous")

Figure contains an object of type uitable.

Create a list box that supports multiple item selection.

fig = uifigure;
listbox = uilistbox(fig,Multiselect="on");

A figure with a list box that has four items. Item 1 is selected.

Create an interactive test case and select the first and third items in the list box. You can specify the items to select either by their names in the Items property (["Item 1" "Item 3"]) or by their indices ([1 3]).

testCase = matlab.uitest.TestCase.forInteractiveUse;
testCase.choose(listbox,[1 3])

A figure with a list box that has four items. Items 1 and 3 are selected.

Now, select items 1 through 3 by simulating a Shift+click action.

testCase.choose(listbox,[1 3],SelectionMode="contiguous")

A figure with a list box that has four items. Items 1 through 3 are selected.

Version History

Introduced in R2018a

expand all