detectAssertions function exported ✓ 100.0%

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

Metrics

LOC: 77 Complexity: 13 Params: 4 Coverage: 100.0% (20/20 lines, 11x executed)

Signature

detectAssertions( actions: CapturedAction[], pageUrl: string, capturedResponses?: CapturedHttpResponse[], ): : E2EAssertion[]

Summary

Detects potential success assertions for a form submit. Analyzes the page for common patterns of success feedback and uses captured HTTP responses to generate response assertions.

Architecture violations

View all

  • [warning] max-cyclomatic-complexity: 'detectAssertions' has cyclomatic complexity 13 (max 10)

Source Code

export function detectAssertions(
  actions: CapturedAction[],
  pageUrl: string,
  capturedResponses?: CapturedHttpResponse[],
): E2EAssertion[] {
  const assertions: E2EAssertion[] = [];

  // 1. If there's a submit action, expect URL might change
  const hasSubmit = actions.some(
    (a) => a.actionType === "click" || a.actionType === "submit",
  );

  if (hasSubmit) {
    assertions.push({
      type: "url-changed",
      expected: pageUrl,
      description: "URL should change after form submit",
    });
  }

  // 2. Look for common success message containers
  const successSelectors = [
    ".alert-success",
    ".toast-success",
    ".notification-success",
    "[role='alert']",
    ".success-message",
    ".flash-success",
    ".Toastify__toast--success",
    ".ant-message-success",
    ".MuiAlert-standardSuccess",
  ];

  for (const sel of successSelectors) {
    if (document.querySelector(sel)) {
      assertions.push({
        type: "element-visible",
        selector: sel,
        description: `Success element "${sel}" should be visible`,
      });
      break; // One success assertion is enough
    }
  }

  // 3. If form has action attribute with different URL, expect redirect
  const forms = document.querySelectorAll("form[action]");
  for (const form of forms) {
    const action = form.getAttribute("action");
    if (
      action &&
      action !== "#" &&
      action !== "" &&
      !action.startsWith("javascript:")
    ) {
      assertions.push({
        type: "redirect",
        expected: action,
        description: `Form should redirect to ${action}`,
      });
      break;
    }
  }

  // 4. HTTP response assertions from captured AJAX/XHR requests
  if (capturedResponses && capturedResponses.length > 0) {
    for (const response of capturedResponses) {
      assertions.push({
        type: "response-ok",
        selector: response.url,
        expected: String(response.status),
        description: `${response.method} ${response.url} should return ${response.status}`,
      });
    }
  }

  return assertions;
}

Dependencies (Outgoing)

graph LR detectAssertions["detectAssertions"] E2EAssertion["E2EAssertion"] CapturedAction["CapturedAction"] CapturedHttpResponse["CapturedHttpResponse"] detectAssertions -->|uses| E2EAssertion detectAssertions -->|uses| CapturedAction detectAssertions -->|uses| CapturedHttpResponse style detectAssertions fill:#dbeafe,stroke:#2563eb,stroke-width:2px click detectAssertions "a17e691084ab4134.html" click E2EAssertion "2559fefe794f4820.html" click CapturedAction "1b1d5e067ceacd9c.html" click CapturedHttpResponse "24761f29aa5746ab.html"

Impact (Incoming)

graph LR detectAssertions["detectAssertions"] FillableElement["FillableElement"] FillableElement -->|uses| detectAssertions style detectAssertions fill:#dbeafe,stroke:#2563eb,stroke-width:2px click detectAssertions "a17e691084ab4134.html" click FillableElement "2ecf5aaac3f668a8.html"
SourceType
FillableElement uses