From e70f98ddb93e460777e5bc93a19fea68efab904c Mon Sep 17 00:00:00 2001 From: AlexChung1995 Date: Tue, 31 Oct 2017 21:47:52 -0700 Subject: [PATCH] feat(Page.select): return selected options from Page.select (#1099) This patch teaches Page.select to return an array of actually selected options. If no option is selected, an empty array will be returned. --- docs/api.md | 4 ++-- lib/Page.js | 15 ++++++-------- test/test.js | 58 +++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 50 insertions(+), 27 deletions(-) diff --git a/docs/api.md b/docs/api.md index 23b70720..5f54e0f2 100644 --- a/docs/api.md +++ b/docs/api.md @@ -929,10 +929,10 @@ Shortcut for [page.mainFrame().executionContext().queryObjects(prototypeHandle)] #### page.select(selector, ...values) - `selector` <[string]> A [selector] to query page for - `...values` <...[string]> Values of options to select. If the `` element matching `selector`, the method throws an error. +If there's no ` element.'); - const options = Array.from(element.options); - - if (element.multiple) { - for (const option of options) - option.selected = values.includes(option.value); - } else { - element.value = values.shift(); - } + 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); } diff --git a/test/test.js b/test/test.js index 3b0bde15..5f98ee57 100644 --- a/test/test.js +++ b/test/test.js @@ -2712,27 +2712,53 @@ describe('Page', function() { expect(await page.evaluate(() => result.onBubblingChange)).toEqual(['blue']); })); - it('should work with no options', SX(async function() { - await page.goto(PREFIX + '/input/select.html'); - await page.evaluate(() => makeEmpty()); - await page.select('select', '42'); - expect(await page.evaluate(() => result.onInput)).toEqual([]); - expect(await page.evaluate(() => result.onChange)).toEqual([]); - })); - - it('should not select a non-existent option', SX(async function() { - await page.goto(PREFIX + '/input/select.html'); - await page.select('select', '42'); - expect(await page.evaluate(() => result.onInput)).toEqual([]); - expect(await page.evaluate(() => result.onChange)).toEqual([]); - })); - - it('should throw', SX(async function() { + it('should throw when element is not a element.'); })); + + it('should return [] on no matched values', SX(async function() { + await page.goto(PREFIX + '/input/select.html'); + const result = await page.select('select','42','abc'); + expect(result).toEqual([]); + })); + + it('should return an array of matched values', SX(async function() { + await page.goto(PREFIX + '/input/select.html'); + await page.evaluate(() => makeMultiple()); + const result = await page.select('select','blue','black','magenta'); + expect(result.reduce((accumulator,current) => ['blue', 'black', 'magenta'].includes(current) && accumulator, true)).toEqual(true); + })); + + it('should return an array of one element when multiple is not set', SX(async function() { + await page.goto(PREFIX + '/input/select.html'); + const result = await page.select('select','42','blue','black','magenta'); + expect(result.length).toEqual(1); + })); + + it('should return [] on no values',SX(async function() { + await page.goto(PREFIX + '/input/select.html'); + const result = await page.select('select'); + expect(result).toEqual([]); + })); + + it('should deselect all options when passed no values for a multiple select',SX(async function() { + await page.goto(PREFIX + '/input/select.html'); + await page.evaluate(() => makeMultiple()); + await page.select('select','blue','black','magenta'); + await page.select('select'); + expect(await page.$eval('select', select => Array.from(select.options).every(option => !option.selected))).toEqual(true); + })); + + it('should deselect all options when passed no values for a select without multiple',SX(async function() { + await page.goto(PREFIX + '/input/select.html'); + await page.select('select','blue','black','magenta'); + await page.select('select'); + expect(await page.$eval('select', select => Array.from(select.options).every(option => !option.selected))).toEqual(true); + })); + }); describe('Tracing', function() {