waitForElement function infrastructure exported ✓ 100.0%

Last updated: 2026-03-04T23:21:38.390Z

Metrics

LOC: 29 Complexity: 3 Params: 3 Coverage: 100.0% (2/2 lines, 0x executed)

Signature

waitForElement( selector: string, timeoutMs = 500, ): : Promise<HTMLElement | null>

Summary

Waits for an element matching the selector to appear in the DOM. Returns null if the element doesn't appear within the timeout.

Source Code

export function waitForElement(
  selector: string,
  timeoutMs = 500,
): Promise<HTMLElement | null> {
  const existing = document.querySelector<HTMLElement>(selector);
  if (existing) return Promise.resolve(existing);

  return new Promise((resolve) => {
    const observer = new MutationObserver(() => {
      const el = document.querySelector<HTMLElement>(selector);
      if (el) {
        observer.disconnect();
        resolve(el);
      }
    });

    observer.observe(document.body, {
      childList: true,
      subtree: true,
      attributes: true,
      attributeFilter: ["class", "style"],
    });

    setTimeout(() => {
      observer.disconnect();
      resolve(null);
    }, timeoutMs);
  });
}

No outgoing dependencies.

Impact (Incoming)

graph LR waitForElement["waitForElement"] matches["matches"] fill["fill"] selectOption["selectOption"] selectMultipleOptions["selectMultipleOptions"] makeSelect["makeSelect"] matches -->|uses| waitForElement fill -->|calls| waitForElement matches -->|uses| waitForElement fill -->|calls| waitForElement selectOption -->|calls| waitForElement selectMultipleOptions -->|calls| waitForElement matches -->|uses| waitForElement fill -->|calls| waitForElement makeSelect -->|uses| waitForElement style waitForElement fill:#dbeafe,stroke:#2563eb,stroke-width:2px click waitForElement "230bbf0882ca7c81.html" click matches "73613b6536c4a9f0.html" click fill "7a85157abe4e6b0a.html" click selectOption "fe5ab67a02962060.html" click selectMultipleOptions "e53f83dda6e2588d.html" click makeSelect "75e6fe1e0954bfa2.html"
SourceType
matches uses
fill calls
matches uses
fill calls
selectOption calls
selectMultipleOptions calls
matches uses
fill calls
makeSelect uses