Question

Use Java script

Hi,
I followed below URL to make use Execute/Invoke methods.

https://community1.pega.com/community/pega-support/question/excutescriptinvoke-script

Replicated the same thing for training.openspan.com but couldn't achive login.

Used below code as javascript

  1. function raiseEvent(htmlId) {
  2. var target = document.getElementById(htmlId);
  3. var eventObject = document.createEvent("Event");
  4. eventObject.initEvent('input', true, false);
  5. target.dispatchEvent(eventObject);
  6. }

I am missing anything ? Need any property changes to use javascript

Correct Answer
November 12, 2019 - 1:14pm

The script should raise the keyup event and not input as you have for this specific control (the password).

function raiseEvent(htmlId) {
var target = document.getElementById(htmlId);
var eventObject = document.createEvent("Event");
eventObject.initEvent('keyup', true, false);
target.dispatchEvent(eventObject);
}

Comments

Keep up to date on this post and subscribe to comments

Pega
November 12, 2019 - 1:14pm

The script should raise the keyup event and not input as you have for this specific control (the password).

function raiseEvent(htmlId) {
var target = document.getElementById(htmlId);
var eventObject = document.createEvent("Event");
eventObject.initEvent('keyup', true, false);
target.dispatchEvent(eventObject);
}

November 13, 2019 - 1:54am
Response to tsasnett

Thanks for the reply.
If I know which event to be raised. I can make use of raise event method instead of using java script.
My concern is, when the page does not show any events in the developer mode but still setting data is not achieved.

In such cases how can we proceed.

Pega
November 13, 2019 - 8:05am
Response to chakri5122

Very rarely will the control itself show exactly what events to raise. You would then need to research the operation of the page with the developer tools available within the browser. We do have a script below we've used in the past that essentially raises all of the events at once. It's casting a pretty wide net though, but it makes discovering which ones are required a little easier. This would require one more script to get the 

 

function raiseEvents(elementId, eventsArr) {

    var events = eventsArr;

var target = document.getElementById(elementId);

 

    for(var i = 0; i < events.length; i++) {

try {

if (window.dispatchEvent) {

if (events[i].indexOf("mouse") >= 0) {

var eventObject = document.createEvent("MouseEvents");

eventObject.initMouseEvent(events[i], true, true, window, 0, eventObject.screenX, eventObject.screenY, eventObject.clientX, eventObject.clientY, eventObject.ctrlKey, eventObject.altKey, eventObject.shiftKey, eventObject.metaKey, 0, null);

console.log(events[i] + ": initialized as MouseEvents.");

}

else if (window.PointerEvent && events[i].indexOf("pointer") >= 0) {

var eventObject = document.createEvent("PointerEvent");

eventObject.initPointerEvent(events[i], true, true, window, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 42, "mouse", 0, 0);

//var eventObject = new PointerEvent(events[i], { bubbles: true, cancelable: true, composed: true, pointerId: 42, pointerType: "mouse", clientX: 0, clientY: 0 });

console.log(events[i] +A ": initialized as PointerEvent");

}

else if (events[i].indexOf("blur") >= 0 || events[i].indexOf("focus") >= 0 ) {

var eventObject = document.createEvent("FocusEvent");

eventObject.initEvent(events[i], true, false);

console.log(events[i] + ": initialized as FocusEvent");

}

else if (window.KeyboardEvent && events[i].indexOf("key") >= 0) {

var eventObject = document.createEvent("KeyboardEvent");

eventObject.initKeyboardEvent(events[i], true, true, window, 0, 0, 0, 0, 0, ('a').charCodeAt(0));

console.log(events[i] + ": initialized as KeyboardEvent");

}

else {

var eventObject = document.createEvent("Event");

eventObject.initEvent(events[i], true, false);

console.log(events[i] + ": initialized as Event");

}

target.dispatchEvent(eventObject);

console.log(events[i] + ": dispatch complete");

}

else {

var eventObject = document.createEventObject();

console.log("EventObject is initialized (IE8)");

eventObject.button=1;

target.fireEvent("on" + events[i], eventObject);

console.log("on" + events[i] + ": fire complete");

}

} catch (err) {

            console.log(events[i] + ": " + err.message);

        }

    } 

}

