Skip to content

Commit

Permalink
Deprecate beforeunload
Browse files Browse the repository at this point in the history
  • Loading branch information
zabil committed May 17, 2024
1 parent 09c12af commit d1d3554
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 31 deletions.
15 changes: 2 additions & 13 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,24 +126,13 @@ const connect_to_cri = async (target, options = {}) => {
return initCRI(tgt, numRetries, options);
};

const closeConnection = async (promisesToBeResolvedBeforeCloseBrowser) => {
const closeConnection = async () => {
if (_client) {
// remove listeners other than JS dialog for beforeUnload on client first to stop executing them when closing
await _client.removeAllListeners();
pageHandler.addJavascriptDialogOpeningListener();
//TODO: Remove check once fixed https://bugs.chromium.org/p/chromium/issues/detail?id=1147809
if (!defaultConfig.firefox && !(process.platform == 'win32' && defaultConfig.headful)) {
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
24 changes: 7 additions & 17 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(promisesToBeResolvedBeforeCloseBrowser);
await closeConnection();
};

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

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

const promisesToBeResolvedBeforeCloseBrowser = [];

const dialog = (dialogType, dialogMessage, callback) => {
validate();
// TODO: Commented to fix issue with browser closing for unresolved events
// let resolver = null;
// if (dialogType === 'beforeunload') {
// promisesToBeResolvedBeforeCloseBrowser.push(
// new Promise((resolve) => {
// resolver = resolve;
// }),
// );
// }
let eventName = '';
if (isFunction(dialogMessage)) {
eventName = dialogType;
Expand All @@ -2537,10 +2530,7 @@ const dialog = (dialogType, dialogMessage, callback) => {
eventName = createJsDialogEvent(dialogMessage, dialogType);
eventRegexMap.set(eventName, new RegExp(dialogMessage));
}
return eventHandler.once(eventName, async (args) => {
await callback(args);
// resolver && resolver();
});
return eventHandler.once(eventName, callback);
};

const createJsDialogEvent = (message, dType) => {
Expand Down
3 changes: 2 additions & 1 deletion test/unit-tests/beforeunload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ let {
let { createHtml, removeFile, openBrowserArgs } = require('./test-util');
const test_name = 'beforeunload';

// TODO: Fix this test before merge
// TODO: Skip this test and remove the unload functionality
// Refer:https://developer.chrome.com/docs/web-platform/deprecating-unload
xdescribe(test_name, () => {
let filePath, filePath1;
let called = false;
Expand Down

0 comments on commit d1d3554

Please sign in to comment.