Skip to content

Commit

Permalink
add back beforeunload
Browse files Browse the repository at this point in the history
- fix the test by updating the script
  • Loading branch information
NivedhaSenthil committed May 24, 2024
1 parent f8cc4ac commit 9b803ea
Show file tree
Hide file tree
Showing 6 changed files with 2,994 additions and 12,552 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ docs/layout/partials/content.html
#local chromium
.local-chromium

#asdf tool-version
.tool-versions

#test report
test/functional-tests/reports/
test/functional-tests/.gauge/
Expand Down
1 change: 0 additions & 1 deletion .tool-versions

This file was deleted.

12 changes: 11 additions & 1 deletion lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,23 @@ const connect_to_cri = async (target, options = {}) => {
return initCRI(tgt, numRetries, options);
};

const closeConnection = async () => {
const closeConnection = async (promisesToBeResolvedBeforeCloseBrowser) => {
if (_client) {
// remove listeners other than JS dialog for beforeUnload on client first to stop executing them when closing
await _client.removeAllListeners();
pageHandler.addJavascriptDialogOpeningListener();
if (!defaultConfig.firefox) {
await pageHandler.closePage();

await new Promise((resolve) => {
let timeout = setTimeout(() => {
resolve();
}, defaultConfig.retryTimeout);
Promise.all(promisesToBeResolvedBeforeCloseBrowser).then(() => {
clearTimeout(timeout);
resolve();
});
});
}
}
defaultConfig.connectedToRemoteBrowser ? await _client.Browser.close() : await closeBrowser();
Expand Down
23 changes: 16 additions & 7 deletions lib/taiko.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ module.exports.closeBrowser = async () => {

const _closeBrowser = async () => {
fetchHandler.resetInterceptors();
await closeConnection();
await closeConnection(promisesToBeResolvedBeforeCloseBrowser);
};

/**
Expand Down Expand Up @@ -2223,11 +2223,7 @@ module.exports.confirm = (message, callback) => dialog('confirm', message, callb
*
* @param {function} callback - Action to perform. Accept/Dismiss.
*/
module.exports.beforeunload = () => {
console.warn(
'beforeunload handler is not supported any more refer https://developer.chrome.com/docs/web-platform/deprecating-unload',
);
};
module.exports.beforeunload = (callback) => dialog('beforeunload', '', callback);

/**
* Evaluates script on element matching the given selector.
Expand Down Expand Up @@ -2520,8 +2516,18 @@ module.exports.getConfig = getConfig;
*/
module.exports.setConfig = setConfig;

const promisesToBeResolvedBeforeCloseBrowser = [];

const dialog = (dialogType, dialogMessage, callback) => {
validate();
let resolver = null;
if (dialogType === 'beforeunload') {
promisesToBeResolvedBeforeCloseBrowser.push(
new Promise((resolve) => {
resolver = resolve;
}),
);
}
let eventName = '';
if (isFunction(dialogMessage)) {
eventName = dialogType;
Expand All @@ -2530,7 +2536,10 @@ const dialog = (dialogType, dialogMessage, callback) => {
eventName = createJsDialogEvent(dialogMessage, dialogType);
eventRegexMap.set(eventName, new RegExp(dialogMessage));
}
return eventHandler.once(eventName, callback);
return eventHandler.once(eventName, async (args) => {
await callback(args);
resolver && resolver();
});
};

const createJsDialogEvent = (message, dType) => {
Expand Down
Loading

0 comments on commit 9b803ea

Please sign in to comment.