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)
| Source | Type |
|---|---|
| matches | uses |
| fill | calls |
| matches | uses |
| fill | calls |
| selectOption | calls |
| selectMultipleOptions | calls |
| matches | uses |
| fill | calls |
| makeSelect | uses |