-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(browser): keep querying elements even if locator is created with …
…elementLocator, add pubic @vitest/browser/utils (#6296)
- Loading branch information
1 parent
73abf30
commit 30dc579
Showing
19 changed files
with
566 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { type Locator, type LocatorSelectors, page } from '@vitest/browser/context' | ||
import { type StringifyOptions, stringify } from 'vitest/utils' | ||
import { asLocator } from 'ivya' | ||
|
||
export function getElementLocatorSelectors(element: Element): LocatorSelectors { | ||
const locator = page.elementLocator(element) | ||
return { | ||
getByAltText: (altText, options) => locator.getByAltText(altText, options), | ||
getByLabelText: (labelText, options) => locator.getByLabelText(labelText, options), | ||
getByPlaceholder: (placeholderText, options) => locator.getByPlaceholder(placeholderText, options), | ||
getByRole: (role, options) => locator.getByRole(role, options), | ||
getByTestId: testId => locator.getByTestId(testId), | ||
getByText: (text, options) => locator.getByText(text, options), | ||
getByTitle: (title, options) => locator.getByTitle(title, options), | ||
} | ||
} | ||
|
||
type PrettyDOMOptions = Omit<StringifyOptions, 'maxLength'> | ||
|
||
export function debug( | ||
el?: Element | Locator | null | (Element | Locator)[], | ||
maxLength?: number, | ||
options?: PrettyDOMOptions, | ||
): void { | ||
if (Array.isArray(el)) { | ||
// eslint-disable-next-line no-console | ||
el.forEach(e => console.log(prettyDOM(e, maxLength, options))) | ||
} | ||
else { | ||
// eslint-disable-next-line no-console | ||
console.log(prettyDOM(el, maxLength, options)) | ||
} | ||
} | ||
|
||
export function prettyDOM( | ||
dom?: Element | Locator | undefined | null, | ||
maxLength: number = Number(import.meta.env.DEBUG_PRINT_LIMIT ?? 7000), | ||
prettyFormatOptions: PrettyDOMOptions = {}, | ||
): string { | ||
if (maxLength === 0) { | ||
return '' | ||
} | ||
|
||
if (!dom) { | ||
dom = document.body | ||
} | ||
|
||
if ('element' in dom && 'all' in dom) { | ||
dom = dom.element() | ||
} | ||
|
||
const type = typeof dom | ||
if (type !== 'object' || !dom.outerHTML) { | ||
const typeName = type === 'object' ? dom.constructor.name : type | ||
throw new TypeError(`Expecting a valid DOM element, but got ${typeName}.`) | ||
} | ||
|
||
const pretty = stringify(dom, Number.POSITIVE_INFINITY, { | ||
maxLength, | ||
highlight: true, | ||
...prettyFormatOptions, | ||
}) | ||
return dom.outerHTML.length > maxLength | ||
? `${pretty.slice(0, maxLength)}...` | ||
: pretty | ||
} | ||
|
||
export function getElementError(selector: string, container: Element): Error { | ||
const error = new Error(`Cannot find element with locator: ${asLocator('javascript', selector)}\n\n${prettyDOM(container)}`) | ||
error.name = 'VitestBrowserElementError' | ||
return error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// should be in sync with tester/public-utils.ts | ||
// we cannot bundle it because vitest depend on the @vitest/browser and vise versa | ||
// fortunately, the file is quite small | ||
|
||
import { LocatorSelectors } from '@vitest/browser/context' | ||
import { StringifyOptions } from 'vitest/utils' | ||
|
||
type PrettyDOMOptions = Omit<StringifyOptions, 'maxLength'> | ||
|
||
export declare function getElementLocatorSelectors(element: Element): LocatorSelectors | ||
export declare function debug( | ||
el?: Element | Locator | null | (Element | Locator)[], | ||
maxLength?: number, | ||
options?: PrettyDOMOptions, | ||
): void | ||
export declare function prettyDOM( | ||
dom?: Element | Locator | undefined | null, | ||
maxLength?: number, | ||
prettyFormatOptions?: PrettyDOMOptions, | ||
): string | ||
export declare function getElementError(selector: string, container?: Element): Error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.