tui-tester
Version:
End-to-end testing framework for terminal user interfaces
173 lines (172 loc) • 5.39 kB
JavaScript
// src/helpers/interactions.ts
async function navigateMenu(tester, direction, count = 1) {
for (let i = 0; i < count; i++) {
await tester.sendKey(direction);
await tester.sleep(100);
}
}
async function selectMenuItem(tester, itemText, maxAttempts = 10) {
for (let i = 0; i < maxAttempts; i++) {
const screen = await tester.getScreenText();
if (screen.includes(`> ${itemText}`) || screen.includes(`\u25CF ${itemText}`)) {
await tester.sendKey("enter");
return;
}
await tester.sendKey("down");
await tester.sleep(100);
}
throw new Error(`Menu item "${itemText}" not found after ${maxAttempts} attempts`);
}
async function fillField(tester, fieldName, value) {
await tester.waitForText(fieldName);
await tester.sendKey("a", { ctrl: true });
await tester.sendKey("delete");
await tester.typeText(value);
await tester.sendKey("tab");
}
async function submitForm(tester, formData) {
if (formData) {
for (const [field, value] of Object.entries(formData)) {
await tester.waitForText(field, { timeout: 5e3 });
await tester.typeText(value);
await tester.sendKey("enter");
await tester.sleep(200);
}
} else {
await tester.sendKey("enter");
}
}
async function cancel(tester) {
await tester.sendKey("escape");
}
async function confirmDialog(tester, accept = true) {
if (accept) {
await tester.sendKey("y");
} else {
await tester.sendKey("n");
}
}
async function scroll(tester, direction, count = 1) {
const key = direction === "up" || direction === "down" ? direction : direction.toLowerCase();
for (let i = 0; i < count; i++) {
await tester.sendKey(key);
await tester.sleep(50);
}
}
async function clickOnText(tester, text) {
const screen = await tester.getScreenText();
const lines = screen.split("\n");
for (let y = 0; y < lines.length; y++) {
const x = lines[y].indexOf(text);
if (x !== -1) {
await tester.sendMouse({
type: "click",
position: { x, y },
button: "left"
});
return;
}
}
throw new Error(`Text "${text}" not found on screen`);
}
async function clickAt(tester, position, button = "left") {
await tester.sendMouse({
type: "click",
position,
button
});
}
async function drag(tester, from, to) {
await tester.sendMouse({
type: "down",
position: from,
button: "left"
});
await tester.sleep(100);
await tester.sendMouse({
type: "drag",
position: to,
button: "left"
});
await tester.sleep(100);
await tester.sendMouse({
type: "up",
position: to,
button: "left"
});
}
async function selectText(tester, startText, endText) {
const screen = await tester.getScreenText();
const lines = screen.split("\n");
let startPos = null;
let endPos = null;
for (let y = 0; y < lines.length; y++) {
const startX = lines[y].indexOf(startText);
if (startX !== -1 && !startPos) {
startPos = { x: startX, y };
}
const endX = lines[y].indexOf(endText);
if (endX !== -1) {
endPos = { x: endX + endText.length - 1, y };
}
}
if (!startPos || !endPos) {
throw new Error("Could not find text positions for selection");
}
await drag(tester, startPos, endPos);
}
async function copySelection(tester) {
await tester.sendKey("c", { ctrl: true });
}
async function pasteFromClipboard(tester) {
await tester.sendKey("v", { ctrl: true });
}
async function executeCommand(tester, command) {
await tester.sendText(command);
await tester.sendKey("enter");
}
async function waitForPrompt(tester, prompt = "$", options) {
await tester.waitForText(prompt, options);
}
async function login(tester, username, password) {
await tester.waitForText("Username:");
await tester.typeText(username);
await tester.sendKey("enter");
await tester.waitForText("Password:");
await tester.typeText(password);
await tester.sendKey("enter");
}
async function switchTab(tester, tabIndex) {
await tester.sendKey(tabIndex.toString(), { alt: true });
}
async function openCommandPalette(tester) {
await tester.sendKey("p", { ctrl: true, shift: true });
}
async function search(tester, searchText) {
const screen = await tester.getScreenText();
return screen.includes(searchText);
}
async function exitApplication(tester, force = false) {
if (force) {
await tester.sendKey("c", { ctrl: true });
} else {
await tester.sendKey("q");
}
}
async function waitForLoading(tester, options) {
const indicators = ["Loading...", "Please wait...", "\u280B", "\u2819", "\u2839", "\u2838", "\u283C", "\u2834", "\u2826", "\u2827", "\u2807", "\u280F"];
await tester.waitForPattern(
new RegExp(`^(?!.*(${indicators.join("|")})).*$`, "s"),
options
);
}
async function takeAnnotatedSnapshot(tester, name, annotations) {
const snapshot = await tester.takeSnapshot(name);
if (annotations && snapshot.metadata) {
snapshot.metadata.annotations = annotations;
}
await tester.saveSnapshot(snapshot);
}
export { cancel, clickAt, clickOnText, confirmDialog, copySelection, drag, executeCommand, exitApplication, fillField, login, navigateMenu, openCommandPalette, pasteFromClipboard, scroll, search, selectMenuItem, selectText, submitForm, switchTab, takeAnnotatedSnapshot, waitForLoading, waitForPrompt };
//# sourceMappingURL=interactions.js.map
//# sourceMappingURL=interactions.js.map