@salesforce/salesforcedx-vscode-test-tools
Version:
Test automation framework for Salesforce Extensions for VS Code
202 lines • 9.35 kB
JavaScript
;
/*
* 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.isCommandAvailable = void 0;
exports.openCommandPromptWithCommand = openCommandPromptWithCommand;
exports.runCommandFromCommandPrompt = runCommandFromCommandPrompt;
exports.selectQuickPickWithText = selectQuickPickWithText;
exports.selectQuickPickItem = selectQuickPickItem;
exports.findQuickPickItem = findQuickPickItem;
exports.waitForQuickPick = waitForQuickPick;
exports.executeQuickPick = executeQuickPick;
exports.clickFilePathOkButton = clickFilePathOkButton;
const miscellaneous_1 = require("../core/miscellaneous");
const retryUtils_1 = require("../retryUtils");
const workbench_1 = require("./workbench");
const vscode_extension_tester_1 = require("vscode-extension-tester");
/**
* Opens the command prompt and enters a command
* @param workbench - The VSCode workbench instance
* @param command - The command to enter in the prompt
* @returns The InputBox or QuickOpenBox representing the command prompt
*/
async function openCommandPromptWithCommand(workbench, command) {
const prompt = await (await workbench.openCommandPrompt()).wait();
await (await prompt.wait()).setText(`>${command}`);
return prompt;
}
/**
* Runs a command from the command prompt
* @param workbench - The VSCode workbench instance
* @param command - The command to run
* @param durationInSeconds - Optional duration to wait after running the command
* @returns The InputBox or QuickOpenBox representing the command prompt
*/
async function runCommandFromCommandPrompt(workbench, command, durationInSeconds = miscellaneous_1.Duration.seconds(0)) {
const prompt = await (await openCommandPromptWithCommand(workbench, command)).wait();
await selectQuickPickItem(prompt, command);
if (durationInSeconds.milliseconds > 0) {
await (0, miscellaneous_1.pause)(durationInSeconds);
}
return prompt;
}
/**
* Selects a quick pick option with the specified text
* @param prompt - The command prompt
* @param text - The text of the quick pick option to select
*/
async function selectQuickPickWithText(prompt, text) {
// Set the text in the command prompt. Only selectQuickPick() needs to be called, but setting
// the text in the command prompt is a nice visual feedback to anyone watching the tests run.
await prompt.setText(text);
await (0, miscellaneous_1.pause)(miscellaneous_1.Duration.seconds(1));
await prompt.selectQuickPick(text);
await (0, miscellaneous_1.pause)(miscellaneous_1.Duration.seconds(1));
// After the text has been entered and selectQuickPick() is called, you might see the last few characters
// in the input box be deleted. This is b/c selectQuickPick() calls resetPosition(), which for some reason
// deletes the last two characters. This doesn't seem to affect the outcome though.
}
/**
* Selects a quick pick item with the exact text
* @param prompt - The command prompt
* @param text - The exact text of the quick pick item to select
* @throws Error if the prompt is undefined or if the quick pick item is not found
*/
async function selectQuickPickItem(prompt, text) {
if (!prompt) {
throw new Error('Prompt cannot be undefined');
}
const quickPick = await prompt.findQuickPick(text);
if (!quickPick || (await quickPick.getLabel()) !== text) {
throw new Error(`Quick pick item ${text} was not found`);
}
await quickPick.select();
await (0, miscellaneous_1.pause)(miscellaneous_1.Duration.seconds(1));
}
/**
* Finds a quick pick item in the command prompt
* @param inputBox - The command prompt
* @param quickPickItemTitle - The text to search for in quick pick items
* @param useExactMatch - If true, looks for exact match; if false, checks if item contains the text
* @param selectTheQuickPickItem - If true, selects the item when found
* @returns True if the item was found, false otherwise
*/
async function findQuickPickItem(inputBox, quickPickItemTitle, useExactMatch, selectTheQuickPickItem) {
if (!inputBox) {
return false;
}
// Type the text into the filter. Do this in case the pick list is long and
// the target item is not visible (and one needs to scroll down to see it).
await inputBox.setText(quickPickItemTitle);
await (0, miscellaneous_1.pause)(miscellaneous_1.Duration.seconds(1));
let itemWasFound = false;
const quickPicks = await inputBox.getQuickPicks();
for (const quickPick of quickPicks) {
const label = await quickPick.getLabel();
if (useExactMatch && label === quickPickItemTitle) {
itemWasFound = true;
}
else if (!useExactMatch && label.includes(quickPickItemTitle)) {
itemWasFound = true;
}
if (itemWasFound) {
if (selectTheQuickPickItem) {
await quickPick.select();
await (0, miscellaneous_1.pause)(miscellaneous_1.Duration.seconds(1));
}
return true;
}
}
return false;
}
/**
* Waits for a quick pick item to appear in the command prompt
* @param prompt - The command prompt
* @param pickListItem - The text of the quick pick item to wait for
* @param options - Optional configuration including timeout and error message
*/
async function waitForQuickPick(prompt, pickListItem, options = { timeout: miscellaneous_1.Duration.milliseconds(10_000) }) {
await (0, workbench_1.getBrowser)().wait(async () => {
try {
await findQuickPickItem(prompt, pickListItem, false, true);
return true;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
}
catch (_) {
return false;
}
}, options.timeout?.milliseconds, options.msg ?? `Expected to find option ${pickListItem} before ${options.timeout} milliseconds`, 500 // Check every 500 ms
);
}
/**
* Runs exact command from command palette
* @param command - The command to execute
* @param wait - Duration to wait after executing the command; default is 1 second
* @returns The command prompt interface
* @throws Error if the command is not found or execution fails
*/
async function executeQuickPick(command, wait = miscellaneous_1.Duration.seconds(1)) {
(0, miscellaneous_1.log)(`executeQuickPick command: ${command}`);
return await (0, retryUtils_1.retryOperation)(async () => {
const workbench = (0, workbench_1.getWorkbench)();
const inputBox = await workbench.openCommandPrompt();
await (0, miscellaneous_1.pause)(miscellaneous_1.Duration.seconds(2));
await inputBox.wait();
await inputBox.setText(`>${command}`);
await inputBox.selectQuickPick(command);
await (0, miscellaneous_1.pause)(wait);
(0, miscellaneous_1.log)(`executeQuickPick command: ${command} - done`);
return inputBox;
}, 3, `executeQuickPick command: ${command} - failed`);
}
/**
* Clicks the OK button in a file path dialog
* Also handles the Overwrite confirmation if a folder already exists
* @throws Error if the OK button is not found
*/
async function clickFilePathOkButton() {
const browser = (0, workbench_1.getBrowser)();
const okButton = await browser.findElement(vscode_extension_tester_1.By.css('*:not([style*="display: none"]).quick-input-action .monaco-button'));
if (!okButton) {
throw new Error('Ok button not found');
}
await (0, miscellaneous_1.pause)(miscellaneous_1.Duration.milliseconds(500));
await okButton.sendKeys(vscode_extension_tester_1.Key.ENTER);
await (0, miscellaneous_1.pause)(miscellaneous_1.Duration.seconds(1));
const buttons = await browser.findElements(vscode_extension_tester_1.By.css('a.monaco-button.monaco-text-button'));
for (const item of buttons) {
const text = await item.getText();
if (text.includes('Overwrite')) {
(0, miscellaneous_1.log)('clickFilePathOkButton() - folder already exists');
await browser.wait(async () => (await item.isDisplayed()) && (await item.isEnabled()), miscellaneous_1.Duration.seconds(5).milliseconds, `Overwrite button not clickable within 5 seconds`);
await item.click();
break;
}
}
await (0, miscellaneous_1.pause)(miscellaneous_1.Duration.seconds(2));
}
/**
* Checks if a VSCode command is available.
* @param commandName - Name of the VSCode command to check
* @returns boolean - true if the command is available, false otherwise
*/
const isCommandAvailable = async (commandName) => {
(0, miscellaneous_1.log)('Checking if command is available: ' + commandName);
const workbench = (0, workbench_1.getWorkbench)();
const prompt = await workbench.openCommandPrompt();
await prompt.setText(`>${commandName}`);
const availableCommands = await prompt.getQuickPicks();
for (const item of availableCommands) {
if ((await item.getLabel()) === commandName) {
return true;
}
}
return false;
};
exports.isCommandAvailable = isCommandAvailable;
//# sourceMappingURL=commandPrompt.js.map