Skip to content

Commit f050033

Browse files
hi-ogawaclaude
andauthored
fix(browser): don't fail trace when locator has no match for non-playwright providers (#11975)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 0c41888 commit f050033

5 files changed

Lines changed: 97 additions & 4 deletions

File tree

‎packages/browser/src/client/tester/expect-element.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { getBrowserState, getWorkerState, now } from '../utils'
77
import { resolveActionTimeout } from './action'
88
import { ariaMatchers } from './aria'
99
import { matchers } from './expect'
10+
import { Locator as InternalLocator } from './locators'
1011
import { createBrowserTraceRangeId, recordBrowserTraceEntry } from './trace'
1112

1213
const kLocator = Symbol.for('$$vitest:locator')
@@ -71,9 +72,7 @@ function element<T extends HTMLElement | SVGElement | null | Locator>(
7172
const sourceError = new Error('__vitest_mark_trace__')
7273
const traceRangeId = hasActiveTraceView ? createBrowserTraceRangeId() : undefined
7374
const getSelector = () =>
74-
!elementOrLocator || elementOrLocator instanceof Element
75-
? undefined
76-
: elementOrLocator.serialize()
75+
elementOrLocator instanceof InternalLocator ? elementOrLocator._toTraceElement() : undefined
7776
const getTraceName = (assertion: Assertion) => {
7877
const isNot = chai.util.flag(assertion, 'negate')
7978
const name = chai.util.flag(assertion, '_name') || '<unknown>'

‎packages/browser/src/client/tester/locators.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,21 @@ getBrowserState().selectorEngine = selectorEngine
8787

8888
const kLocator = Symbol.for('$$vitest:locator')
8989

90+
/**
91+
* Runtime base class behind the public `Locator` type from `vitest/browser`, which providers extend.
92+
*/
9093
export abstract class Locator {
9194
public abstract selector: string
9295

9396
private _parsedSelector: ParsedSelector | undefined
9497
protected _container?: Element | undefined
98+
/**
99+
* Playwright-style selector, e.g. `internal:role=button`. `preview` and `webdriverio` set it,
100+
* because their `selector` resolves matched elements to CSS and throws when nothing matches.
101+
* `playwright` leaves it unset, because its `selector` is already this string.
102+
*/
95103
protected _pwSelector?: string | undefined
104+
/** Cache of `asLocator()`, e.g. `getByRole('button')`. */
96105
protected _pwLocator?: string | undefined
97106
protected _errorSource?: Error
98107

@@ -225,7 +234,7 @@ export abstract class Locator {
225234
await recordBrowserTraceEntry(currentTest, {
226235
name,
227236
kind: options?.kind ?? 'mark',
228-
element: this.serialize(),
237+
element: this._toTraceElement(),
229238
stack: options?.stack ?? error?.stack,
230239
})
231240
}
@@ -372,6 +381,17 @@ export abstract class Locator {
372381
}
373382
}
374383

384+
/**
385+
* @internal
386+
*/
387+
public _toTraceElement(): { selector: string; locator: string } {
388+
// unlike serialize(), never resolves elements on any provider
389+
return {
390+
selector: this._pwSelector || this.selector,
391+
locator: this.asLocator(),
392+
}
393+
}
394+
375395
public asLocator(): string {
376396
return (
377397
this._pwLocator ||

‎packages/browser/src/client/tester/tester-utils.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ export class CommandsManager {
178178
}
179179
let status: BrowserTraceEntryStatus = 'pass'
180180
const traceRangeId = hasActiveTraceView ? createBrowserTraceRangeId() : undefined
181+
// commands that target an element pass its `SerializedLocator` first
181182
const element =
182183
typeof args[0] === 'object' &&
183184
args[0] !== null &&

‎packages/utils/src/trace.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface BrowserTraceEntry {
2929
element?: {
3030
selector: string
3131
locator: string
32+
_pwSelector?: string
3233
}
3334
snapshot?: BrowserTraceSnapshot
3435
}

‎test/browser/specs/trace.test.ts‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,3 +1724,75 @@ test('action settling after its attempt ended is not recorded', async () => {
17241724
}
17251725
`)
17261726
})
1727+
1728+
test('locator without a match is traced without reading its selector', async () => {
1729+
const { stderr, errorTree, results } = await runInlineBrowserTests(
1730+
{
1731+
'basic.test.ts': /* ts */ `
1732+
import { expect, test } from 'vitest'
1733+
import { page } from 'vitest/browser'
1734+
1735+
test('absent', async () => {
1736+
const locator = page.getByRole('button')
1737+
// mimic providers whose selector resolves elements and throws when nothing matches
1738+
Object.defineProperties(locator, {
1739+
_pwSelector: { value: locator.selector },
1740+
selector: { get() { throw new Error('selector should not be read') } },
1741+
})
1742+
// the only matcher that passes when nothing matches
1743+
await expect.element(locator).not.toBeInTheDocument()
1744+
await locator.mark('absent mark')
1745+
})
1746+
`,
1747+
},
1748+
{
1749+
browser: {
1750+
instances: instances.slice(0, 1),
1751+
traceView: 'on',
1752+
},
1753+
},
1754+
)
1755+
expect(stderr).toBe('')
1756+
expect(errorTree()).toMatchInlineSnapshot(`
1757+
{
1758+
"basic.test.ts": {
1759+
"absent": "passed",
1760+
},
1761+
}
1762+
`)
1763+
const entries = results
1764+
.flatMap((testModule) => [...testModule.children.allTests()])
1765+
.flatMap((testCase) => testCase.artifacts())
1766+
.flatMap((artifact) =>
1767+
artifact.type === 'internal:browserTrace' ? (artifact.data as BrowserTraceData).entries : [],
1768+
)
1769+
.map((entry) => ({
1770+
name: entry.name,
1771+
locator: entry.element?.locator,
1772+
selectorResolution: entry.snapshot?.selectorResolution,
1773+
}))
1774+
expect(entries).toMatchInlineSnapshot(`
1775+
[
1776+
{
1777+
"locator": "getByRole('button')",
1778+
"name": "not.toBeInTheDocument",
1779+
"selectorResolution": "missing",
1780+
},
1781+
{
1782+
"locator": "getByRole('button')",
1783+
"name": "not.toBeInTheDocument",
1784+
"selectorResolution": "missing",
1785+
},
1786+
{
1787+
"locator": "getByRole('button')",
1788+
"name": "absent mark",
1789+
"selectorResolution": "missing",
1790+
},
1791+
{
1792+
"locator": undefined,
1793+
"name": "vitest:onAfterRetryTask",
1794+
"selectorResolution": undefined,
1795+
},
1796+
]
1797+
`)
1798+
})

0 commit comments

Comments
 (0)