Skip to content

Commit 5a2cfdb

Browse files
authored
api: add isChecked method (#4953)
This adds `{Page,Frame}.isChecked(selector)` and `ElementHandle.isChecked()` methods. Useful to do assertions in tests: ```js await page.click('text="Add TODO"'); expect(await page.isChecked('.item-done')).toBe(false); ```
1 parent 3b617b3 commit 5a2cfdb

16 files changed

+162
-0
lines changed

docs/src/api/class-elementhandle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,11 @@ Returns the `element.innerHTML`.
327327

328328
Returns the `element.innerText`.
329329

330+
## async method: ElementHandle.isChecked
331+
- returns: <[boolean]>
332+
333+
Returns whether the element is checked. Throws if the element is not a checkbox or radio input.
334+
330335
## async method: ElementHandle.isDisabled
331336
- returns: <[boolean]>
332337

docs/src/api/class-frame.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,15 @@ Returns `element.innerText`.
536536

537537
### option: Frame.innerText.timeout = %%-input-timeout-%%
538538

539+
## async method: Frame.isChecked
540+
- returns: <[boolean]>
541+
542+
Returns whether the element is checked. Throws if the element is not a checkbox or radio input.
543+
544+
### param: Frame.isChecked.selector = %%-input-selector-%%
545+
546+
### option: Frame.isChecked.timeout = %%-input-timeout-%%
547+
539548
## method: Frame.isDetached
540549
- returns: <[boolean]>
541550

docs/src/api/class-page.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,15 @@ Returns `element.innerText`.
10021002

10031003
### option: Page.innerText.timeout = %%-input-timeout-%%
10041004

1005+
## async method: Page.isChecked
1006+
- returns: <[boolean]>
1007+
1008+
Returns whether the element is checked. Throws if the element is not a checkbox or radio input.
1009+
1010+
### param: Page.isChecked.selector = %%-input-selector-%%
1011+
1012+
### option: Page.isChecked.timeout = %%-input-timeout-%%
1013+
10051014
## method: Page.isClosed
10061015
- returns: <[boolean]>
10071016

src/client/elementHandle.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ export class ElementHandle<T extends Node = Node> extends JSHandle<T> implements
8787
});
8888
}
8989

90+
async isChecked(): Promise<boolean> {
91+
return this._wrapApiCall('elementHandle.isChecked', async () => {
92+
return (await this._elementChannel.isChecked()).value;
93+
});
94+
}
95+
9096
async isDisabled(): Promise<boolean> {
9197
return this._wrapApiCall('elementHandle.isDisabled', async () => {
9298
return (await this._elementChannel.isDisabled()).value;

src/client/frame.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,12 @@ export class Frame extends ChannelOwner<channels.FrameChannel, channels.FrameIni
362362
});
363363
}
364364

365+
async isChecked(selector: string, options: channels.FrameIsCheckedOptions = {}): Promise<boolean> {
366+
return this._wrapApiCall(this._apiName('isChecked'), async () => {
367+
return (await this._channel.isChecked({ selector, ...options })).value;
368+
});
369+
}
370+
365371
async isDisabled(selector: string, options: channels.FrameIsDisabledOptions = {}): Promise<boolean> {
366372
return this._wrapApiCall(this._apiName('isDisabled'), async () => {
367373
return (await this._channel.isDisabled({ selector, ...options })).value;

src/client/page.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,10 @@ export class Page extends ChannelOwner<channels.PageChannel, channels.PageInitia
530530
return this._attributeToPage(() => this._mainFrame.getAttribute(selector, name, options));
531531
}
532532

533+
async isChecked(selector: string, options?: channels.FrameIsCheckedOptions): Promise<boolean> {
534+
return this._attributeToPage(() => this._mainFrame.isChecked(selector, options));
535+
}
536+
533537
async isDisabled(selector: string, options?: channels.FrameIsDisabledOptions): Promise<boolean> {
534538
return this._attributeToPage(() => this._mainFrame.isDisabled(selector, options));
535539
}

src/dispatchers/elementHandlerDispatcher.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ export class ElementHandleDispatcher extends JSHandleDispatcher implements chann
6666
return { value: await this._elementHandle.innerHTML() };
6767
}
6868

69+
async isChecked(): Promise<channels.ElementHandleIsCheckedResult> {
70+
return { value: await this._elementHandle.isChecked() };
71+
}
72+
6973
async isDisabled(): Promise<channels.ElementHandleIsDisabledResult> {
7074
return { value: await this._elementHandle.isDisabled() };
7175
}

src/dispatchers/frameDispatcher.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ export class FrameDispatcher extends Dispatcher<Frame, channels.FrameInitializer
159159
return { value: value === null ? undefined : value };
160160
}
161161

