docs

Extensions for OPA5

Extend OPA capabilities with custom extensions.

You can provide application-aware assertions that are called from the test but operate in the context of the application being tested.


Interface

The extension API is defined in the sap.ui.test.OpaExtension class. A custom extension should extend this class and implement the necessary methods. The extension class should be available in the application and loaded in the application frame.


Lifecycle

To load an extension, the test should enable it by specifying extension class name as string in the key ‘extensions’ in the options object given to Opa5.extendConfig(). An array of extension names could be specified or the extension name ?opaExtensions=[my/custom/Extension] could be given in the test URL. If the extension needs some application parameters, they could be provided in the appParams.

For more information, see the API Reference: Opa5.extendConfig()


Example

Custom extension class:

sap.ui.define([
  'sap/ui/test/OpaExtension'
], function(OpaExtension) {
  "use strict";
  var customExtension = OpaExtension.extend("sap.ui.test.sample.CustomOpaExtension", {
    metadata : {
      publicMethods : [
        "getAssertions"
      ]
    },
    getAssertions : function() {
      return {
        myCustomAssertion: function() {
          return new Promise(function(resolve, reject) {

            // start custom assertion logic, resolve the promise when ready
            setTimeout(function() {

              // Assertion passes
              resolve({
                result: true,
                message: "Custom assertion passes"
              });

              // OR Assertion fails
              resolve({
                result: false,
                message: "Custom assertion fails"
              });

              // OR Propagate an error while evaluating assertion
              reject(new Error("Error while evaluating assertion, details: " + details));

            },0);

          });
        }
      }
    }
  });
  
  return customExtension;
});

Activate this extension and provide some URI parameters to the application:

Opa5.extendConfig({
  extensions: ["sap/ui/test/sample/CustomOpaExtension"],
  appParams: {
    "key": "value"
  }
});

Call the custom extension from the test:

Opa5.createPageObjects({
  onMyView : {
    viewName : "MyView",
    assertions : {
      iShouldUseMyCustomAssertion : function () {
        return this.waitFor({
          id: "MyControlId",
          actions: new Press(),
          success : function () {
            Opa5.assert.myCustomAssertion();
          }
        });
      }
    }
  }
});