j-petty
j-petty

Reputation: 3026

How to trigger a button click event from the user's browser

I'm trying to set up some UI automation in SAP Fiori by adding JavaScript to the page so that when a user loads a form, some fields are prefilled for them, and then it automatically goes to the next screen.

I can't make modifications in the SAP UI, this needs to be done in the browser at runtime.

enter image description here

I need to trigger the click of the 'Create' button above. The button's DOM id is __button9.

However, invoking the normal JavaScript click event doesn't work. I believe the SAP UI framework abstracts these events. So I'm wondering if there's a way to still trigger the underlying SAP event to invoke the click functionality?

Using the button.click function doesn't trigger the SAP button click event:

const button = document.getElementById('__button9');
button.click();

Dispatching the event like this also doesn't work:

const clickEvent = document.createEvent('MouseEvents');
clickEvent.initEvent('click', true, true);

const button = document.getElementById('__button9');

button.dispatchEvent(clickEvent);

This is a very product specific question, but a similar question has already been asked on the SAP support channels here and didn't receive a response.

This blog explains the SAP event model pretty well, but the use case of triggering button events from outside the context of the SAP UI Controller doesn't seem to be covered.

Upvotes: 2

Views: 3982

Answers (2)

Alex
Alex

Reputation: 101

https://sapui5.hana.ondemand.com/1.71.38/#/api/sap.m.Button/methods/firePress

Controls already have fireEvent method and derivatives in their API as fire+EventName.

var oButton = this.byId("button");
oButton.attachPress(function(oEvent){
    console.log("Pressed!");
});

oButton.firePress();

Upvotes: 0

I found this in the SAP forum but haven't tried it yet.

sap.ui.getCore().byId( $('#navigation button').eq(0).attr("id")).firePress();

https://answers.sap.com/questions/10107636/unable-to-trigger-a-click-event-in-onafterrenderin.html

Maybe it helps.

Kind regards Sebastian

Upvotes: 2

Related Questions