UNPKG

@salesforce/salesforcedx-vscode-test-tools

Version:
144 lines 5.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getWorkbench = getWorkbench; exports.getBrowser = getBrowser; exports.reloadWindow = reloadWindow; exports.closeCurrentEditor = closeCurrentEditor; exports.closeAllEditors = closeAllEditors; exports.enableAllExtensions = enableAllExtensions; exports.showExplorerView = showExplorerView; exports.zoom = zoom; exports.zoomReset = zoomReset; exports.openNewTerminal = openNewTerminal; const vscode_extension_tester_1 = require("vscode-extension-tester"); const commandPrompt_1 = require("./commandPrompt"); const miscellaneous_1 = require("../core/miscellaneous"); /** * Gets a reference to the VSCode workbench * @returns The workbench instance for interacting with the VSCode UI */ function getWorkbench() { (0, miscellaneous_1.debug)('calling getWorkbench()'); return new vscode_extension_tester_1.Workbench(); } /** * Gets a reference to the WebDriver controlling the browser * @returns The WebDriver instance used for browser automation */ function getBrowser() { (0, miscellaneous_1.debug)('calling getBrowser()'); return vscode_extension_tester_1.VSBrowser.instance.driver; } /** * Reloads the VSCode window * @param predicateOrWait - Either a predicate with timeout or a duration to wait after reload */ async function reloadWindow(predicateOrWait = miscellaneous_1.Duration.milliseconds(0)) { (0, miscellaneous_1.log)(`Reloading window`); const prompt = await (0, commandPrompt_1.executeQuickPick)('Developer: Reload Window'); await handlePredicateOrWait(predicateOrWait, prompt); } /** * Closes the currently active editor tab */ async function closeCurrentEditor() { (0, miscellaneous_1.log)(`Closing current editor`); await (0, commandPrompt_1.executeQuickPick)('View: Close Editor'); await (0, miscellaneous_1.pause)(miscellaneous_1.Duration.seconds(1)); } /** * Closes all open editor tabs */ async function closeAllEditors() { (0, miscellaneous_1.log)(`Closing all editors`); await (0, commandPrompt_1.executeQuickPick)('View: Close All Editors'); await (0, miscellaneous_1.pause)(miscellaneous_1.Duration.seconds(1)); } /** * Enables all VSCode extensions */ async function enableAllExtensions() { (0, miscellaneous_1.log)(`Enabling all extensions`); await (0, commandPrompt_1.executeQuickPick)('Extensions: Enable All Extensions'); } /** * Opens the Explorer view in the Activity Bar * @throws Error if the Explorer view cannot be opened */ async function showExplorerView() { (0, miscellaneous_1.log)('Show Explorer'); const control = await new vscode_extension_tester_1.ActivityBar().getViewControl('Explorer'); if (!control) { throw new Error('Could not open Explorer view in activity bar'); } await control.openView(); } /** * Zooms the editor view in or out by a specified level * @param zoomIn - Direction to zoom, either 'In' or 'Out' * @param zoomLevel - Number of zoom steps to perform * @param wait - Duration to wait between zoom operations */ async function zoom(zoomIn, zoomLevel, wait = miscellaneous_1.Duration.seconds(1)) { await zoomReset(wait); for (let level = 0; level < zoomLevel; level++) { await (0, commandPrompt_1.executeQuickPick)(`View: Zoom ${zoomIn}`, wait); } } /** * Resets the editor zoom level to the default * @param wait - Duration to wait after resetting zoom */ async function zoomReset(wait = miscellaneous_1.Duration.seconds(1)) { await (0, commandPrompt_1.executeQuickPick)('View: Reset Zoom', wait); } /** * Opens a new terminal in the bottom panel */ async function openNewTerminal() { await new vscode_extension_tester_1.BottomBarPanel().openTerminalView(); } /** * Handles either waiting for a specified duration or until a predicate resolves * @param predicateOrWait - Either a duration to wait or a predicate with timeout * @param prompt - The prompt to pass to the predicate * @throws Error if the predicate fails or times out * @private */ async function handlePredicateOrWait(predicateOrWait, prompt) { (0, miscellaneous_1.log)('handlePredicateOrWait'); if ((0, miscellaneous_1.isDuration)(predicateOrWait)) { if (predicateOrWait.milliseconds > 0) { await (0, miscellaneous_1.pause)(predicateOrWait); } } else { const { predicate, maxWaitTime } = predicateOrWait; const safePredicate = withFailsafe(predicate, maxWaitTime, prompt); try { const result = await safePredicate(); if (result !== true) { throw new Error('Predicate did not resolve to true'); } } catch (error) { (0, miscellaneous_1.log)(`Predicate failed or timed out: ${error.message}`); throw error; } } } /** * Creates a failsafe version of a predicate that will timeout after a specified duration * @param predicate - The original predicate function * @param timeout - Maximum time to wait for the predicate to resolve * @param prompt - The prompt to pass to the predicate * @returns A failsafe predicate function that will reject if it times out * @private */ function withFailsafe(predicate, timeout, prompt) { return async function () { const timeoutPromise = new Promise((_, reject) => setTimeout(() => reject(new Error('Predicate timed out')), timeout.milliseconds)); return Promise.race([predicate(prompt), timeoutPromise]); }; } //# sourceMappingURL=workbench.js.map