"Do you want to leave this Site" Dialog stops TC script

  • KM03695092
  • 01-Sep-2020
  • 13-Nov-2020

Summary

Here is a scripting level hotfix that will able to prevent "Do you want to leave this Site" Dialog showing.

Question

Some AUT will popup a "Do you want to leave this Site" Dialog, which stops TC replaying.

To suppress this dialog, we can add an evaluate javascript step before the step who "creates" the popup dialog.

The javascript step will contain the following code:

   
// Inject this code to all windows
function injectToDoc(doc) {
var scr = document.createElement("script");
scr.type = "text/javascript";
scr.innerHTML = `
window.addEventListener(
"beforeunload",
function(e) {

var rreState = window.top.document.documentElement.getAttribute("hp_attr_state");
if (rreState === "replay") {
console.log("*** setting e.returnValue");
e.returnValue = "TruClient_We_will_let_you_go_";
}
}, true
);
`;

doc.head.appendChild(scr);
}

function doInjection(win) {
injectToDoc(win.document);
const iframes = win.document.querySelectorAll('iframe');
if (iframes.length > 0) {
for (let i = 0; i < iframes.length; ++i) {
console.log(`injecting to iframe: ${iframes[i].id}`);
doInjection(iframes[i].contentWindow);
}
}
}
doInjection(window);