| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2017 Google Inc. All rights reserved. | 
					
						
							|  |  |  |  * Modifications copyright (c) Microsoft Corporation. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-08-19 21:32:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-26 10:59:27 -07:00
										 |  |  | import { it, expect } from './fixtures'; | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should select single option', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/select.html'); | 
					
						
							|  |  |  |   await page.selectOption('select', 'blue'); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   expect(await page.evaluate(() => window['result'].onInput)).toEqual(['blue']); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => window['result'].onChange)).toEqual(['blue']); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should select single option by value', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/select.html'); | 
					
						
							|  |  |  |   await page.selectOption('select', { value: 'blue' }); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   expect(await page.evaluate(() => window['result'].onInput)).toEqual(['blue']); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => window['result'].onChange)).toEqual(['blue']); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should select single option by label', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/select.html'); | 
					
						
							|  |  |  |   await page.selectOption('select', { label: 'Indigo' }); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   expect(await page.evaluate(() => window['result'].onInput)).toEqual(['indigo']); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => window['result'].onChange)).toEqual(['indigo']); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should select single option by handle', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/select.html'); | 
					
						
							|  |  |  |   await page.selectOption('select', await page.$('[id=whiteOption]')); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   expect(await page.evaluate(() => window['result'].onInput)).toEqual(['white']); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => window['result'].onChange)).toEqual(['white']); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should select single option by index', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/select.html'); | 
					
						
							|  |  |  |   await page.selectOption('select', { index: 2 }); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   expect(await page.evaluate(() => window['result'].onInput)).toEqual(['brown']); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => window['result'].onChange)).toEqual(['brown']); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should select single option by multiple attributes', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/select.html'); | 
					
						
							|  |  |  |   await page.selectOption('select', { value: 'green', label: 'Green' }); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   expect(await page.evaluate(() => window['result'].onInput)).toEqual(['green']); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => window['result'].onChange)).toEqual(['green']); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should not select single option when some attributes do not match', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/select.html'); | 
					
						
							|  |  |  |   await page.selectOption('select', { value: 'green', label: 'Brown' }); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => document.querySelector('select').value)).toEqual(''); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should select only first option', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/select.html'); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   await page.selectOption('select', ['blue', 'green', 'red']); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => window['result'].onInput)).toEqual(['blue']); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => window['result'].onChange)).toEqual(['blue']); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should not throw when select causes navigation', async ({page, server}) => { | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/input/select.html'); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   await page.$eval('select', select => select.addEventListener('input', () => window.location.href = '/empty.html')); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await Promise.all([ | 
					
						
							|  |  |  |     page.selectOption('select', 'blue'), | 
					
						
							|  |  |  |     page.waitForNavigation(), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(page.url()).toContain('empty.html'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should select multiple options', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/select.html'); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   await page.evaluate(() => window['makeMultiple']()); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.selectOption('select', ['blue', 'green', 'red']); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   expect(await page.evaluate(() => window['result'].onInput)).toEqual(['blue', 'green', 'red']); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => window['result'].onChange)).toEqual(['blue', 'green', 'red']); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should select multiple options with attributes', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/select.html'); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   await page.evaluate(() => window['makeMultiple']()); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.selectOption('select', [{ value: 'blue' }, { label: 'Green' }, { index: 4 }]); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   expect(await page.evaluate(() => window['result'].onInput)).toEqual(['blue', 'gray', 'green']); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => window['result'].onChange)).toEqual(['blue', 'gray', 'green']); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should respect event bubbling', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/select.html'); | 
					
						
							|  |  |  |   await page.selectOption('select', 'blue'); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   expect(await page.evaluate(() => window['result'].onBubblingInput)).toEqual(['blue']); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => window['result'].onBubblingChange)).toEqual(['blue']); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should throw when element is not a <select>', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   let error = null; | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/input/select.html'); | 
					
						
							|  |  |  |   await page.selectOption('body', '').catch(e => error = e); | 
					
						
							|  |  |  |   expect(error.message).toContain('Element is not a <select> element.'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should return [] on no matched values', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/select.html'); | 
					
						
							|  |  |  |   const result = await page.selectOption('select', ['42','abc']); | 
					
						
							|  |  |  |   expect(result).toEqual([]); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should return an array of matched values', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/select.html'); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   await page.evaluate(() => window['makeMultiple']()); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   const result = await page.selectOption('select', ['blue','black','magenta']); | 
					
						
							|  |  |  |   expect(result.reduce((accumulator,current) => ['blue', 'black', 'magenta'].includes(current) && accumulator, true)).toEqual(true); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should return an array of one element when multiple is not set', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/select.html'); | 
					
						
							|  |  |  |   const result = await page.selectOption('select',['42','blue','black','magenta']); | 
					
						
							|  |  |  |   expect(result.length).toEqual(1); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should return [] on no values',async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/select.html'); | 
					
						
							|  |  |  |   const result = await page.selectOption('select', []); | 
					
						
							|  |  |  |   expect(result).toEqual([]); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should not allow null items',async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/select.html'); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   await page.evaluate(() => window['makeMultiple']()); | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |   let error = null; | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.selectOption('select', ['blue', null, 'black','magenta']).catch(e => error = e); | 
					
						
							|  |  |  |   expect(error.message).toContain('options[1]: expected object, got null'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should unselect with null',async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/select.html'); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   await page.evaluate(() => window['makeMultiple']()); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   const result = await page.selectOption('select', ['blue', 'black','magenta']); | 
					
						
							|  |  |  |   expect(result.reduce((accumulator,current) => ['blue', 'black', 'magenta'].includes(current) && accumulator, true)).toEqual(true); | 
					
						
							|  |  |  |   await page.selectOption('select', null); | 
					
						
							|  |  |  |   expect(await page.$eval('select', select => Array.from(select.options).every(option => !option.selected))).toEqual(true); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should deselect all options when passed no values for a multiple select',async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/select.html'); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   await page.evaluate(() => window['makeMultiple']()); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.selectOption('select', ['blue','black','magenta']); | 
					
						
							|  |  |  |   await page.selectOption('select', []); | 
					
						
							|  |  |  |   expect(await page.$eval('select', select => Array.from(select.options).every(option => !option.selected))).toEqual(true); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should deselect all options when passed no values for a select without multiple',async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/select.html'); | 
					
						
							|  |  |  |   await page.selectOption('select', ['blue','black','magenta']); | 
					
						
							|  |  |  |   await page.selectOption('select', []); | 
					
						
							|  |  |  |   expect(await page.$eval('select', select => Array.from(select.options).every(option => !option.selected))).toEqual(true); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should throw if passed wrong types', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   let error; | 
					
						
							|  |  |  |   await page.setContent('<select><option value="12"/></select>'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   error = null; | 
					
						
							|  |  |  |   try { | 
					
						
							| 
									
										
										
										
											2020-09-09 03:06:52 -07:00
										 |  |  |     // @ts-expect-error cannot select numbers
 | 
					
						
							|  |  |  |     await page.selectOption('select', 12); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   } catch (e) { | 
					
						
							|  |  |  |     error = e; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   expect(error.message).toContain('options[0]: expected object, got number'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   error = null; | 
					
						
							|  |  |  |   try { | 
					
						
							| 
									
										
										
										
											2020-09-09 03:06:52 -07:00
										 |  |  |     // @ts-expect-error cannot select numbers
 | 
					
						
							|  |  |  |     await page.selectOption('select', { value: 12 }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   } catch (e) { | 
					
						
							|  |  |  |     error = e; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   expect(error.message).toContain('options[0].value: expected string, got number'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   error = null; | 
					
						
							|  |  |  |   try { | 
					
						
							| 
									
										
										
										
											2020-09-09 03:06:52 -07:00
										 |  |  |     // @ts-expect-error cannot select numbers
 | 
					
						
							|  |  |  |     await page.selectOption('select', { label: 12 }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   } catch (e) { | 
					
						
							|  |  |  |     error = e; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   expect(error.message).toContain('options[0].label: expected string, got number'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   error = null; | 
					
						
							|  |  |  |   try { | 
					
						
							| 
									
										
										
										
											2020-09-09 03:06:52 -07:00
										 |  |  |     // @ts-expect-error cannot select string indices
 | 
					
						
							|  |  |  |     await page.selectOption('select', { index: '12' }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   } catch (e) { | 
					
						
							|  |  |  |     error = e; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   expect(error.message).toContain('options[0].index: expected number, got string'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | // @see https://github.com/GoogleChrome/puppeteer/issues/3327
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should work when re-defining top-level Event class', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/input/select.html'); | 
					
						
							|  |  |  |   await page.evaluate(() => window.Event = null); | 
					
						
							|  |  |  |   await page.selectOption('select', 'blue'); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   expect(await page.evaluate(() => window['result'].onInput)).toEqual(['blue']); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => window['result'].onChange)).toEqual(['blue']); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | }); |