Skip to content

Commit

Permalink
setInputFiles events
Browse files Browse the repository at this point in the history
  • Loading branch information
yury-s committed Dec 21, 2023
1 parent 5bd75eb commit a0f5b75
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
12 changes: 6 additions & 6 deletions packages/playwright-core/src/server/injected/injectedScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,8 +720,8 @@ export class InjectedScript {
select.value = undefined as any;
selectedOptions.forEach(option => option.selected = true);
progress.log(' selected specified option(s)');
select.dispatchEvent(new Event('input', { 'bubbles': true, 'composed': true }));
select.dispatchEvent(new Event('change', { 'bubbles': true }));
select.dispatchEvent(new Event('input', { bubbles: true, composed: true }));
select.dispatchEvent(new Event('change', { bubbles: true }));
return selectedOptions.map(option => option.value);
}

Expand Down Expand Up @@ -749,8 +749,8 @@ export class InjectedScript {
input.value = value;
if (input.value !== value)
throw this.createStacklessError('Malformed value');
element.dispatchEvent(new Event('input', { 'bubbles': true, 'composed': true }));
element.dispatchEvent(new Event('change', { 'bubbles': true }));
element.dispatchEvent(new Event('input', { bubbles: true, composed: true }));
element.dispatchEvent(new Event('change', { bubbles: true }));
return 'done'; // We have already changed the value, no need to input it.
}
} else if (element.nodeName.toLowerCase() === 'textarea') {
Expand Down Expand Up @@ -852,8 +852,8 @@ export class InjectedScript {
for (const file of files)
dt.items.add(file);
input.files = dt.files;
input.dispatchEvent(new Event('input', { 'bubbles': true }));
input.dispatchEvent(new Event('change', { 'bubbles': true }));
input.dispatchEvent(new Event('input', { bubbles: true, composed: true }));
input.dispatchEvent(new Event('change', { bubbles: true }));
}

expectHitTarget(hitPoint: { x: number, y: number }, targetElement: Element) {
Expand Down
35 changes: 35 additions & 0 deletions tests/page/page-set-input-files.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,41 @@ it('should emit input and change events', async ({ page, asset }) => {
expect(events[1].type).toBe('change');
});

it('input event.composed should be true and cross shadow dom boundary', async ({ page, server, asset }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/28726' });
await page.goto(server.EMPTY_PAGE);
await page.setContent(`<body><script>
const div = document.createElement('div');
const shadowRoot = div.attachShadow({mode: 'open'});
shadowRoot.innerHTML = '<input type=file></input>';
document.body.appendChild(div);
</script></body>`);
await page.locator('body').evaluate(select => {
(window as any).firedBodyEvents = [];
for (const event of ['input', 'change']) {
select.addEventListener(event, e => {
(window as any).firedBodyEvents.push(e.type + ':' + e.composed);
}, false);
}
});

await page.locator('input').evaluate(select => {
(window as any).firedEvents = [];
for (const event of ['input', 'change']) {
select.addEventListener(event, e => {
(window as any).firedEvents.push(e.type + ':' + e.composed);
}, false);
}
});
await page.locator('input').setInputFiles({
name: 'test.txt',
mimeType: 'text/plain',
buffer: Buffer.from('this is a test')
});
expect(await page.evaluate(() => window['firedEvents'])).toEqual(['input:true', 'change:false']);
expect(await page.evaluate(() => window['firedBodyEvents'])).toEqual(['input:true']);
});

it('should work for single file pick', async ({ page, server }) => {
await page.setContent(`<input type=file>`);
const [fileChooser] = await Promise.all([
Expand Down

0 comments on commit a0f5b75

Please sign in to comment.