Show TOC

Simulating User Interactions on ControlsLocate this document in the navigation structure

OPA5 has a built-in actions parameter that can be used for simulating events. If you use an action, OPA5 will make sure that the UI is in a state that allows the action to be executed.

We recommend that you use actions and not success functions for user interactions as using success functions will not execute the checks on the UI. For more details, see the sap.ui.test.matchers.Interactable and sap.ui.test.actions API documentation in the Demo Kit.

Simulating a Press Event

Use the press action for sap.m.Button controls or to select any kind of sap.m list item, such as a StandardListItem.

Here's an example showing how to trigger a press event on a button using the OPA5 waitFor function and the press action:

sap.ui.require(["sap/ui/test/opaQUnit", "sap/ui/test/actions/Press"], function (opaTest, Press) {

    opaTest("Should trigger a press event", function (Given, When, Then) {
        // Startup the application using Given

        When.waitFor({
            id: "myButton",
            actions: new Press()
        });

        // Assert what happened after pressing using Then

    });

});

For more information and a list of supported controls, sap.ui.test.actions.Press in the API Reference in the Demo Kit.

Entering Text into Input Fields

If you want to enter text into fields within a form, OPA contains an EnterText action which you can use to enter texts or sap.m.Tokens into sap.m.Input, sap.m.MultiInput, sap.m.SearchField, or sap.m.TextArea.

In the following example, the text of an input will be changed twice. First the user types Hello. The text will be set as a value of the input. In the second action, the user types World - the resulting text in the input will be Hello World. This means you can use multiple actions on one control. You may also mix built-in actions with custom actions.

sap.ui.require(["sap/ui/test/opaQUnit", "sap/ui/test/actions/EnterText"], function (opaTest, EnterText) {

    opaTest("Should trigger a press event", function (Given, When, Then) {
        // Startup the application using Given

        When.waitFor({
            id: "myInput",
            // If you want you can provide multiple actions
            actions: [new EnterText({ text: "Hello " }), new EnterText({ text: " World" })
        });

        // Assert what happened after pressing using Then
    });

});
Writing Your Own Action

Since OPA5 uses JavaScript for its execution, you cannot use native browser events to simulate user events. Sometimes it's also hard to know the exact position where to click or enter your keystrokes since SAPUI5 controls don't have a common interface for that. For example, it's really hard to select a sap.m.Select item using a mouse: you have to click the Select, wait until its popup has opened, find the item and then click on it. It is much easier to use the API of the setSelectedKey select. If you find you're missing a certain built-in action, you can create your own actions very easily. Just provide an inline function as shown here:

sap.ui.require(["sap/ui/test/opaQUnit"], function (opaTest) {

    opaTest("Should trigger a press event", function (Given, When, Then) {
        // Startup the application using Given

        When.waitFor({
            // Even if there are multiple selects
            controlType: "sap.m.Select",
            // An action will get a single select and will be invoked once for every select
            // Opa will make sure the select is available and in a state that you can interact with it
            actions: function (oSelect) {
                oSelect.setSelectedKey("keyOfMyItem");
            }
        });

        // Assert what happened after selecting the item using Then

    });

});