|
| 1 | +import { type Locator, type LocatorSelectors, page } from '@vitest/browser/context' |
| 2 | +import { type StringifyOptions, stringify } from 'vitest/utils' |
| 3 | +import { asLocator } from 'ivya' |
| 4 | + |
| 5 | +export function getElementLocatorSelectors(element: Element): LocatorSelectors { |
| 6 | + const locator = page.elementLocator(element) |
| 7 | + return { |
| 8 | + getByAltText: (altText, options) => locator.getByAltText(altText, options), |
| 9 | + getByLabelText: (labelText, options) => locator.getByLabelText(labelText, options), |
| 10 | + getByPlaceholder: (placeholderText, options) => locator.getByPlaceholder(placeholderText, options), |
| 11 | + getByRole: (role, options) => locator.getByRole(role, options), |
| 12 | + getByTestId: testId => locator.getByTestId(testId), |
| 13 | + getByText: (text, options) => locator.getByText(text, options), |
| 14 | + getByTitle: (title, options) => locator.getByTitle(title, options), |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +type PrettyDOMOptions = Omit<StringifyOptions, 'maxLength'> |
| 19 | + |
| 20 | +export function debug( |
| 21 | + el?: Element | Locator | null | (Element | Locator)[], |
| 22 | + maxLength?: number, |
| 23 | + options?: PrettyDOMOptions, |
| 24 | +): void { |
| 25 | + if (Array.isArray(el)) { |
| 26 | + // eslint-disable-next-line no-console |
| 27 | + el.forEach(e => console.log(prettyDOM(e, maxLength, options))) |
| 28 | + } |
| 29 | + else { |
| 30 | + // eslint-disable-next-line no-console |
| 31 | + console.log(prettyDOM(el, maxLength, options)) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +export function prettyDOM( |
| 36 | + dom?: Element | Locator | undefined | null, |
| 37 | + maxLength: number = Number(import.meta.env.DEBUG_PRINT_LIMIT ?? 7000), |
| 38 | + prettyFormatOptions: PrettyDOMOptions = {}, |
| 39 | +): string { |
| 40 | + if (maxLength === 0) { |
| 41 | + return '' |
| 42 | + } |
| 43 | + |
| 44 | + if (!dom) { |
| 45 | + dom = document.body |
| 46 | + } |
| 47 | + |
| 48 | + if ('element' in dom && 'all' in dom) { |
| 49 | + dom = dom.element() |
| 50 | + } |
| 51 | + |
| 52 | + const type = typeof dom |
| 53 | + if (type !== 'object' || !dom.outerHTML) { |
| 54 | + const typeName = type === 'object' ? dom.constructor.name : type |
| 55 | + throw new TypeError(`Expecting a valid DOM element, but got ${typeName}.`) |
| 56 | + } |
| 57 | + |
| 58 | + const pretty = stringify(dom, Number.POSITIVE_INFINITY, { |
| 59 | + maxLength, |
| 60 | + highlight: true, |
| 61 | + ...prettyFormatOptions, |
| 62 | + }) |
| 63 | + return dom.outerHTML.length > maxLength |
| 64 | + ? `${pretty.slice(0, maxLength)}...` |
| 65 | + : pretty |
| 66 | +} |
| 67 | + |
| 68 | +export function getElementError(selector: string, container: Element): Error { |
| 69 | + const error = new Error(`Cannot find element with locator: ${asLocator('javascript', selector)}\n\n${prettyDOM(container)}`) |
| 70 | + error.name = 'VitestBrowserElementError' |
| 71 | + return error |
| 72 | +} |
0 commit comments