Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
docs: document Python's expect_event methods (#4963)
Pavel Feldman4dbbb4758334
diff --git a/docs/src/api/class-frame.md b/docs/src/api/class-frame.mdindex c4bb7ba16f..ebaeaaded8 100644--- a/docs/src/api/class-frame.md+++ b/docs/src/api/class-frame.md@@ -856,15 +856,7 @@ await frame.click('button'); // Click triggers navigation.await frame.waitForLoadState(); // Waits for 'load' state by default.```-### param: Frame.waitForLoadState.state-- `state` <"load"|"domcontentloaded"|"networkidle">--Optional load state to wait for, defaults to `load`. If the state has been already reached while loading current document, the-method returns immediately. Can be one of:- * `'load'` - wait for the `load` event to be fired.- * `'domcontentloaded'` - wait for the `DOMContentLoaded` event to be fired.- * `'networkidle'` - wait until there are no network connections for at least `500` ms.-+### param: Frame.waitForLoadState.state = %%-wait-for-load-state-state-%%### option: Frame.waitForLoadState.timeout = %%-navigation-timeout-%%## async method: Frame.waitForNavigationdiff --git a/docs/src/api/class-page.md b/docs/src/api/class-page.mdindex 0f34e466ad..0fab1e0bfc 100644--- a/docs/src/api/class-page.md+++ b/docs/src/api/class-page.md@@ -1608,10 +1608,7 @@ Returns the event data value.Waits for event to fire and passes its value into the predicate function. Returns when the predicate returns truthyvalue. Will throw an error if the page is closed before the event is fired.-### param: Page.waitForEvent.event-- `event` <[string]>--Event name, same one would pass into `page.on(event)`.+### param: Page.waitForEvent.event = %%-wait-for-event-event-%%### param: Page.waitForEvent.optionsOrPredicate* langs: js@@ -1693,15 +1690,7 @@ console.log(await popup.title()); // Popup is ready to use.Shortcut for main frame's [`method: Frame.waitForLoadState`].-### param: Page.waitForLoadState.state-- `state` <"load"|"domcontentloaded"|"networkidle">--Optional load state to wait for, defaults to `load`. If the state has been already reached while loading current document, the-method resolves immediately. Can be one of:- * `'load'` - wait for the `load` event to be fired.- * `'domcontentloaded'` - wait for the `DOMContentLoaded` event to be fired.- * `'networkidle'` - wait until there are no network connections for at least `500` ms.-+### param: Page.waitForLoadState.state = %%-wait-for-load-state-state-%%### option: Page.waitForLoadState.timeout = %%-navigation-timeout-%%## async method: Page.waitForNavigation@@ -1729,10 +1718,7 @@ Shortcut for main frame's [`method: Frame.waitForNavigation`].### option: Page.waitForNavigation.timeout = %%-navigation-timeout-%%-### option: Page.waitForNavigation.url-- `url` <[string]|[RegExp]|[function]\([URL]\):[boolean]>--A glob pattern, regex pattern or predicate receiving [URL] to match while waiting for the navigation.+### option: Page.waitForNavigation.url = %%-wait-for-navigation-url-%%### option: Page.waitForNavigation.waitUntil = %%-navigation-wait-until-%%diff --git a/docs/src/api/params.md b/docs/src/api/params.mdindex 5b34f076c6..3b04a61e0f 100644--- a/docs/src/api/params.md+++ b/docs/src/api/params.md@@ -322,6 +322,25 @@ Options to select. If the `<select>` has the `multiple` attribute, all matchingfirst option matching one of the passed options is selected. String values are equivalent to `{value:'string'}`. Optionis considered matching if all specified properties match.+## wait-for-navigation-url+- `url` <[string]|[RegExp]|[function]\([URL]\):[boolean]>++A glob pattern, regex pattern or predicate receiving [URL] to match while waiting for the navigation.++## wait-for-event-event+- `event` <[string]>++Event name, same one typically passed into `page.on(event)`.++## wait-for-load-state-state+- `state` <"load"|"domcontentloaded"|"networkidle">++Optional load state to wait for, defaults to `load`. If the state has been already reached while loading current document, the+method resolves immediately. Can be one of:+ * `'load'` - wait for the `load` event to be fired.+ * `'domcontentloaded'` - wait for the `DOMContentLoaded` event to be fired.+ * `'networkidle'` - wait until there are no network connections for at least `500` ms.+## python-select-options-element* langs: python- `element` <[ElementHandle]|[Array]<[ElementHandle]>>diff --git a/docs/src/api/python.md b/docs/src/api/python.mdindex f5f0ba951c..c64fdc5cfc 100644--- a/docs/src/api/python.md+++ b/docs/src/api/python.md@@ -116,6 +116,323 @@ page.on('requestfailed', lambda request: print(request.url + ' ' + request.failuWaits for this response to finish, returns failure error if request failed.+## async method: Page.expectEvent+* langs: python+- returns: <[EventContextManager]>++Performs action and waits for given `event` to fire. If predicate is provided, it passes+event's value into the `predicate` function and waits for `predicate(event)` to return a truthy value.+Will throw an error if the page is closed before the `event` is fired.++```python-async+async with page.expect_event(event_name) as event_info:+ await page.click("button")+value = await event_info.value+```++```python-sync+with page.expect_event(event_name) as event_info:+ page.click("button")+value = event_info.value+```++### param: Page.expectEvent.event = %%-wait-for-event-event-%%+### option: Page.expectEvent.predicate = %%-python-wait-for-event-predicate-%%+### option: Page.expectEvent.timeout = %%-python-wait-for-event-timeout-%%++## async method: BrowserContext.expectEvent+* langs: python+- returns: <[EventContextManager]>++Performs action and waits for given `event` to fire. If predicate is provided, it passes+event's value into the `predicate` function and waits for `predicate(event)` to return a truthy value.+Will throw an error if browser context is closed before the `event` is fired.++```python-async+async with context.expect_event(event_name) as event_info:+ await context.click("button")+value = await event_info.value+```++```python-sync+with context.expect_event(event_name) as event_info:+ context.click("button")+value = event_info.value+```++### param: BrowserContext.expectEvent.event = %%-wait-for-event-event-%%+### option: BrowserContext.expectEvent.predicate = %%-python-wait-for-event-predicate-%%+### option: BrowserContext.expectEvent.timeout = %%-python-wait-for-event-timeout-%%++## async method: WebSocket.expectEvent+* langs: python+- returns: <[EventContextManager]>++Performs action and waits for given `event` to fire. If predicate is provided, it passes+event's value into the `predicate` function and waits for `predicate(event)` to return a truthy value.+Will throw an error if the socket is closed before the `event` is fired.++```python-async+async with ws.expect_event(event_name) as event_info:+ await ws.click("button")+value = await event_info.value+```++```python-sync+with ws.expect_event(event_name) as event_info:+ ws.click("button")+value = event_info.value+```++### param: WebSocket.expectEvent.event = %%-wait-for-event-event-%%+### option: WebSocket.expectEvent.predicate = %%-python-wait-for-event-predicate-%%+### option: WebSocket.expectEvent.timeout = %%-python-wait-for-event-timeout-%%++## async method: Page.expectLoadState+* langs: python+- returns: <[EventContextManager]>++Performs action and waits for the required load state. It resolves when the page reaches a required load state, `load` by default. The navigation must have been committed when this method is called. If current document has+already reached the required state, resolves immediately.++```python-async+async with page.expect_load_state():+ await page.click('button') # Click triggers navigation.+# Context manager waits for 'load' event.+```++```python-sync+with page.expect_load_state():+ page.click('button') # Click triggers navigation.+# Context manager waits for 'load' event.+```++Shortcut for main frame's [`method: Frame.expectLoadState`].++### param: Page.expectLoadState.state = %%-wait-for-load-state-state-%%+### option: Page.expectLoadState.timeout = %%-navigation-timeout-%%++## async method: Frame.expectLoadState+* langs: python+- returns: <[EventContextManager]>++Performs action and waits for the required load state. It resolves when the page reaches a required load state, `load` by default. The navigation must have been committed when this method is called. If current document has+already reached the required state, resolves immediately.++```python-async+async with frame.expect_load_state():+ await frame.click('button') # Click triggers navigation.+# Context manager waits for 'load' event.+```++```python-sync+with frame.expect_load_state():+ frame.click('button') # Click triggers navigation.+# Context manager waits for 'load' event.+```++### param: Frame.expectLoadState.state = %%-wait-for-load-state-state-%%+### option: Frame.expectLoadState.timeout = %%-navigation-timeout-%%++## async method: Page.expectNavigation+* langs: python+- returns: <[EventContextManager]>++Performs action and wait for the next navigation. In case of multiple redirects, the navigation will resolve with+the response of the last redirect. In case of navigation to a different anchor or navigation due to History API+usage, the navigation will resolve with `null`.++This resolves when the page navigates to a new URL or reloads. It is useful for when you run code which will+indirectly cause the page to navigate. e.g. The click target has an `onclick` handler that triggers navigation+from a `setTimeout`. Consider this example:++```python-async+async with page.expect_navigation():+ await page.click("a.delayed-navigation") # Clicking the link will indirectly cause a navigation+# Context manager waited for the navigation to happen.+```++```python-sync+with page.expect_navigation():+ page.click("a.delayed-navigation") # Clicking the link will indirectly cause a navigation+# Context manager waited for the navigation to happen.+```++**NOTE** Usage of the [History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API) to change the+URL is considered a navigation.++Shortcut for main frame's [`method: Frame.expectNavigation`].++### option: Page.expectNavigation.timeout = %%-navigation-timeout-%%+### option: Page.expectNavigation.url = %%-wait-for-navigation-url-%%+### option: Page.expectNavigation.waitUntil = %%-navigation-wait-until-%%++## async method: Frame.expectNavigation+* langs: python+- returns: <[EventContextManager]>++Performs action and wait for the next navigation. In case of multiple redirects, the navigation will resolve with+the response of the last redirect. In case of navigation to a different anchor or navigation due to History API+usage, the navigation will resolve with `null`.++This resolves when the page navigates to a new URL or reloads. It is useful for when you run code which will+indirectly cause the page to navigate. e.g. The click target has an `onclick` handler that triggers navigation+from a `setTimeout`. Consider this example:++```python-async+async with frame.expect_navigation():+ await frame.click("a.delayed-navigation") # Clicking the link will indirectly cause a navigation+# Context manager waited for the navigation to happen.+```++```python-sync+with frame.expect_navigation():+ frame.click("a.delayed-navigation") # Clicking the link will indirectly cause a navigation+# Context manager waited for the navigation to happen.+```++**NOTE** Usage of the [History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API) to change the+URL is considered a navigation.++### option: Frame.expectNavigation.timeout = %%-navigation-timeout-%%+### option: Frame.expectNavigation.url = %%-wait-for-navigation-url-%%+### option: Frame.expectNavigation.waitUntil = %%-navigation-wait-until-%%+++## async method: Page.expectDownload+* langs: python+- returns: <[EventContextManager]<[Download]>>++Performs action and waits for `download` event to fire. If predicate is provided, it passes+[Download] value into the `predicate` function and waits for `predicate(event)` to return a truthy value.+Will throw an error if the page is closed before the download event is fired.++### option: Page.expectDownload.predicate =+* langs: python+- `predicate` <[function]\([Download]\):[bool]>++Receives the [Download] object and resolves to truthy value when the waiting should resolve.++### option: Page.expectDownload.timeout = %%-python-wait-for-event-timeout-%%+++## async method: Page.expectPopup+* langs: python+- returns: <[EventContextManager]<[Page]>>++Performs action and waits for `popup` event to fire. If predicate is provided, it passes+[Popup] value into the `predicate` function and waits for `predicate(event)` to return a truthy value.+Will throw an error if the page is closed before the popup event is fired.++### option: Page.expectPopup.predicate =+* langs: python+- `predicate` <[function]\([Page]\):[bool]>++Receives the [Popup] object and resolves to truthy value when the waiting should resolve.++### option: Page.expectPopup.timeout = %%-python-wait-for-event-timeout-%%+++## async method: Page.expectWorker+* langs: python+- returns: <[EventContextManager]<[Worker]>>++Performs action and waits for `worker` event to fire. If predicate is provided, it passes+[Worker] value into the `predicate` function and waits for `predicate(event)` to return a truthy value.+Will throw an error if the page is closed before the worker event is fired.++### option: Page.expectWorker.predicate =+* langs: python+- `predicate` <[function]\([Worker]\):[bool]>++Receives the [Worker] object and resolves to truthy value when the waiting should resolve.++### option: Page.expectWorker.timeout = %%-python-wait-for-event-timeout-%%+++## async method: Page.expectConsoleMessage+* langs: python+- returns: <[EventContextManager]<[ConsoleMessage]>>++Performs action and waits for `console` event to fire. If predicate is provided, it passes+[ConsoleMessage] value into the `predicate` function and waits for `predicate(event)` to return a truthy value.+Will throw an error if the page is closed before the worker event is fired.++### option: Page.expectConsoleMessage.predicate =+* langs: python+- `predicate` <[function]\([ConsoleMessage]\):[bool]>++Receives the [ConsoleMessage] object and resolves to truthy value when the waiting should resolve.++### option: Page.expectConsoleMessage.timeout = %%-python-wait-for-event-timeout-%%+++## async method: Page.expectFileChooser+* langs: python+- returns: <[EventContextManager]<[FileChooser]>>++Performs action and waits for `filechooser` event to fire. If predicate is provided, it passes+[FileChooser] value into the `predicate` function and waits for `predicate(event)` to return a truthy value.+Will throw an error if the page is closed before the worker event is fired.++### option: Page.expectFileChooser.predicate =+* langs: python+- `predicate` <[function]\([FileChooser]\):[bool]>++Receives the [FileChooser] object and resolves to truthy value when the waiting should resolve.++### option: Page.expectFileChooser.timeout = %%-python-wait-for-event-timeout-%%+++## async method: BrowserContext.expectPage+* langs: python+- returns: <[EventContextManager]<[Page]>>++Performs action and waits for `page` event to fire. If predicate is provided, it passes+[Page] value into the `predicate` function and waits for `predicate(event)` to return a truthy value.+Will throw an error if the page is closed before the worker event is fired.++### option: BrowserContext.expectPage.predicate =+* langs: python+- `predicate` <[function]\([Page]\):[bool]>++Receives the [Page] object and resolves to truthy value when the waiting should resolve.++### option: BrowserContext.expectPage.timeout = %%-python-wait-for-event-timeout-%%+++## async method: Page.expectRequest+* langs: python+- returns: <[EventContextManager]<[Request]>>++Performs action and waits for `response` event to fire. If predicate is provided, it passes+[Request] value into the `predicate` function and waits for `predicate(event)` to return a truthy value.+Will throw an error if the page is closed before the download event is fired.++### param: Page.expectRequest.url_or_predicate =+* langs: python+- `url_or_predicate` <[str]|[RegExp]|[function]\([Request]\):[bool]>++Receives the [Request] object and resolves to truthy value when the waiting should resolve.++### option: Page.expectRequest.timeout = %%-python-wait-for-event-timeout-%%++## async method: Page.expectResponse+* langs: python+- returns: <[EventContextManager]<[Response]>>++Performs action and waits for `response` event to fire. If predicate is provided, it passes+[Response] value into the `predicate` function and waits for `predicate(event)` to return a truthy value.+Will throw an error if the page is closed before the download event is fired.++### param: Page.expectResponse.url_or_predicate =+* langs: python+- `url_or_predicate` <[str]|[RegExp]|[function]\([Response]\):[bool]>++Receives the [Response] object and resolves to truthy value when the waiting should resolve.++### option: Page.expectResponse.timeout = %%-python-wait-for-event-timeout-%%+### option: BrowserContext.waitForEvent.predicate = %%-python-wait-for-event-predicate-%%### option: BrowserContext.waitForEvent.timeout = %%-python-wait-for-event-timeout-%%diff --git a/types/types.d.ts b/types/types.d.tsindex 47bc4a5dfb..69931eb7ce 100644--- a/types/types.d.ts+++ b/types/types.d.ts@@ -4499,7 +4499,7 @@ export interface Frame {* await frame.waitForLoadState(); // Waits for 'load' state by default.* ```*- * @param state Optional load state to wait for, defaults to `load`. If the state has been already reached while loading current document, the method returns immediately. Can be one of:+ * @param state Optional load state to wait for, defaults to `load`. If the state has been already reached while loading current document, the method resolves immediately. Can be one of:* - `'load'` - wait for the `load` event to be fired.* - `'domcontentloaded'` - wait for the `DOMContentLoaded` event to be fired.* - `'networkidle'` - wait until there are no network connections for at least `500` ms.diff --git a/utils/doclint/api_parser.js b/utils/doclint/api_parser.jsindex 8f7da38a49..1d8317ecf1 100644--- a/utils/doclint/api_parser.js+++ b/utils/doclint/api_parser.js@@ -77,6 +77,8 @@ class ApiParser {*/parseMember(spec) {const match = spec.text.match(/(event|method|property|async method): ([^.]+)\.(.*)/);+ if (!match)+ throw new Error('Invalid member: ' + spec.text);const name = match[3];let returnType = null;for (const item of spec.children || []) {@@ -106,7 +108,11 @@ class ApiParser {parseArgument(spec) {const match = spec.text.match(/(param|option): ([^.]+)\.([^.]+)\.(.*)/);const clazz = this.classes.get(match[2]);+ if (!clazz)+ throw new Error('Invalid class ' + match[2]);const method = clazz.membersArray.find(m => m.kind === 'method' && m.name === match[3]);+ if (!method)+ throw new Error('Invalid method ' + match[2] + '.' + match[3]);if (match[1] === 'param') {method.argsArray.push(this.parseProperty(spec));} else {