diff --git a/docs/api.md b/docs/api.md index d5cd4bf5dd7..0f73e1a170c 100644 --- a/docs/api.md +++ b/docs/api.md @@ -128,6 +128,7 @@ * [frame.isDetached()](#frameisdetached) * [frame.name()](#framename) * [frame.parentFrame()](#frameparentframe) + * [frame.select(selector, ...values)](#frameselectselector-values) * [frame.title()](#frametitle) * [frame.url()](#frameurl) * [frame.waitFor(selectorOrFunctionOrTimeout[, options[, ...args]])](#framewaitforselectororfunctionortimeout-options-args) @@ -932,13 +933,15 @@ Shortcut for [page.mainFrame().executionContext().queryObjects(prototypeHandle)] - returns: <[Promise]<[Array]<[string]>>> Returns an array of option values that have been successfully selected. Triggers a `change` and `input` event once all the provided options have been selected. -If there's no `` element matching `selector`, the method throws an error. ```js page.select('select#colors', 'blue'); // single selection page.select('select#colors', 'red', 'green', 'blue'); // multiple selections ``` +Shortcut for [page.mainFrame.select()](#frameselectselector-values) + #### page.setContent(html, options) - `html` <[string]> HTML markup to assign to the page. - `options` <[Object]> Navigation parameters which might have the following properties: @@ -1504,6 +1507,19 @@ If the name is empty, returns the id attribute instead. #### frame.parentFrame() - returns: <[Frame]> Returns parent frame, if any. Detached frames and main frames return `null`. +#### frame.select(selector, ...values) +- `selector` <[string]> A [selector] to query frame for +- `...values` <...[string]> Values of options to select. If the `` element matching `selector`, the method throws an error. + +```js +frame.select('select#colors', 'blue'); // single selection +frame.select('select#colors', 'red', 'green', 'blue'); // multiple selections +``` + #### frame.title() - returns: <[Promise]<[string]>> Returns page's title. diff --git a/lib/FrameManager.js b/lib/FrameManager.js index 051f2ce398b..222f1203fe8 100644 --- a/lib/FrameManager.js +++ b/lib/FrameManager.js @@ -406,6 +406,26 @@ class Frame { } } + /** + * @param {string} selector + * @param {!Array} values + * @return {!Promise>} + */ + async select(selector, ...values){ + return await this.$eval(selector, (element, values) => { + if (element.nodeName.toLowerCase() !== 'select') + throw new Error('Element is not a element.'); - const options = Array.from(element.options); - element.value = undefined; - for (const option of options) - option.selected = values.includes(option.value); - element.dispatchEvent(new Event('input', { 'bubbles': true })); - element.dispatchEvent(new Event('change', { 'bubbles': true })); - return options.filter(option => option.selected).map(option => option.value); - }, values); + return this.mainFrame().select(selector, ...values); } /**