Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore: discourage waitForSelector (#22133)

Pavel Feldmanee39af260d14
diff --git a/docs/src/api/class-frame.md b/docs/src/api/class-frame.md
index ac7bb4147e..994e92628f 100644
--- a/docs/src/api/class-frame.md
+++ b/docs/src/api/class-frame.md
@@ -2104,6 +2104,8 @@ a navigation.
## async method: Frame.waitForSelector
* since: v1.8
+* discouraged: Use web assertions that assert visibility or a locator-based [`method: Locator.waitFor`] instead.
+ Read more about [locators](../locators.md).
- returns: <[null]|[ElementHandle]>
Returns when element specified by selector satisfies [`option: state`] option. Returns `null` if waiting for `hidden` or
diff --git a/docs/src/api/class-page.md b/docs/src/api/class-page.md
index 4892f530cf..b952ac6e36 100644
--- a/docs/src/api/class-page.md
+++ b/docs/src/api/class-page.md
@@ -4654,6 +4654,8 @@ changed by using the [`method: BrowserContext.setDefaultTimeout`] or [`method: P
## async method: Page.waitForSelector
* since: v1.8
+* discouraged: Use web assertions that assert visibility or a locator-based [`method: Locator.waitFor`] instead.
+ Read more about [locators](../locators.md).
- returns: <[null]|[ElementHandle]>
Returns when element specified by selector satisfies [`option: state`] option. Returns `null` if waiting for `hidden` or
@@ -4814,6 +4816,8 @@ Codition to wait for.
## async method: Page.waitForTimeout
* since: v1.8
+* discouraged: Never wait for timeout in production. Tests that wait for time are
+ inherently flaky. Use [Locator] actions and web assertions that wait automatically.
Waits for the given [`param: timeout`] in milliseconds.
diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.ts
index 99337e9769..00fe441089 100644
--- a/packages/playwright-core/types/types.d.ts
+++ b/packages/playwright-core/types/types.d.ts
@@ -611,6 +611,10 @@ export interface Page {
waitForFunction<R>(pageFunction: PageFunction<void, R>, arg?: any, options?: PageWaitForFunctionOptions): Promise<SmartHandle<R>>;
/**
+ * **NOTE** Use web assertions that assert visibility or a locator-based
+ * [locator.waitFor([options])](https://playwright.dev/docs/api/class-locator#locator-wait-for) instead. Read more
+ * about [locators](https://playwright.dev/docs/locators).
+ *
* Returns when element specified by selector satisfies `state` option. Returns `null` if waiting for `hidden` or
* `detached`.
*
@@ -645,6 +649,10 @@ export interface Page {
*/
waitForSelector<K extends keyof HTMLElementTagNameMap>(selector: K, options?: PageWaitForSelectorOptionsNotHidden): Promise<ElementHandleForTag<K>>;
/**
+ * **NOTE** Use web assertions that assert visibility or a locator-based
+ * [locator.waitFor([options])](https://playwright.dev/docs/api/class-locator#locator-wait-for) instead. Read more
+ * about [locators](https://playwright.dev/docs/locators).
+ *
* Returns when element specified by selector satisfies `state` option. Returns `null` if waiting for `hidden` or
* `detached`.
*
@@ -679,6 +687,10 @@ export interface Page {
*/
waitForSelector(selector: string, options?: PageWaitForSelectorOptionsNotHidden): Promise<ElementHandle<SVGElement | HTMLElement>>;
/**
+ * **NOTE** Use web assertions that assert visibility or a locator-based
+ * [locator.waitFor([options])](https://playwright.dev/docs/api/class-locator#locator-wait-for) instead. Read more
+ * about [locators](https://playwright.dev/docs/locators).
+ *
* Returns when element specified by selector satisfies `state` option. Returns `null` if waiting for `hidden` or
* `detached`.
*
@@ -713,6 +725,10 @@ export interface Page {
*/
waitForSelector<K extends keyof HTMLElementTagNameMap>(selector: K, options: PageWaitForSelectorOptions): Promise<ElementHandleForTag<K> | null>;
/**
+ * **NOTE** Use web assertions that assert visibility or a locator-based
+ * [locator.waitFor([options])](https://playwright.dev/docs/api/class-locator#locator-wait-for) instead. Read more
+ * about [locators](https://playwright.dev/docs/locators).
+ *
* Returns when element specified by selector satisfies `state` option. Returns `null` if waiting for `hidden` or
* `detached`.
*
@@ -4558,6 +4574,9 @@ export interface Page {
}): Promise<Response>;
/**
+ * **NOTE** Never wait for timeout in production. Tests that wait for time are inherently flaky. Use [Locator] actions and web
+ * assertions that wait automatically.
+ *
* Waits for the given `timeout` in milliseconds.
*
* Note that `page.waitForTimeout()` should only be used for debugging. Tests using the timer in production are going
@@ -5202,6 +5221,10 @@ export interface Frame {
waitForFunction<R>(pageFunction: PageFunction<void, R>, arg?: any, options?: PageWaitForFunctionOptions): Promise<SmartHandle<R>>;
/**
+ * **NOTE** Use web assertions that assert visibility or a locator-based
+ * [locator.waitFor([options])](https://playwright.dev/docs/api/class-locator#locator-wait-for) instead. Read more
+ * about [locators](https://playwright.dev/docs/locators).
+ *
* Returns when element specified by selector satisfies `state` option. Returns `null` if waiting for `hidden` or
* `detached`.
*
@@ -5236,6 +5259,10 @@ export interface Frame {
*/
waitForSelector<K extends keyof HTMLElementTagNameMap>(selector: K, options?: PageWaitForSelectorOptionsNotHidden): Promise<ElementHandleForTag<K>>;
/**
+ * **NOTE** Use web assertions that assert visibility or a locator-based
+ * [locator.waitFor([options])](https://playwright.dev/docs/api/class-locator#locator-wait-for) instead. Read more
+ * about [locators](https://playwright.dev/docs/locators).
+ *
* Returns when element specified by selector satisfies `state` option. Returns `null` if waiting for `hidden` or
* `detached`.
*
@@ -5270,6 +5297,10 @@ export interface Frame {
*/
waitForSelector(selector: string, options?: PageWaitForSelectorOptionsNotHidden): Promise<ElementHandle<SVGElement | HTMLElement>>;
/**
+ * **NOTE** Use web assertions that assert visibility or a locator-based
+ * [locator.waitFor([options])](https://playwright.dev/docs/api/class-locator#locator-wait-for) instead. Read more
+ * about [locators](https://playwright.dev/docs/locators).
+ *
* Returns when element specified by selector satisfies `state` option. Returns `null` if waiting for `hidden` or
* `detached`.
*
@@ -5304,6 +5335,10 @@ export interface Frame {
*/
waitForSelector<K extends keyof HTMLElementTagNameMap>(selector: K, options: PageWaitForSelectorOptions): Promise<ElementHandleForTag<K> | null>;
/**
+ * **NOTE** Use web assertions that assert visibility or a locator-based
+ * [locator.waitFor([options])](https://playwright.dev/docs/api/class-locator#locator-wait-for) instead. Read more
+ * about [locators](https://playwright.dev/docs/locators).
+ *
* Returns when element specified by selector satisfies `state` option. Returns `null` if waiting for `hidden` or
* `detached`.
*