Skip to content

Commit 2cc34e0

Browse files
authored
feat(browser): expose utils.configurePrettyDOM (#9103)
1 parent 22e381e commit 2cc34e0

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

docs/api/browser/context.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,43 @@ export const server: {
191191
config: SerializedConfig
192192
}
193193
```
194+
195+
## `utils`
196+
197+
Utility functions useful for custom render libraries.
198+
199+
```ts
200+
export const utils: {
201+
/**
202+
* This is simillar to calling `page.elementLocator`, but it returns only
203+
* locator selectors.
204+
*/
205+
getElementLocatorSelectors(element: Element): LocatorSelectors
206+
/**
207+
* Prints prettified HTML of an element.
208+
*/
209+
debug(
210+
el?: Element | Locator | null | (Element | Locator)[],
211+
maxLength?: number,
212+
options?: PrettyDOMOptions,
213+
): void
214+
/**
215+
* Returns prettified HTML of an element.
216+
*/
217+
prettyDOM(
218+
dom?: Element | Locator | undefined | null,
219+
maxLength?: number,
220+
prettyFormatOptions?: PrettyDOMOptions,
221+
): string
222+
/**
223+
* Configures default options of `prettyDOM` and `debug` functions.
224+
* This will also affect `vitest-browser-{framework}` package.
225+
* @experimental
226+
*/
227+
configurePrettyDOM(options: StringifyOptions): void
228+
/**
229+
* Creates "Cannot find element" error. Useful for custom locators.
230+
*/
231+
getElementError(selector: string, container?: Element): Error
232+
}
233+
```

packages/browser/context.d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,17 +753,36 @@ export interface BrowserLocators {
753753
export type PrettyDOMOptions = Omit<StringifyOptions, 'maxLength'>
754754

755755
export const utils: {
756+
/**
757+
* This is simillar to calling `page.elementLocator`, but it returns only
758+
* locator selectors.
759+
*/
756760
getElementLocatorSelectors(element: Element): LocatorSelectors
761+
/**
762+
* Prints prettified HTML of an element.
763+
*/
757764
debug(
758765
el?: Element | Locator | null | (Element | Locator)[],
759766
maxLength?: number,
760767
options?: PrettyDOMOptions,
761768
): void
769+
/**
770+
* Returns prettified HTML of an element.
771+
*/
762772
prettyDOM(
763773
dom?: Element | Locator | undefined | null,
764774
maxLength?: number,
765775
prettyFormatOptions?: PrettyDOMOptions,
766776
): string
777+
/**
778+
* Configures default options of `prettyDOM` and `debug` functions.
779+
* This will also affect `vitest-browser-{framework}` package.
780+
* @experimental
781+
*/
782+
configurePrettyDOM(options: StringifyOptions): void
783+
/**
784+
* Creates "Cannot find element" error. Useful for custom locators.
785+
*/
767786
getElementError(selector: string, container?: Element): Error
768787
}
769788

packages/browser/src/client/tester/context.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,8 @@ function getElementLocatorSelectors(element: Element): LocatorSelectors {
406406

407407
type PrettyDOMOptions = Omit<StringifyOptions, 'maxLength'>
408408

409+
let defaultOptions: StringifyOptions | undefined
410+
409411
function debug(
410412
el?: Element | Locator | null | (Element | Locator)[],
411413
maxLength?: number,
@@ -423,7 +425,7 @@ function debug(
423425

424426
function prettyDOM(
425427
dom?: Element | Locator | undefined | null,
426-
maxLength: number = Number(import.meta.env.DEBUG_PRINT_LIMIT ?? 7000),
428+
maxLength: number = Number(defaultOptions?.maxLength ?? import.meta.env.DEBUG_PRINT_LIMIT ?? 7000),
427429
prettyFormatOptions: PrettyDOMOptions = {},
428430
): string {
429431
if (maxLength === 0) {
@@ -447,6 +449,7 @@ function prettyDOM(
447449
const pretty = stringify(dom, Number.POSITIVE_INFINITY, {
448450
maxLength,
449451
highlight: true,
452+
...defaultOptions,
450453
...prettyFormatOptions,
451454
})
452455
return dom.outerHTML.length > maxLength
@@ -460,9 +463,17 @@ function getElementError(selector: string, container: Element): Error {
460463
return error
461464
}
462465

466+
/**
467+
* @experimental
468+
*/
469+
function configurePrettyDOM(options: StringifyOptions) {
470+
defaultOptions = options
471+
}
472+
463473
export const utils = {
464474
getElementError,
465475
prettyDOM,
466476
debug,
467477
getElementLocatorSelectors,
478+
configurePrettyDOM,
468479
}

test/browser/test/utils.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,19 @@ test('should be able to opt out of shadow DOM content', async () => {
9393

9494
expect(await commands.stripVTControlCharacters(utils.prettyDOM(undefined, undefined, { printShadowRoot: false }))).toMatchSnapshot()
9595
})
96+
97+
test('changing the defaults works', async () => {
98+
utils.configurePrettyDOM({
99+
maxDepth: 1,
100+
})
101+
102+
const div = document.createElement('div')
103+
div.innerHTML = '<div><div><div><div></div></div></div></div>'
104+
document.body.append(div)
105+
106+
expect(await commands.stripVTControlCharacters(utils.prettyDOM(div))).toMatchInlineSnapshot(`
107+
"<div>
108+
<div … />
109+
</div>"
110+
`)
111+
})

0 commit comments

Comments
 (0)