From e3e2da31869215e7f0ef6e640261ee66a6a4d36b Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Tue, 4 Feb 2020 14:39:11 -0800 Subject: [PATCH] feat(check): introduce page.check/uncheck (#826) --- docs/api.md | 96 +++++++++++++++++++++++++++++++++++++--------- src/dom.ts | 40 +++++++++++++++++++ src/frames.ts | 12 ++++++ src/page.ts | 8 ++++ test/click.spec.js | 54 ++++++++++++++++++++++++++ 5 files changed, 192 insertions(+), 18 deletions(-) diff --git a/docs/api.md b/docs/api.md index 5468e67218..81198d0f4a 100644 --- a/docs/api.md +++ b/docs/api.md @@ -446,6 +446,7 @@ page.removeListener('request', logRequest); - [page.addStyleTag(options)](#pageaddstyletagoptions) - [page.authenticate(credentials)](#pageauthenticatecredentials) - [page.browserContext()](#pagebrowsercontext) +- [page.check(selector, [options])](#pagecheckselector-options) - [page.click(selector[, options])](#pageclickselector-options) - [page.close([options])](#pagecloseoptions) - [page.content()](#pagecontent) @@ -483,6 +484,7 @@ page.removeListener('request', logRequest); - [page.title()](#pagetitle) - [page.tripleclick(selector[, options])](#pagetripleclickselector-options) - [page.type(selector, text[, options])](#pagetypeselector-text-options) +- [page.uncheck(selector, [options])](#pageuncheckselector-options) - [page.url()](#pageurl) - [page.viewport()](#pageviewport) - [page.waitFor(selectorOrFunctionOrTimeout[, options[, ...args]])](#pagewaitforselectororfunctionortimeout-options-args) @@ -729,6 +731,18 @@ To disable authentication, pass `null`. Get the browser context that the page belongs to. +#### page.check(selector, [options]) +- `selector` <[string]> A selector to search for checkbox or radio button to check. If there are multiple elements satisfying the selector, the first will be checked. +- `options` <[Object]> + - `waitFor` <"visible"|"hidden"|"any"|"nowait"> Wait for element to become visible (`visible`), hidden (`hidden`), present in dom (`any`) or do not wait at all (`nowait`). Defaults to `visible`. + - `timeout` <[number]> Maximum time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the [page.setDefaultTimeout(timeout)](#pagesetdefaulttimeouttimeout) method. +- returns: <[Promise]> Promise which resolves when the element matching `selector` is successfully checked. The Promise will be rejected if there is no element matching `selector`. + +This method fetches an element with `selector`, if element is not already checked, it scrolls it into view if needed, and then uses [page.click](#pageclickselector-options) to click in the center of the element. +If there's no element matching `selector`, the method throws an error. + +Shortcut for [page.mainFrame().check(selector[, options])](#framecheckselector-options). + #### page.click(selector[, options]) - `selector` <[string]> A selector to search for element to click. If there are multiple elements satisfying the selector, the first will be clicked. - `options` <[Object]> @@ -740,7 +754,7 @@ Get the browser context that the page belongs to. - y <[number]> - `modifiers` <[Array]<"Alt"|"Control"|"Meta"|"Shift">> Modifier keys to press. Ensures that only these modifiers are pressed during the click, and then restores current modifiers back. If not specified, currently pressed modifiers are used. - `waitFor` <"visible"|"hidden"|"any"|"nowait"> Wait for element to become visible (`visible`), hidden (`hidden`), present in dom (`any`) or do not wait at all (`nowait`). Defaults to `visible`. - - `timeout` <[number]> Maximum navigation time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the [page.setDefaultTimeout(timeout)](#pagesetdefaulttimeouttimeout) method. + - `timeout` <[number]> Maximum time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the [page.setDefaultTimeout(timeout)](#pagesetdefaulttimeouttimeout) method. - returns: <[Promise]> Promise which resolves when the element matching `selector` is successfully clicked. The Promise will be rejected if there is no element matching `selector`. This method fetches an element with `selector`, scrolls it into view if needed, and then uses [page.mouse](#pagemouse) to click in the center of the element. @@ -788,7 +802,7 @@ Gets the full HTML contents of the page, including the doctype. - y <[number]> - `modifiers` <[Array]<"Alt"|"Control"|"Meta"|"Shift">> Modifier keys to press. Ensures that only these modifiers are pressed during the double click, and then restores current modifiers back. If not specified, currently pressed modifiers are used. - `waitFor` <"visible"|"hidden"|"any"|"nowait"> Wait for element to become visible (`visible`), hidden (`hidden`), present in dom (`any`) or do not wait at all (`nowait`). Defaults to `visible`. - - `timeout` <[number]> Maximum navigation time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the [page.setDefaultTimeout(timeout)](#pagesetdefaulttimeouttimeout) method. + - `timeout` <[number]> Maximum time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the [page.setDefaultTimeout(timeout)](#pagesetdefaulttimeouttimeout) method. - returns: <[Promise]> Promise which resolves when the element matching `selector` is successfully double clicked. The Promise will be rejected if there is no element matching `selector`. This method fetches an element with `selector`, scrolls it into view if needed, and then uses [page.mouse](#pagemouse) to double click in the center of the element. @@ -988,7 +1002,7 @@ const fs = require('fs'); - `value` <[string]> Value to fill for the ``, `