docs: clarify the waitForXPath return type (#9541) (#9583)

This commit is contained in:
Rafael-Martins 2023-02-01 12:23:32 -03:00 committed by GitHub
parent 1dd7bbe04f
commit f12f27e1eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 24 deletions

View File

@ -64,7 +64,7 @@ class ElementHandle {
Promise<[ElementHandle](./puppeteer.elementhandle.md)<Node> \| null> Promise<[ElementHandle](./puppeteer.elementhandle.md)<Node> \| null>
Promise which resolves when element specified by xpath string is added to DOM. Resolves to `null` if waiting for `hidden: true` and xpath is not found in DOM. Promise which resolves when element specified by xpath string is added to DOM. Resolves to `null` if waiting for `hidden: true` and xpath is not found in DOM, otherwise resolves to `ElementHandle`.
## Remarks ## Remarks

View File

@ -34,27 +34,23 @@ import puppeteer from 'puppeteer';
class Page { class Page {
waitForXPath( waitForXPath(
xpath: string, xpath: string,
options?: { options?: WaitForSelectorOptions
visible?: boolean;
hidden?: boolean;
timeout?: number;
}
): Promise<ElementHandle<Node> | null>; ): Promise<ElementHandle<Node> | null>;
} }
``` ```
## Parameters ## Parameters
| Parameter | Type | Description | | Parameter | Type | Description |
| --------- | ---------------------------------------------------------- | --------------------------------------------------------------------------------------- | | --------- | --------------------------------------------------------------- | --------------------------------------------------------------------------------------- |
| xpath | string | A [xpath](https://developer.mozilla.org/en-US/docs/Web/XPath) of an element to wait for | | xpath | string | A [xpath](https://developer.mozilla.org/en-US/docs/Web/XPath) of an element to wait for |
| options | { visible?: boolean; hidden?: boolean; timeout?: number; } | <i>(Optional)</i> Optional waiting parameters | | options | [WaitForSelectorOptions](./puppeteer.waitforselectoroptions.md) | <i>(Optional)</i> Optional waiting parameters |
**Returns:** **Returns:**
Promise&lt;[ElementHandle](./puppeteer.elementhandle.md)&lt;Node&gt; \| null&gt; Promise&lt;[ElementHandle](./puppeteer.elementhandle.md)&lt;Node&gt; \| null&gt;
Promise which resolves when element specified by xpath string is added to DOM. Resolves to `null` if waiting for `hidden: true` and xpath is not found in DOM. Promise which resolves when element specified by xpath string is added to DOM. Resolves to `null` if waiting for `hidden: true` and xpath is not found in DOM, otherwise resolves to `ElementHandle`.
## Remarks ## Remarks

View File

@ -2465,7 +2465,7 @@ export class Page extends EventEmitter {
* @param options - Optional waiting parameters * @param options - Optional waiting parameters
* @returns Promise which resolves when element specified by xpath string is * @returns Promise which resolves when element specified by xpath string is
* added to DOM. Resolves to `null` if waiting for `hidden: true` and xpath is * added to DOM. Resolves to `null` if waiting for `hidden: true` and xpath is
* not found in DOM. * not found in DOM, otherwise resolves to `ElementHandle`.
* @remarks * @remarks
* The optional Argument `options` have properties: * The optional Argument `options` have properties:
* *
@ -2483,11 +2483,7 @@ export class Page extends EventEmitter {
*/ */
waitForXPath( waitForXPath(
xpath: string, xpath: string,
options?: { options?: WaitForSelectorOptions
visible?: boolean;
hidden?: boolean;
timeout?: number;
}
): Promise<ElementHandle<Node> | null>; ): Promise<ElementHandle<Node> | null>;
waitForXPath(): Promise<ElementHandle<Node> | null> { waitForXPath(): Promise<ElementHandle<Node> | null> {
throw new Error('Not implemented'); throw new Error('Not implemented');

View File

@ -381,7 +381,7 @@ export class ElementHandle<
* @param options - Optional waiting parameters * @param options - Optional waiting parameters
* @returns Promise which resolves when element specified by xpath string is * @returns Promise which resolves when element specified by xpath string is
* added to DOM. Resolves to `null` if waiting for `hidden: true` and xpath is * added to DOM. Resolves to `null` if waiting for `hidden: true` and xpath is
* not found in DOM. * not found in DOM, otherwise resolves to `ElementHandle`.
* @remarks * @remarks
* The optional Argument `options` have properties: * The optional Argument `options` have properties:
* *

View File

@ -1631,11 +1631,7 @@ export class CDPPage extends Page {
override waitForXPath( override waitForXPath(
xpath: string, xpath: string,
options: { options: WaitForSelectorOptions = {}
visible?: boolean;
hidden?: boolean;
timeout?: number;
} = {}
): Promise<ElementHandle<Node> | null> { ): Promise<ElementHandle<Node> | null> {
return this.mainFrame().waitForXPath(xpath, options); return this.mainFrame().waitForXPath(xpath, options);
} }

View File

@ -15,7 +15,7 @@
*/ */
import expect from 'expect'; import expect from 'expect';
import {TimeoutError} from 'puppeteer'; import {TimeoutError, ElementHandle} from 'puppeteer';
import {isErrorLike} from 'puppeteer-core/internal/util/ErrorLike.js'; import {isErrorLike} from 'puppeteer-core/internal/util/ErrorLike.js';
import { import {
createTimeout, createTimeout,
@ -787,6 +787,22 @@ describe('waittask specs', function () {
expect(await waitForXPath).toBe(true); expect(await waitForXPath).toBe(true);
expect(divHidden).toBe(true); expect(divHidden).toBe(true);
}); });
it('hidden should return null if the element is not found', async () => {
const {page} = getTestState();
const waitForXPath = await page.waitForXPath('//div', {hidden: true});
expect(waitForXPath).toBe(null);
});
it('hidden should return an empty element handle if the element is found', async () => {
const {page} = getTestState();
await page.setContent(`<div style='display: none;'>text</div>`);
const waitForXPath = await page.waitForXPath('//div', {hidden: true});
expect(waitForXPath).toBeInstanceOf(ElementHandle);
});
it('should return the element handle', async () => { it('should return the element handle', async () => {
const {page} = getTestState(); const {page} = getTestState();