Search code examples
integration-testingsapui5

SAPUI5 OPA5 How to trigger a select event


Below a typical action to test if a sap.m.Select contains an item with the name xyz and then select it.

success: function(oSelect) {
    var oItems = oSelect.getItems();
    $.each(oItems, function(i,v) {
        if(oItems[i].getText() === "TestItemNameILikeToSelect") {
            oTestOpa5TestItem = oItems[i];
        }
    });
    if(oTestOpa5TestItem !== null) {
        oSelect.setSelectedKey(oTestOpa5TestItem.getKey());
        oTestOpa5TestItem.$().trigger("tap");
    }
},

When I start the test run it does correctly select the proper item from the list and sets it visibly in the browser, but it does not trigger the attached event that is behind (e.g. change="onListItemChanged"). My application works fine, but I don't find a way to create a working test for it.

Thanks in advance


Solution

  • OPA5 has an 'Action' interface and two default implementations e.g. 'EnterText' and 'Press'. The recommended usage is to define an action block on the waitFor() options like this:

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

    What you use is the 'old way' but it has some shortcomings:

    • success block is not synchronized with XHR requests but action is.
    • Sending a click/tap event to a control could require selecting some internal element. Imagine a click to nav container - there are several places you could click actually. Actions handle those details and define a standard behavior you could depend on.
    • It is better to encapsulate your selection logic inside a matchers block and even abstract it to a custom matcher. This way your success block will be cleaner and you could reuse the matcher in several places in your test.

    OPA5 Actions