generateFromRecording function ✓ 100.0%

Last updated: 2026-03-01T23:25:47.084Z

Metrics

LOC: 56 Complexity: 18 Params: 3 Coverage: 100.0% (24/24 lines, 23x executed)

Signature

generateFromRecording( steps: RecordedStep[], options?: RecordingGenerateOptions, ): : string

Architecture violations

View all

  • [warning] max-cyclomatic-complexity: 'generateFromRecording' has cyclomatic complexity 18 (max 10)

Source Code

function generateFromRecording(
  steps: RecordedStep[],
  options?: RecordingGenerateOptions,
): string {
  const opts = options ?? {};
  const testName = opts.testName ?? "recorded test";
  const useSmartSelectors = opts.useSmartSelectors !== false;
  const minWait = opts.minWaitThreshold ?? 1000;

  const lines: string[] = [];

  for (let i = 0; i < steps.length; i++) {
    const step = steps[i];

    // Skip steps based on options
    if (step.type === "scroll" && !opts.includeScrollSteps) continue;
    if (step.type === "hover" && !opts.includeHoverSteps) continue;
    // Skip the initial navigate if it matches pageUrl (already in goto)
    if (i === 0 && step.type === "navigate" && opts.pageUrl) continue;

    // Insert explicit wait for significant pauses between actions
    if (i > 0) {
      const delay = shouldInsertDelay(step, steps[i - 1], minWait);
      if (delay !== null) {
        lines.push(`  // User paused for ~${Math.round(delay / 1000)}s`);
        lines.push(`  await page.waitForTimeout(${delay});`);
      }
    }

    lines.push(recordedStepLine(step, useSmartSelectors));
  }

  const urlLine = opts.pageUrl
    ? `  await page.goto('${escapeString(opts.pageUrl)}');\n\n`
    : "";

  const assertionLines =
    opts.includeAssertions && opts.assertions?.length
      ? ["\n  // Assertions", ...opts.assertions.map(assertionLine)]
      : [];

  const parts = [
    `import { test, expect } from '@playwright/test';`,
    ``,
    `test('${escapeString(testName)}', async ({ page }) => {`,
    urlLine + lines.join("\n"),
  ];

  if (assertionLines.length > 0) {
    parts.push(assertionLines.join("\n"));
  }

  parts.push(`});`);
  parts.push("");
  return parts.join("\n");
}

Dependencies (Outgoing)

graph LR generateFromRecording["generateFromRecording"] shouldInsertDelay["shouldInsertDelay"] recordedStepLine["recordedStepLine"] escapeString["escapeString"] generateFromRecording -->|calls| shouldInsertDelay generateFromRecording -->|calls| recordedStepLine generateFromRecording -->|calls| escapeString style generateFromRecording fill:#dbeafe,stroke:#2563eb,stroke-width:2px click generateFromRecording "bc12aadd5ae3e7fb.html" click shouldInsertDelay "7d9c1d11ee65d3a9.html" click recordedStepLine "b05ab4bbb41f5984.html" click escapeString "5c2f25e3b4548c2d.html"
TargetType
shouldInsertDelay calls
recordedStepLine calls
escapeString calls

No incoming dependencies.