Skip to content

Commit

Permalink
fix(keyboard): correctly press enter on firefox (#1023)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelEinbinder authored Feb 15, 2020
1 parent df8de20 commit 21acb36
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/firefox/ffInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ export class RawKeyboardImpl implements input.RawKeyboard {
code = 'OSLeft';
if (code === 'MetaRight')
code = 'OSRight';
// Firefox will figure out Enter by itself
if (text === '\r')
text = '';
await this._client.send('Page.dispatchKeyEvent', {
type: 'keydown',
keyCode: keyCodeWithoutLocation,
Expand Down
26 changes: 17 additions & 9 deletions test/keyboard.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,23 @@ module.exports.describe = function({testRunner, expect, FFOX, CHROMIUM, WEBKIT,
await textarea.press('NumpadSubtract');
expect(await page.evaluate('keyLocation')).toBe(3);
});
it.skip(FFOX)('should press Enter', async({page, server}) => {
await page.setContent('<input></input>');
await page.$eval('input', body => body.addEventListener('keydown', event => {
if (event.key === 'Enter')
window.ENTER_DOWN = true;
}, false));
await page.focus('input');
await page.keyboard.press('Enter');
expect(await page.evaluate(() => window.ENTER_DOWN)).toBe(true);
it('should press Enter', async({page, server}) => {
await page.setContent('<textarea></textarea>');
await page.focus('textarea');
await page.evaluate(() => window.addEventListener('keydown', e => window.lastEvent = {key: e.key, code:e.code}));
await testEnterKey('Enter', 'Enter', 'Enter');
await testEnterKey('NumpadEnter', 'Enter', 'NumpadEnter');
await testEnterKey('\n', 'Enter', 'Enter');
await testEnterKey('\r', 'Enter', 'Enter');

async function testEnterKey(key, expectedKey, expectedCode) {
await page.keyboard.press(key);
const lastEvent = await page.evaluate('lastEvent');
expect(lastEvent.key).toBe(expectedKey, `${JSON.stringify(key)} had the wrong key: ${lastEvent.key}`);
expect(lastEvent.code).toBe(expectedCode, `${JSON.stringify(key)} had the wrong code: ${lastEvent.code}`);
expect(await page.$eval('textarea', t => t.value)).toBe('\n', `${JSON.stringify(key)} failed to create a newline`);
await page.$eval('textarea', t => t.value = '');
}
});
it('should throw on unknown keys', async({page, server}) => {
let error = await page.keyboard.press('NotARealKey').catch(e => e);
Expand Down

0 comments on commit 21acb36

Please sign in to comment.