From 52ae218bfc51a878e8a37a3bb3707e9b7cf3079a Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Wed, 2 Dec 2020 14:43:41 -0800 Subject: [PATCH] fix(fill): allow filling more input types (#4563) This includes invalid types that are no recognized by the browser. --- src/server/injected/injectedScript.ts | 4 +-- test/page-fill.spec.ts | 38 +++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/server/injected/injectedScript.ts b/src/server/injected/injectedScript.ts index e49fb2c94e..ba45417daf 100644 --- a/src/server/injected/injectedScript.ts +++ b/src/server/injected/injectedScript.ts @@ -281,8 +281,8 @@ export class InjectedScript { } if (element.nodeName.toLowerCase() === 'input') { const input = element as HTMLInputElement; - const type = (input.getAttribute('type') || '').toLowerCase(); - const kDateTypes = new Set(['date', 'time', 'datetime', 'datetime-local']); + const type = input.type.toLowerCase(); + const kDateTypes = new Set(['date', 'time', 'datetime', 'datetime-local', 'month', 'week']); const kTextInputTypes = new Set(['', 'email', 'number', 'password', 'search', 'tel', 'text', 'url']); if (!kTextInputTypes.has(type) && !kDateTypes.has(type)) { progress.log(` input of type "${type}" cannot be filled`); diff --git a/test/page-fill.spec.ts b/test/page-fill.spec.ts index f2552c119d..cbb3d4fb32 100644 --- a/test/page-fill.spec.ts +++ b/test/page-fill.spec.ts @@ -54,7 +54,7 @@ it('should fill textarea with label', async ({page}) => { it('should throw on unsupported inputs', async ({page, server}) => { await page.goto(server.PREFIX + '/input/textarea.html'); - for (const type of ['color', 'file']) { + for (const type of ['button', 'checkbox', 'file', 'image', 'radio', 'range', 'reset', 'submit']) { await page.$eval('input', (input, type) => input.setAttribute('type', type), type); let error = null; await page.fill('input', '').catch(e => error = e); @@ -64,7 +64,7 @@ it('should throw on unsupported inputs', async ({page, server}) => { it('should fill different input types', async ({page, server}) => { await page.goto(server.PREFIX + '/input/textarea.html'); - for (const type of ['password', 'search', 'tel', 'text', 'url']) { + for (const type of ['password', 'search', 'tel', 'text', 'url', 'invalid-type']) { await page.$eval('input', (input, type) => input.setAttribute('type', type), type); await page.fill('input', 'text ' + type); expect(await page.evaluate(() => window['result'])).toBe('text ' + type); @@ -79,7 +79,7 @@ it('should fill date input after clicking', async ({page, server}) => { }); it('should throw on incorrect date', (test, { browserName }) => { - test.skip(browserName === 'webkit'); + test.skip(browserName === 'webkit', 'WebKit does not support date inputs'); }, async ({page}) => { await page.setContent(''); const error = await page.fill('input', '2020-13-05').catch(e => e); @@ -92,8 +92,36 @@ it('should fill time input', async ({page}) => { expect(await page.$eval('input', input => input.value)).toBe('13:15'); }); +it('should fill month input', async ({page}) => { + await page.setContent(''); + await page.fill('input', '2020-07'); + expect(await page.$eval('input', input => input.value)).toBe('2020-07'); +}); + +it('should throw on incorrect month', (test, { browserName }) => { + test.skip(browserName !== 'chromium', 'Only Chromium supports month inputs'); +}, async ({page}) => { + await page.setContent(''); + const error = await page.fill('input', '2020-13').catch(e => e); + expect(error.message).toContain('Malformed value'); +}); + +it('should fill week input', async ({page}) => { + await page.setContent(''); + await page.fill('input', '2020-W50'); + expect(await page.$eval('input', input => input.value)).toBe('2020-W50'); +}); + +it('should throw on incorrect week', (test, { browserName }) => { + test.skip(browserName !== 'chromium', 'Only Chromium supports week inputs'); +}, async ({page}) => { + await page.setContent(''); + const error = await page.fill('input', '2020-123').catch(e => e); + expect(error.message).toContain('Malformed value'); +}); + it('should throw on incorrect time', (test, { browserName }) => { - test.skip(browserName === 'webkit'); + test.skip(browserName === 'webkit', 'WebKit does not support time inputs'); }, async ({page}) => { await page.setContent(''); const error = await page.fill('input', '25:05').catch(e => e); @@ -107,7 +135,7 @@ it('should fill datetime-local input', async ({page, server}) => { }); it('should throw on incorrect datetime-local', (test, { browserName }) => { - test.skip(browserName === 'webkit' || browserName === 'firefox'); + test.skip(browserName !== 'chromium', 'Only Chromium supports datetime-local inputs'); }, async ({page, server}) => { await page.setContent(''); const error = await page.fill('input', 'abc').catch(e => e);