Press Tab or Return
Note
This function doesn't generate Navigation events. Interactions with certain controls that depend on Navigation events are therefore not supported in Play mode tests.
To simulate pressing the Tab key, use the TabKeyPress() or the ReturnKeyPress() method from the PanelSimulator class. Before calling these methods, focus the control or element that needs to react to them.
The following example shows how to simulate pressing Tab:
// TabKeyPress is only officially supported for Editor.
[Test]
public void TabKeyPressExample()
{
// Make sure the UI is totally up to date.
simulate.FrameUpdate();
// Fetch and focus the Button.
var button = rootVisualElement.Q<Button>("MyButton");
button.Focus();
simulate.FrameUpdate();
Assume.That(button.hasFocusPseudoState, Is.True);
// Send Tab key press.
simulate.TabKeyPress();
simulate.FrameUpdate();
Assert.That(button.hasFocusPseudoState, Is.False);
}
The following example shows how to simulate pressing Return:
// ReturnKeyPress is only officially supported for Editor.
[Test]
public void ReturnKeyPressExample()
{
// Make sure the UI is totally up to date.
simulate.FrameUpdate();
var actionWasExecuted = false;
// Set up the button's clicked functionality to flip a Boolean.
Button button = rootVisualElement.Q<Button>("MyButton");
button.clicked += () => { actionWasExecuted = true; };
// Click on the button's position.
button.Focus();
simulate.ReturnKeyPress();
simulate.FrameUpdate();
Assert.That(actionWasExecuted, Is.True,
"Button action was not executed.");
}