Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negative Test in SAPUI5 Using OPA

Is there a way, to check with OPA-Testing, if an element does not exist?

For example the test succeeds, if the waitFor#success callback is not executed and an error message will be shown?

I have an use case, where a button shall be shown or not depending on a very important model property. I want to check this on every deployment with an OPA Test.

The button property is bound to visible, and if the property is false, the button doesnt appear in the DOM and can not be checked for its state because of that.

like image 888
monavari-lebrecht Avatar asked Oct 28 '25 05:10

monavari-lebrecht


2 Answers

If the control was never created or has been dismantled or completely removed from SAPUI5's managers, for example with oMyControl.destroy(), the following works:

theControlShouldNotBeThere: function(sControlId) {
  return this.waitFor({
    success: function() {
      var bExists = (Opa5.getJQuery()("#" + sControlId).length > 0);
      Opa5.assert.ok(!bExists, "Control doesn't exist");
    }
  });
}

Mind the following details:

  • Use waitFor with only the success callback to ensure that OPA puts this assertion at the end of its queue of other steps to execute. Without, the code would be executed as very first step in the OPA test. This is described as a best practice in OPA5's Cookbook.

  • The Opa5.getJQuery method returns the jQuery object within the iFrame that runs the tested app. $ or jQuery would address the jQuery object of the surrounding window that runs the OPA environment, which would be unable to identify the requested controls.

  • .length is jQuery's preferred way of validating whether a selection is empty.

like image 182
Florian Avatar answered Oct 30 '25 10:10

Florian


You can use PropertyStrictEqual matcer for that

There is an exmaple:

            // Check if the control is not visible
        iShouldNotSeeTheControl: function (sControlId, sViewName) {
            return this.waitFor({
                id: sControlId,
                viewName: sViewName,
                visible: false,
                matchers: new PropertyStrictEquals({
                    name : "visible", 
                    value : false}),
                success: function () {
                    Opa5.assert.ok(true, "The control (" + sControlId + ") is not visible");
                },
                errorMessage: "Did not find the hidden control: " + sControlId
            });
        },
like image 25
Pavel Lazhbanov Avatar answered Oct 30 '25 11:10

Pavel Lazhbanov