From a6f62f126d4a3ffaccd2484bcb57ec23144230d6 Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Wed, 10 Feb 2021 11:04:56 -0800 Subject: [PATCH] fix(isVisible): do not wait for the selector to be resolved (#5393) (#5398) --- docs/src/api/class-frame.md | 4 ++-- docs/src/api/class-page.md | 4 ++-- src/server/frames.ts | 5 ++--- test/elementhandle-convenience.spec.ts | 5 +++++ types/types.d.ts | 12 ++++++++---- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/docs/src/api/class-frame.md b/docs/src/api/class-frame.md index 5485606f86..41fd9f09bb 100644 --- a/docs/src/api/class-frame.md +++ b/docs/src/api/class-frame.md @@ -761,7 +761,7 @@ Returns whether the element is [enabled](./actionability.md#enabled). ## async method: Frame.isHidden - returns: <[boolean]> -Returns whether the element is hidden, the opposite of [visible](./actionability.md#visible). +Returns whether the element is hidden, the opposite of [visible](./actionability.md#visible). [`option: selector`] that does not match any elements is considered hidden. ### param: Frame.isHidden.selector = %%-input-selector-%% @@ -770,7 +770,7 @@ Returns whether the element is hidden, the opposite of [visible](./actionability ## async method: Frame.isVisible - returns: <[boolean]> -Returns whether the element is [visible](./actionability.md#visible). +Returns whether the element is [visible](./actionability.md#visible). [`option: selector`] that does not match any elements is considered not visible. ### param: Frame.isVisible.selector = %%-input-selector-%% diff --git a/docs/src/api/class-page.md b/docs/src/api/class-page.md index d2c4bc234e..93ef28320c 100644 --- a/docs/src/api/class-page.md +++ b/docs/src/api/class-page.md @@ -1477,7 +1477,7 @@ Returns whether the element is [enabled](./actionability.md#enabled). ## async method: Page.isHidden - returns: <[boolean]> -Returns whether the element is hidden, the opposite of [visible](./actionability.md#visible). +Returns whether the element is hidden, the opposite of [visible](./actionability.md#visible). [`option: selector`] that does not match any elements is considered hidden. ### param: Page.isHidden.selector = %%-input-selector-%% @@ -1486,7 +1486,7 @@ Returns whether the element is hidden, the opposite of [visible](./actionability ## async method: Page.isVisible - returns: <[boolean]> -Returns whether the element is [visible](./actionability.md#visible). +Returns whether the element is [visible](./actionability.md#visible). [`option: selector`] that does not match any elements is considered not visible. ### param: Page.isVisible.selector = %%-input-selector-%% diff --git a/src/server/frames.ts b/src/server/frames.ts index b522ade64b..d975061a24 100644 --- a/src/server/frames.ts +++ b/src/server/frames.ts @@ -928,11 +928,10 @@ export class Frame extends EventEmitter { } async isVisible(selector: string, options: types.TimeoutOptions = {}): Promise { - const info = this._page.selectors._parseSelector(selector); - const task = dom.visibleTask(info); return runAbortableTask(async progress => { progress.log(` checking visibility of "${selector}"`); - return this._scheduleRerunnableTask(progress, info.world, task); + const element = await this.$(selector); + return element ? await element.isVisible() : false; }, this._page._timeoutSettings.timeout(options)); } diff --git a/test/elementhandle-convenience.spec.ts b/test/elementhandle-convenience.spec.ts index 39d2954d0f..3f7cea8135 100644 --- a/test/elementhandle-convenience.spec.ts +++ b/test/elementhandle-convenience.spec.ts @@ -159,16 +159,21 @@ it('getAttribute should be atomic', async ({ playwright, page }) => { it('isVisible and isHidden should work', async ({ page }) => { await page.setContent(`
Hi
`); + const div = await page.$('div'); expect(await div.isVisible()).toBe(true); expect(await div.isHidden()).toBe(false); expect(await page.isVisible('div')).toBe(true); expect(await page.isHidden('div')).toBe(false); + const span = await page.$('span'); expect(await span.isVisible()).toBe(false); expect(await span.isHidden()).toBe(true); expect(await page.isVisible('span')).toBe(false); expect(await page.isHidden('span')).toBe(true); + + expect(await page.isVisible('no-such-element')).toBe(false); + expect(await page.isHidden('no-such-element')).toBe(true); }); it('isEnabled and isDisabled should work', async ({ page }) => { diff --git a/types/types.d.ts b/types/types.d.ts index 2f2b8ac754..b6221ad28d 100644 --- a/types/types.d.ts +++ b/types/types.d.ts @@ -2028,7 +2028,8 @@ export interface Page { }): Promise; /** - * Returns whether the element is hidden, the opposite of [visible](https://github.com/microsoft/playwright/blob/master/docs/actionability.md#visible). + * Returns whether the element is hidden, the opposite of [visible](https://github.com/microsoft/playwright/blob/master/docs/actionability.md#visible). `selector` that does not + * match any elements is considered hidden. * @param selector A selector to search for element. If there are multiple elements satisfying the selector, the first will be used. See [working with selectors](https://github.com/microsoft/playwright/blob/master/docs/selectors.md#working-with-selectors) for more details. * @param options */ @@ -2044,7 +2045,8 @@ export interface Page { }): Promise; /** - * Returns whether the element is [visible](https://github.com/microsoft/playwright/blob/master/docs/actionability.md#visible). + * Returns whether the element is [visible](https://github.com/microsoft/playwright/blob/master/docs/actionability.md#visible). `selector` that does not match any elements is + * considered not visible. * @param selector A selector to search for element. If there are multiple elements satisfying the selector, the first will be used. See [working with selectors](https://github.com/microsoft/playwright/blob/master/docs/selectors.md#working-with-selectors) for more details. * @param options */ @@ -3992,7 +3994,8 @@ export interface Frame { }): Promise; /** - * Returns whether the element is hidden, the opposite of [visible](https://github.com/microsoft/playwright/blob/master/docs/actionability.md#visible). + * Returns whether the element is hidden, the opposite of [visible](https://github.com/microsoft/playwright/blob/master/docs/actionability.md#visible). `selector` that does not + * match any elements is considered hidden. * @param selector A selector to search for element. If there are multiple elements satisfying the selector, the first will be used. See [working with selectors](https://github.com/microsoft/playwright/blob/master/docs/selectors.md#working-with-selectors) for more details. * @param options */ @@ -4008,7 +4011,8 @@ export interface Frame { }): Promise; /** - * Returns whether the element is [visible](https://github.com/microsoft/playwright/blob/master/docs/actionability.md#visible). + * Returns whether the element is [visible](https://github.com/microsoft/playwright/blob/master/docs/actionability.md#visible). `selector` that does not match any elements is + * considered not visible. * @param selector A selector to search for element. If there are multiple elements satisfying the selector, the first will be used. See [working with selectors](https://github.com/microsoft/playwright/blob/master/docs/selectors.md#working-with-selectors) for more details. * @param options */