From ee9a7dad12b02cfddd48b0a0cdd6c09cb9f5fd88 Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Mon, 11 Dec 2023 17:35:29 -0800 Subject: [PATCH] docs: emphasize that `has` is a relative locator (#28588) References #28556. --- docs/src/api/params.md | 4 +- docs/src/locators.md | 57 +++++++++++++++++++++-- packages/playwright-core/types/types.d.ts | 45 ++++++++++++++---- 3 files changed, 92 insertions(+), 14 deletions(-) diff --git a/docs/src/api/params.md b/docs/src/api/params.md index 441ad4e544..c2c9e33c0d 100644 --- a/docs/src/api/params.md +++ b/docs/src/api/params.md @@ -1023,9 +1023,11 @@ For example, `"Playwright"` matches `
Playwright
`. ## locator-option-has - `has` <[Locator]> -Matches elements containing an element that matches an inner locator. Inner locator is queried against the outer one. +Narrows down the results of the method to those which contain elements matching this relative locator. For example, `article` that has `text=Playwright` matches `
Playwright
`. +Inner locator **must be relative** to the outer locator and is queried starting with the outer locator match, not the document root. For example, you can find `content` that has `div` in `
Playwright
`. However, looking for `content` that has `article div` will fail, because the inner locator must be relative and should not use any elements outside the `content`. + Note that outer and inner locators must belong to the same frame. Inner locator must not contain [FrameLocator]s. ## locator-option-has-not diff --git a/docs/src/locators.md b/docs/src/locators.md index ee036dbf79..720e5618ca 100644 --- a/docs/src/locators.md +++ b/docs/src/locators.md @@ -973,19 +973,21 @@ await page .ClickAsync(); ``` -We can also assert the product card to make sure there is only one +We can also assert the product card to make sure there is only one: ```js await expect(page .getByRole('listitem') - .filter({ has: page.getByText('Product 2') })) + .filter({ has: page.getByRole('heading', { name: 'Product 2' }) })) .toHaveCount(1); ``` ```java assertThat(page .getByRole(AriaRole.LISTITEM) - .filter(new Locator.FilterOptions().setHas(page.getByText("Product 2"))) + .filter(new Locator.FilterOptions() + .setHas(page.GetByRole(AriaRole.HEADING, + new Page.GetByRoleOptions().setName("Product 2")))) .hasCount(1); ``` @@ -1014,6 +1016,55 @@ await Expect(Page .ToHaveCountAsync(1); ``` +The filtering locator **must be relative** to the original locator and is queried starting with the original locator match, not the document root. Therefore, the following will not work, because the filtering locator starts matching from the `