← Files Flows by plugin.spaceARCHIVED FILE

lib/capture-session.mjs

2.42 KB · Nov 30, 2026 · 00:17 UTC

↓ Download file

export function isHttpUrl(value) {
  try { const url = new URL(value); return ['http:', 'https:'].includes(url.protocol) && !url.username && !url.password; } catch { return false; }
}
export const MANUAL_LOGIN_MESSAGE = 'Open the application in a browser the user can access and ask them to sign in there. Continue capture in that same browser session; do not request passwords, codes or session files.';
export function validateSteps(steps) {
  if (!Array.isArray(steps) || steps.length > 40) throw new Error('Use up to 40 capture steps.');
  for (const step of steps) {
    if (!step || !['click', 'fill', 'press', 'select', 'wait'].includes(step.action) || typeof step.selector !== 'string' || !step.selector || step.selector.length > 1000) throw new Error('Each step needs an action and selector.');
    if ('valueEnv' in step) throw new Error(`Capture no longer reads environment values. ${MANUAL_LOGIN_MESSAGE}`);
    if (['fill', 'press', 'select'].includes(step.action) && (typeof step.value !== 'string' || step.value.length > 8000)) throw new Error('Supply a value for a text step.');
    if (step.action === 'wait' && step.state !== undefined && !['visible', 'hidden', 'attached', 'detached'].includes(step.state)) throw new Error('Unsupported wait state.');
  }
}
export async function applySteps(page, steps) {
  for (const [index, step] of steps.entries()) {
    try {
      const locator = page.locator(step.selector);
      if (step.action !== 'wait' && await locator.evaluate(element => element.matches('input[type="password" i], [autocomplete~="current-password" i], [autocomplete~="new-password" i], [autocomplete~="one-time-code" i]'))) throw new Error(MANUAL_LOGIN_MESSAGE);
      const value = step.value;
      if (step.action === 'click') await locator.click();
      else if (step.action === 'fill') await locator.fill(value);
      else if (step.action === 'press') await locator.press(value);
      else if (step.action === 'select') await locator.selectOption(value);
      else await locator.waitFor({ state: step.state ?? 'visible' });
    } catch (error) { error.captureStep = `${index + 1} (${step.action})`; throw error; }
  }
}
export async function verifyUrl(page, expectedUrl, timeout) {
  const expected = new URL(expectedUrl);
  await page.waitForURL(url => url.origin === expected.origin && url.pathname === expected.pathname && url.search === expected.search && url.hash === expected.hash, { timeout, waitUntil: 'domcontentloaded' });
}

SHA-256: 28cad12f743e0c01d4a5e84c4b6f20f968b760f035d03f7007f92440ee217333