docs

Simulating User Interactions on Controls

OPA5 has a built-in actions parameter that can be used for simulating events. If you use an action, OPA5 makes 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 doesn’t execute the checks on the UI. You can use multiple actions on one control and you can mix built-in actions with custom actions.


Simulating a press Event

In this example, we trigger a press event on a button, using the waitFor function of OPA5, and the Press action. Note that the action has access to the located button implicitly.

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

As of version 1.97, you can add key modifiers, such as [Control], [Alt], and [Shift]:

oOpa.waitFor({
    id: "myButton",
    actions: new Press({
        altKey: true,
        shiftKey: true,
        ctrlKey: true
    }),
});

As of version 1.98, you can use coordinates for the X and Y axes by providing values for the xPercentage and yPercentage properties. The values provided should be percentages, for example in the range [0 - 100], relative to the control, which is then converted to exact coordinates in pixels:

oOpa.waitFor({
    id: "myButton",
    actions: new Press({
        xPercentage: 80,
        yPercentage: 20
    }),
});

Choosing an Item from sap.m.Select

Here’s an example showing how to choose an item from sap.m.Select, using the waitFor function of OPA5, and the Press action:

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

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

        When.waitFor({
            id: "mySelect",
            actions: new Press(),
            success: function(oSelect) {
                this.waitFor({
                    controlType: "sap.ui.core.Item",
                    matchers: [
                        new Ancestor(oSelect),
                        new Properties({ key: "Germany"})
                    ],
                    actions: new Press(),
                    errorMessage: "Cannot select Germany from mySelect"
                });
            },
            errorMessage: "Could not find mySelect"
        });

        // Assert what happened after pressing using Then

    });

});

For sap.m.Combobox, use controlType: "sap.m.StandardListItem".


Entering Text into Input Fields

Use the EnterText action when you want to enter text in a form control.

In this example, the text of an sap.m.Input is changed twice. First, “Hello “ is entered as value. Then, with the second action, “World” is added. As a result, the value of the input is “Hello World”.

oOpa.waitFor({
    id: "myInput",
    actions: [
        new EnterText({ text: "Hello " }),
        new EnterText({ text: "World" })
    ]
});

There are a couple of modifiers to the EnterText action:


Table Interaction

A Table consists of columns (sap.m.Column) and rows. The rows, defined as sap.m.ColumnListItems, consist of cells. In order to use a stable locator, which isn’t expected to change frequently, you can use a field/value combination to retrieve and interact with table items.

The following example simulates a click on an item in a table. The name of the field can be found in the $metadata file of your OData service.

iClickOnTableItemByFieldValue: function () {
                    return this.waitFor({
                        controlType: "sap.m.ColumnListItem",

                        // Retrieve all list items in the table
                        matchers: [function(oCandidateListItem) {
                            var oTableLine = {};
                            oTableLine = oCandidateListItem.getBindingContext().getObject();
                            var sFound = false;

                            // Iterate through the list items until the specified cell is found
                            for (var sName in oTableLine) {
                                if ((sName === "Field Name") && (oTableLine[sName].toString() === "Cell Value")) {
                                     QUnit.ok(true, "Cell has been found");
                                    sFound = true;
                                    break;
                                }
                            }
                            return sFound;
                        }],

                        // Click on the specified item
                        actions: new Press(),
                        errorMessage: "Cell could not be found in the table"
                     });
                }

Simulating Drag and Drop

As of version 1.76, you can use the sap.ui.test.actions.Drag and sap.ui.test.actions.Drop actions.

First, locate a control to drag and use the Drag action with it. Then, locate the control on which you wish to drop the first control, and use the Drop action with it. The Drop action accepts several optional parameters to specify the drop target:

The following example rearranges items in a list:

// Find the item to drag
oOpa.waitFor({
    controlType: "sap.m.StandardListItem",
    matchers: new BindingPath({
        path: "/ProductCollection/1"
    }),
    // Start the dragging
    actions: new Drag()
});

// Find another item on which to drop the dragged item
oOpa.waitFor({
    controlType: "sap.m.StandardListItem",
    matchers: new BindingPath({
        path: "/ProductCollection/5"
    }),
    // Finish dragging and drop the item right before this one.
    // In the end, the item with binding context path "/ProductCollection/1" should appear right on top of the item with
    // binding context path "/ProductCollection/5"
    actions: new Drop({
        before: true
    })
});

Simulating Scroll

As of version 1.90, you can scroll in controls that provide a scrollable area. The following example scrolls sap.uxap.ObjectPageLayout vertically by 200px:

oOpa.waitFor({
    controlType: "sap.uxap.ObjectPageLayout",
    actions: new Scroll({
        x: 0,
        y: 200
    })
});

For more information, see sap.ui.core.delegate.ScrollEnablement.


Writing Your Own Action

Since OPA5 uses JavaScript for its execution, you can’t 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 OpenUI5 controls don’t have a common interface for that. If you find you’re missing a certain built-in action, you can create your own actions easily. Just provide an inline function as shown here:

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

    opaTest("Should simulate press on the delete button", function (Given, When, Then) {
        // Startup the application using Given

        When.waitFor({
            id : "entryList",
            matchers : new Properties({ mode : "Delete"}),
            actions: function (oList) {
                oList.fireDelete({listItem : oList.getItems()[0]});
            },
            errorMessage : "The delete button could not be pressed"
        });

        // Assert what happened after selecting the item using Then

    });

});

Related Information

API Reference: sap.ui.test.actions

API Reference: sap.ui.test.actions.EnterText

API Reference: sap.ui.test.actions.Press

API Reference: sap.ui.test.matchers.Interactable