var hoverEvents = ["mousemove", "mouseover", "focus", "focusin"];

var clickEvents = ["mousedown", "mouseup", "click"];

var leaveEvents = ["focusout", "blur", "mouseout", "mouseleave"];

var hoverClickEvents  = ["mousemove", "mouseover", "focus", "mousedown", "mouseup", "click"];

var changeEvents = ["input", "change"];

var keyEvents = ['keydown', 'keyup', 'keypress'];

var noIdeaEvents= ["focus", "focusin", "mousemove", "hover", "mouseover", "mousedown", "mouseup", "click", "dblclick", "change", "mouseleave", "mouseout", "mouseenter", "blur", "focusout", "select", "pointermove", "pointerover", "pointerdown", "pointerup", "pointerout", "keydown", "keyup", "keypress", "input"];

//focus focusin mousemove hover mouseover mousedown mouseup click dblclick change mouseleave mouseout mouseenter blur focusout select pointermove pointerover pointerdown pointerup pointerout input

 

// |-------------------|

// | COMPRESSED SOURCE |

// |-------------------|

//function raiseEvents(e,o){for(var n=o,t=0;t<n.length;t++)try{if(window.dispatchEvent)n[t].indexOf("mouse")>=0?((i=document.createEvent("MouseEvents")).initMouseEvent(n[t],!0,!0,window,0,i.screenX,i.screenY,i.clientX,i.clientY,i.ctrlKey,i.altKey,i.shiftKey,i.metaKey,0,null),console.log(n[t]+": initialized as MouseEvents.")):window.PointerEvent&&n[t].indexOf("pointer")>=0?((i=document.createEvent("PointerEvent")).initPointerEvent(n[t],!0,!0,window,0,0,0,0,0,0,0,0,0,0,null,0,0,0,0,0,0,0,0,42,"mouse",0,0),console.log(n[t]+": initialized as PointerEvent")):n[t].indexOf("blur")>=0||n[t].indexOf("focus")>=0?((i=document.createEvent("FocusEvent")).initEvent(n[t],!0,!1),console.log(n[t]+": initialized as FocusEvent")):window.KeyboardEvent&&n[t].indexOf("key")>=0?((i=document.createEvent("KeyboardEvent")).initKeyboardEvent(n[t],!0,!0,window,0,0,0,0,0,"a".charCodeAt(0)),console.log(n[t]+": initialized as KeyboardEvent")):((i=document.createEvent("Event")).initEvent(n[t],!0,!1),console.log(n[t]+": initialized as Event")),e.dispatchEvent(i),console.log(n[t]+": dispatch complete");else{var i=document.createEventObject();console.log("EventObject is initialized (IE8)"),i.button=1,e.fireEvent("on"+n[t],i),console.log("on"+n[t]+": fire complete")}}catch(e){console.log(n[t]+": "+e.message)}}var hoverEvents=["mousemove","mouseover","focus","focusin"],clickEvents=["mousedown","mouseup","click"],leaveEvents=["focusout","blur","mouseout","mouseleave"],hoverClickEvents=["mousemove","mouseover","focus","mousedown","mouseup","click"],keyEvents=["keydown","keyup","keypress"],noIdeaEvents=["focus","focusin","mousemove","hover","mouseover","mousedown","mouseup","click","dblclick","change","mouseleave","mouseout","mouseenter","blur","focusout","select","pointermove","pointerover","pointerdown","pointerup","pointerout"];

Pega
November 13, 2019 - 9:34am

In our newer releases (19.1.14.0 and greater) there is a new feature that will in most cases eliminate the need to use Javascript to interact with web pages. You can now use SendKeys to populate these fields which will simulate what a user actually does and cause the appropriate events to be raised. We recommend this approach as the first step to resolving an issue such as yours.