162+
async isChecked(params: channels.FrameIsCheckedParams): Promise<channels.FrameIsCheckedResult> {
163+
return { value: await this._frame.isChecked(params.selector, params) };
164+
}
165+
162166
async isDisabled(params: channels.FrameIsDisabledParams): Promise<channels.FrameIsDisabledResult> {
163167
return { value: await this._frame.isDisabled(params.selector, params) };
164168
}

src/protocol/channels.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,7 @@ export interface FrameChannel extends Channel {
11901190
hover(params: FrameHoverParams, metadata?: Metadata): Promise<FrameHoverResult>;
11911191
innerHTML(params: FrameInnerHTMLParams, metadata?: Metadata): Promise<FrameInnerHTMLResult>;
11921192
innerText(params: FrameInnerTextParams, metadata?: Metadata): Promise<FrameInnerTextResult>;
1193+
isChecked(params: FrameIsCheckedParams, metadata?: Metadata): Promise<FrameIsCheckedResult>;
11931194
isDisabled(params: FrameIsDisabledParams, metadata?: Metadata): Promise<FrameIsDisabledResult>;
11941195
isEnabled(params: FrameIsEnabledParams, metadata?: Metadata): Promise<FrameIsEnabledResult>;
11951196
isHidden(params: FrameIsHiddenParams, metadata?: Metadata): Promise<FrameIsHiddenResult>;
@@ -1446,6 +1447,16 @@ export type FrameInnerTextOptions = {
14461447
export type FrameInnerTextResult = {
14471448
value: string,
14481449
};
1450+
export type FrameIsCheckedParams = {
1451+
selector: string,
1452+
timeout?: number,
1453+
};
1454+
export type FrameIsCheckedOptions = {
1455+
timeout?: number,
1456+
};
1457+
export type FrameIsCheckedResult = {
1458+
value: boolean,
1459+
};
14491460
export type FrameIsDisabledParams = {
14501461
selector: string,
14511462
timeout?: number,
@@ -1783,6 +1794,7 @@ export interface ElementHandleChannel extends JSHandleChannel {
17831794
hover(params: ElementHandleHoverParams, metadata?: Metadata): Promise<ElementHandleHoverResult>;
17841795
innerHTML(params?: ElementHandleInnerHTMLParams, metadata?: Metadata): Promise<ElementHandleInnerHTMLResult>;
17851796
innerText(params?: ElementHandleInnerTextParams, metadata?: Metadata): Promise<ElementHandleInnerTextResult>;
1797+
isChecked(params?: ElementHandleIsCheckedParams, metadata?: Metadata): Promise<ElementHandleIsCheckedResult>;
17861798
isDisabled(params?: ElementHandleIsDisabledParams, metadata?: Metadata): Promise<ElementHandleIsDisabledResult>;
17871799
isEditable(params?: ElementHandleIsEditableParams, metadata?: Metadata): Promise<ElementHandleIsEditableResult>;
17881800
isEnabled(params?: ElementHandleIsEnabledParams, metadata?: Metadata): Promise<ElementHandleIsEnabledResult>;
@@ -1942,6 +1954,11 @@ export type ElementHandleInnerTextOptions = {};
19421954
export type ElementHandleInnerTextResult = {
19431955
value: string,
19441956
};
1957+
export type ElementHandleIsCheckedParams = {};
1958+
export type ElementHandleIsCheckedOptions = {};
1959+
export type ElementHandleIsCheckedResult = {
1960+
value: boolean,
1961+
};
19451962
export type ElementHandleIsDisabledParams = {};
19461963
export type ElementHandleIsDisabledOptions = {};
19471964
export type ElementHandleIsDisabledResult = {

src/protocol/protocol.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,13 @@ Frame:
12181218
returns:
12191219
value: string
12201220

1221+
isChecked:
1222+
parameters:
1223+
selector: string
1224+
timeout: number?
1225+
returns:
1226+
value: boolean
1227+
12211228
isDisabled:
12221229
parameters:
12231230
selector: string
@@ -1637,6 +1644,10 @@ ElementHandle:
16371644
returns:
16381645
value: string
16391646

1647+
isChecked:
1648+
returns:
1649+
value: boolean
1650+
16401651
isDisabled:
16411652
returns:
16421653
value: boolean

0 commit comments

Comments
 (0)