From 7d2cc06355a94579af540c35fb3c0c0005bc747a Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Mon, 9 Jan 2023 13:06:25 -0800 Subject: [PATCH] docs: add usage sections to Locator methods (#19968) --- docs/src/api/class-locator.md | 804 +++++++++++++++++-- docs/src/api/params.md | 248 +++++- docs/src/locators.md | 4 +- packages/playwright-core/types/types.d.ts | 902 ++++++++++++++++++---- 4 files changed, 1729 insertions(+), 229 deletions(-) diff --git a/docs/src/api/class-locator.md b/docs/src/api/class-locator.md index 9dfb391f60..c1fbdb2f6a 100644 --- a/docs/src/api/class-locator.md +++ b/docs/src/api/class-locator.md @@ -46,12 +46,56 @@ foreach (var li in await page.GetByRole('listitem').AllAsync()) Returns an array of `node.innerText` values for all matching nodes. +**Usage** + +```js +const texts = await page.getByRole('link').allInnerTexts(); +``` + +```python async +texts = await page.get_by_role("link").all_inner_texts() +``` + +```python sync +texts = page.get_by_role("link").all_inner_texts() +``` + +```java +String[] texts = page.getByRole(AriaRole.LINK).allInnerTexts(); +``` + +```csharp +var texts = await page.GetByRole(AriaRole.Link).AllInnerTextsAsync(); +``` + ## async method: Locator.allTextContents * since: v1.14 - returns: <[Array]<[string]>> Returns an array of `node.textContent` values for all matching nodes. +**Usage** + +```js +const texts = await page.getByRole('link').allTextContents(); +``` + +```python async +texts = await page.get_by_role("link").all_text_contents() +``` + +```python sync +texts = page.get_by_role("link").all_text_contents() +``` + +```java +String[] texts = page.getByRole(AriaRole.LINK).allTextContents(); +``` + +```csharp +var texts = await page.GetByRole(AriaRole.Link).AllTextContentsAsync(); +``` + ## async method: Locator.blur * since: v1.28 @@ -68,9 +112,11 @@ Calls [blur](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/blur) - `width` <[float]> the width of the element in pixels. - `height` <[float]> the height of the element in pixels. -This method returns the bounding box of the element, or `null` if the element is not visible. The bounding box is +This method returns the bounding box of the element matching the locator, or `null` if the element is not visible. The bounding box is calculated relative to the main frame viewport - which is usually the same as the browser window. +**Details** + Scrolling affects the returned bounding box, similarly to [Element.getBoundingClientRect](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect). That means `x` and/or `y` may be negative. @@ -84,27 +130,27 @@ snippet should click the center of the element. **Usage** ```js -const box = await element.boundingBox(); +const box = await page.getByRole('button').boundingBox(); await page.mouse.click(box.x + box.width / 2, box.y + box.height / 2); ``` ```java -BoundingBox box = element.boundingBox(); +BoundingBox box = page.getByRole(AriaRole.BUTTON).boundingBox(); page.mouse().click(box.x + box.width / 2, box.y + box.height / 2); ``` ```python async -box = await element.bounding_box() +box = await page.get_by_role("button").bounding_box() await page.mouse.click(box["x"] + box["width"] / 2, box["y"] + box["height"] / 2) ``` ```python sync -box = element.bounding_box() +box = page.get_by_role("button").bounding_box() page.mouse.click(box["x"] + box["width"] / 2, box["y"] + box["height"] / 2) ``` ```csharp -var box = await element.BoundingBoxAsync(); +var box = await page.GetByRole(AriaRole.Button).BoundingBoxAsync(); await page.Mouse.ClickAsync(box.X + box.Width / 2, box.Y + box.Height / 2); ``` @@ -114,7 +160,11 @@ await page.Mouse.ClickAsync(box.X + box.Width / 2, box.Y + box.Height / 2); ## async method: Locator.check * since: v1.14 -This method checks the element by performing the following steps: +Ensure that checkbox or radio element is checked. + +**Details** + +Performs the following steps: 1. Ensure that element is a checkbox or a radio input. If not, this method throws. If the element is already checked, this method returns immediately. 1. Wait for [actionability](../actionability.md) checks on the element, unless [`option: force`] option is set. @@ -128,6 +178,28 @@ If the element is detached from the DOM at any moment during the action, this me When all steps combined have not finished during the specified [`option: timeout`], this method throws a [TimeoutError]. Passing zero timeout disables this. +**Usage** + +```js +await page.getByRole('checkbox').check(); +``` + +```java +page.getByRole(AriaRole.CHECKBOX).check(); +``` + +```python async +await page.get_by_role("checkbox").check() +``` + +```python sync +page.get_by_role("checkbox").check() +``` + +```csharp +await page.GetByRole(AriaRole.Checkbox).CheckAsync(); +``` + ### option: Locator.check.position = %%-input-position-%% * since: v1.14 @@ -146,10 +218,38 @@ When all steps combined have not finished during the specified [`option: timeout ## async method: Locator.clear * since: v1.28 +Clear the input field. + +**Details** + This method waits for [actionability](../actionability.md) checks, focuses the element, clears it and triggers an `input` event after clearing. If the target element is not an ``, `