super-productivity/e2e/utils/element-helpers.ts
Johannes Millan 13d7afc458 refactor(e2e): extract ensureGlobalAddTaskBarOpen helper to reduce code duplication
Extract ensureGlobalAddTaskBarOpen() to e2e/utils/element-helpers.ts to avoid
duplicating the logic for opening the global add task bar across multiple tests.

This helper properly waits for the add button and input to be visible,
preventing race conditions in tests.
2026-01-16 13:35:59 +01:00

58 lines
1.8 KiB
TypeScript

import type { Locator, Page } from '@playwright/test';
/**
* Safely checks if an element is visible, returning false on any error.
* Use this instead of `.isVisible().catch(() => false)` pattern.
*
* @param locator - Playwright locator to check
* @param timeout - Optional timeout in ms (default uses Playwright's default)
* @returns Promise<boolean> - true if visible, false otherwise
*/
export const safeIsVisible = async (
locator: Locator,
timeout?: number,
): Promise<boolean> => {
try {
return await locator.isVisible({ timeout });
} catch {
return false;
}
};
/**
* Safely checks if an element is enabled, returning false on any error.
* Use this instead of `.isEnabled().catch(() => false)` pattern.
*
* @param locator - Playwright locator to check
* @returns Promise<boolean> - true if enabled, false otherwise
*/
export const safeIsEnabled = async (locator: Locator): Promise<boolean> => {
try {
return await locator.isEnabled();
} catch {
return false;
}
};
/**
* Ensures the global add task bar is open and returns the input locator.
* If the bar is closed, it will click the add button to open it.
* Uses proper condition-based waiting to avoid race conditions.
*
* @param page - Playwright page object
* @returns Promise<Locator> - The add task input locator, ready for interaction
*/
export const ensureGlobalAddTaskBarOpen = async (page: Page): Promise<Locator> => {
const ADD_TASK_INPUT = 'add-task-bar.global input';
const addTaskInput = page.locator(ADD_TASK_INPUT).first();
const isVisible = await addTaskInput.isVisible().catch(() => false);
if (!isVisible) {
const addBtn = page.locator('.tour-addBtn').first();
await addBtn.waitFor({ state: 'visible', timeout: 10000 });
await addBtn.click();
}
await addTaskInput.waitFor({ state: 'visible', timeout: 10000 });
return addTaskInput;
};