Negative Test in SAPUI5 Using OPA

Viewed 1754

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.

3 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:

How about finding the parent with a this.waitFor, then checking that it does not have the child in one of its aggregations?

This is advantageous because the control id does not have to be known in advance in case we are only looking for a control of a certain type/text/icon without an ID. This approach also supports checking for controls that are not only invisible but have never been created.

For example:

iDoNotSeeTheControl: function (sParentId, sIcon, sViewName) {
  return this.waitFor({
    id: sParentId,
    viewName: sViewName,
    check: function(oParentControl) {
      return oParentControl.getAggregation("content").every(function(oControl) {
        // comparing via something remarkable of the control in question
        return oControl.getIcon() !== sIcon;
      }
    },
    success: function () {
      Opa5.assert.ok(true, 
        "The control (" + sControlId + ") is not visible");
    },
    errorMessage: "Did not find the control: " + sControlId
  });
},
Related