From 067662c677585be53dd28c1ae6a41cde9494e357 Mon Sep 17 00:00:00 2001 From: Philipp Hagemeister Date: Thu, 12 Mar 2020 17:26:03 +0100 Subject: [PATCH] Emit change event on uploadFile calls (#5389) In puppeteer 1.20.0, the following code emitted an alert: ``` const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch({args: ['--no-sandbox'], headless: false}); const page = (await browser.pages())[0]; await page.setContent(` `); const input = await page.waitForSelector('input[type="file"]'); await input.uploadFile(__filename); })(); ``` A bisection yielded 6091a34a360b0b8aec4d67f6190f131d7e8e3300 as the commit that broke this. Emitting the change event as well seems to fix the problem. --- lib/JSHandle.js | 1 + test/input.spec.js | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/lib/JSHandle.js b/lib/JSHandle.js index e5ff0041..fc88ff77 100644 --- a/lib/JSHandle.js +++ b/lib/JSHandle.js @@ -341,6 +341,7 @@ class ElementHandle extends JSHandle { } element.files = dt.files; element.dispatchEvent(new Event('input', { bubbles: true })); + element.dispatchEvent(new Event('change', { bubbles: true })); }, files); } diff --git a/test/input.spec.js b/test/input.spec.js index 6b768983..39ef35ab 100644 --- a/test/input.spec.js +++ b/test/input.spec.js @@ -27,8 +27,14 @@ module.exports.addTests = function({testRunner, expect, puppeteer}) { await page.goto(server.PREFIX + '/input/fileupload.html'); const filePath = path.relative(process.cwd(), FILE_TO_UPLOAD); const input = await page.$('input'); + await page.evaluate(e => { + window._inputEvents = []; + e.addEventListener('change', ev => window._inputEvents.push(ev.type)); + e.addEventListener('input', ev => window._inputEvents.push(ev.type)); + }, input); await input.uploadFile(filePath); expect(await page.evaluate(e => e.files[0].name, input)).toBe('file-to-upload.txt'); + expect(await page.evaluate(() => window._inputEvents)).toEqual(['input', 'change']); expect(await page.evaluate(e => { const reader = new FileReader(); const promise = new Promise(fulfill => reader.onload = fulfill);