UNPKG

@salesforce/salesforcedx-vscode-test-tools

Version:
158 lines 7.83 kB
"use strict"; /* * Copyright (c) 2025, salesforce.com, inc. * All rights reserved. * Licensed under the BSD 3-Clause license. * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ Object.defineProperty(exports, "__esModule", { value: true }); exports.waitForNotificationToGoAway = waitForNotificationToGoAway; exports.notificationIsPresent = notificationIsPresent; exports.notificationIsPresentWithTimeout = notificationIsPresentWithTimeout; exports.notificationIsAbsent = notificationIsAbsent; exports.notificationIsAbsentWithTimeout = notificationIsAbsentWithTimeout; exports.dismissNotification = dismissNotification; exports.acceptNotification = acceptNotification; exports.dismissAllNotifications = dismissAllNotifications; const miscellaneous_1 = require("../core/miscellaneous"); const workbench_1 = require("./workbench"); const commandPrompt_1 = require("./commandPrompt"); const vscode_extension_tester_1 = require("vscode-extension-tester"); /** * Waits for a notification with the specified message to disappear * @param notificationMessage - Regular expression to match the notification message * @param durationInSeconds - Maximum duration to wait for the notification to go away */ async function waitForNotificationToGoAway(notificationMessage, durationInSeconds) { await findNotification(notificationMessage, false, durationInSeconds, true); } /** * Checks if a notification with the specified message is present * @param notificationMessage - Regular expression to match the notification message * @returns True if the notification is present, false otherwise */ async function notificationIsPresent(notificationMessage) { const notification = await findNotification(notificationMessage, true, miscellaneous_1.Duration.milliseconds(500)); return notification ? true : false; } /** * Checks if a notification with the specified message is present within a timeout period * @param notificationMessage - Regular expression to match the notification message * @param durationInSeconds - Maximum duration to wait for the notification to appear * @returns True if the notification is present within the timeout, false otherwise */ async function notificationIsPresentWithTimeout(notificationMessage, durationInSeconds) { const notification = await findNotification(notificationMessage, true, durationInSeconds); return notification ? true : false; } /** * Checks if a notification with the specified message is absent * @param notificationMessage - Regular expression to match the notification message * @returns True if the notification is absent, false otherwise */ async function notificationIsAbsent(notificationMessage) { const notification = await findNotification(notificationMessage, false, miscellaneous_1.Duration.milliseconds(500)); return notification ? false : true; } /** * Checks if a notification with the specified message is absent within a timeout period * @param notificationMessage - Regular expression to match the notification message * @param durationInSeconds - Maximum duration to wait for the notification to be absent * @returns True if the notification is absent within the timeout, false otherwise */ async function notificationIsAbsentWithTimeout(notificationMessage, durationInSeconds) { const notification = await findNotification(notificationMessage, false, durationInSeconds); return notification ? false : true; } /** * Dismisses a notification with the specified message * @param notificationMessage - Regular expression to match the notification message * @param timeout - Maximum duration to wait for the notification to appear before dismissing */ async function dismissNotification(notificationMessage, timeout = miscellaneous_1.Duration.seconds(1)) { const notification = await findNotification(notificationMessage, true, timeout, true); notification?.close(); } /** * Accepts a notification by clicking an action button * @param notificationMessage - The notification message to look for * @param actionName - The name of the action button to click * @param timeout - Maximum duration to wait for the notification * @returns True if the action was successfully clicked, false otherwise */ async function acceptNotification(notificationMessage, actionName, timeout) { console.log(`${notificationMessage}, ${actionName}, ${timeout}`); await (0, commandPrompt_1.executeQuickPick)('Notifications: Show Notifications', miscellaneous_1.Duration.seconds(1)); const actionButtons = await (0, workbench_1.getBrowser)().findElements(vscode_extension_tester_1.By.css(`div.notification-list-item-buttons-container > a.monaco-button.monaco-text-button`)); for (const button of actionButtons) { if ((await button.getText()).includes(actionName)) { (0, miscellaneous_1.log)(`button ${actionName} found`); await button.click(); return true; } } return false; } /** * Dismisses all notifications in the notifications center */ async function dismissAllNotifications() { (0, miscellaneous_1.log)(`calling dismissAllNotifications()`); await (0, commandPrompt_1.executeQuickPick)('Notifications: Clear All Notifications'); } /** * Finds a notification with the specified message * @param message - Regular expression to match the notification message * @param shouldBePresent - If true, looks for the notification to be present; if false, waits for it to be absent * @param timeout - Maximum duration to wait for the notification state to match shouldBePresent * @param throwOnTimeout - If true, throws an error if the timeout is reached; if false, returns null * @returns The notification object if found and shouldBePresent is true, or null in other cases * @throws Error if throwOnTimeout is true and the timeout is reached * @private */ async function findNotification(message, shouldBePresent, timeout = miscellaneous_1.Duration.milliseconds(500), throwOnTimeout = false // New parameter to control throwing on timeout ) { const workbench = (0, workbench_1.getWorkbench)(); const timeoutMessage = `Notification with message "${message}" ${shouldBePresent ? 'not found' : 'still present'} within the specified timeout of ${timeout.seconds} seconds.`; const getMatchingNotification = async () => { try { await workbench.openNotificationsCenter(); } catch (error) { (0, miscellaneous_1.log)(`Error opening notifications center: ${error}`); await (0, commandPrompt_1.executeQuickPick)('Notifications: Show Notifications'); } const notifications = await workbench.getNotifications(); for (const notification of notifications) { const notificationMessage = await notification.getMessage(); if (message.test(notificationMessage)) { return notification; } } return null; }; try { const endTime = Date.now() + timeout.milliseconds; let foundNotification = null; // Retry until timeout is reached or the notification status matches `shouldBePresent` do { foundNotification = await getMatchingNotification(); if (foundNotification) { return foundNotification; } await new Promise(res => setTimeout(res, 100)); // Short delay before retrying } while (Date.now() < endTime); // Throw or return based on `throwOnTimeout` if (throwOnTimeout) { throw new Error(timeoutMessage); } return null; } catch (error) { if (throwOnTimeout) { throw error; } return null; } } //# sourceMappingURL=notifications.js.map