Skip to content

Commit 161ec82

Browse files
committed
improve error message when waitForElementByCss times out
1 parent 315afdd commit 161ec82

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

Diff for: test/lib/browsers/playwright.ts

+29-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
ElementHandle,
1111
devices,
1212
Locator,
13+
errors as PlaywrightErrors,
1314
} from 'playwright'
1415
import path from 'path'
1516

@@ -427,16 +428,36 @@ export class Playwright extends BrowserInterface {
427428
)
428429
}
429430

430-
waitForElementByCss(selector, timeout = 10_000) {
431-
return this.chain(() => {
432-
return page
431+
waitForElementByCss(selector: string, timeout = 10_000) {
432+
return this.chain(async () => {
433+
const el = await page
433434
.waitForSelector(selector, { timeout, state: 'attached' })
434-
.then(async (el) => {
435-
// it seems selenium waits longer and tests rely on this behavior
436-
// so we wait for the load event fire before returning
437-
await page.waitForLoadState()
438-
return this.wrapElement(el, selector)
435+
.catch((err) => {
436+
if (err instanceof PlaywrightErrors.TimeoutError) {
437+
// The default error message from playwright doesn't tell us which selector we were waiting for.
438+
throw new Error(
439+
`Playwright.waitForElementByCss: Timeout ${timeout}ms exceeded while waiting for '${selector}'`
440+
)
441+
} else {
442+
throw err
443+
}
439444
})
445+
446+
// it seems selenium waits longer and tests rely on this behavior
447+
// so we wait for the load event fire before returning
448+
const targetLoadState: Parameters<Page['waitForLoadState']>[0] = 'load'
449+
await page.waitForLoadState(targetLoadState, { timeout }).catch((err) => {
450+
if (err instanceof PlaywrightErrors.TimeoutError) {
451+
// The default error message from playwright doesn't tell us which selector we were waiting for.
452+
throw new Error(
453+
`Playwright.waitForElementByCss: Timeout ${timeout}ms exceeded while waiting for the '${targetLoadState}' event after finding '${selector}'`
454+
)
455+
} else {
456+
throw err
457+
}
458+
})
459+
460+
return this.wrapElement(el, selector)
440461
})
441462
}
442463

0 commit comments

Comments
 (0)