Main Content

choose

Class: matlab.uitest.TestCase
Package: matlab.uitest

Perform choose gesture on UI component

Syntax

choose(testcase,comp,option)
choose(testcase,compNoOpts)

Description

choose(testcase,comp,option) performs a choose gesture on the specified item on the UI component comp.

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 choose a particular tab from a tab group.

Input Arguments

expand all

Instance of the test case, specified as a matlab.uitest.TestCase object.

Component to choose during test, specified as a UI component object that supports a choose gesture. Components that support choose gestures include check boxes, knobs, switches, and drop-down lists.

Supported ComponentTypical Creation Function
Button Groupuibuttongroup
Check Boxuicheckbox
Discrete Knobuiknob
Drop Downuidropdown
Knobuiknob
List Boxuilistbox
Radio Buttonuiradiobutton
Slideruislider
State Buttonuibutton
Switch (Rocker, Slider, Toggle)uiswitch
Tab Groupuitabgroup
Toggle Buttonuitogglebutton
Toggle Tooluitoggletool

Item in the UI component to choose. The data type of option depends on the type of component being tested. For example, if comp is a switch, option is a text or numeric value from the Items property of the switch. If comp is a check box or toggle tool, option is a logical value.

When a component has an Items property, option can be the value of an element in Items or an index to an element in Items. For example, for a default discrete knob, you can choose 'Medium' using a value for option that is either 'Medium' or 3.

Component to choose, specified as a UI component object that supports a choose gesture and does not require additional information. Components that support choose gestures include tabs and tree nodes.

Supported ComponentTypical Creation Function
Tabuitab
Tree Nodeuitreenode

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 performs the programmatic choose gesture.

tc = matlab.uitest.TestCase.forInteractiveUse;
tc.choose(knob,'High')

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

View the value of the Items property on 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'.

tc.choose(knob,2)

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

Create a list box and enable multiple node selection.

listbox = uilistbox('Multiselect','on')
listbox = 

  ListBox (Item 1) with properties:

              Value: {'Item 1'}
              Items: {'Item 1'  'Item 2'  'Item 3'  'Item 4'}
          ItemsData: []
        Multiselect: 'on'
    ValueChangedFcn: ''
           Position: [100 100 100 74]

  Show all properties

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

Create an interactive test case and choose items 1 through 3.

tc = matlab.uitest.TestCase.forInteractiveUse;
tc.choose(listbox,1:3)

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

Choose items 1 and 3 using the values of the Items property.

tc.choose(listbox,{'Item 1','Item 3'})

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

Create a slider.

s = uislider;

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

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

Choose a new slider value and verify the slider value changes. Since the framework mimics 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 = 42;
tc.choose(s,expVal)
tc.verifyEqual(s.Value,expVal,'AbsTol',0.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'.

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

Choose tab 2 and verify that the selected tab changes.

tc.choose(group,'Tab #2')
tc.verifyEqual(group.SelectedTab,tab2)
Verification passed.

Compatibility Considerations

expand all

Behavior changed in R2020b

Introduced in R2018a