/** * Copyright 2018 Google Inc. All rights reserved. * * 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. */ import os from 'os'; import expect from 'expect'; import type {KeyInput} from 'puppeteer-core/internal/common/USKeyboardLayout.js'; import {getTestState, setupTestBrowserHooks} from './mocha-utils.js'; import {attachFrame} from './utils.js'; describe('Keyboard', function () { setupTestBrowserHooks(); it('should type into a textarea', async () => { const {page} = await getTestState(); await page.evaluate(() => { const textarea = document.createElement('textarea'); document.body.appendChild(textarea); textarea.focus(); }); const text = 'Hello world. I am the text that was typed!'; await page.keyboard.type(text); expect( await page.evaluate(() => { return document.querySelector('textarea')!.value; }) ).toBe(text); }); it('should move with the arrow keys', async () => { const {page, server} = await getTestState(); await page.goto(server.PREFIX + '/input/textarea.html'); await page.type('textarea', 'Hello World!'); expect( await page.evaluate(() => { return document.querySelector('textarea')!.value; }) ).toBe('Hello World!'); for (const _ of 'World!') { await page.keyboard.press('ArrowLeft'); } await page.keyboard.type('inserted '); expect( await page.evaluate(() => { return document.querySelector('textarea')!.value; }) ).toBe('Hello inserted World!'); await page.keyboard.down('Shift'); for (const _ of 'inserted ') { await page.keyboard.press('ArrowLeft'); } await page.keyboard.up('Shift'); await page.keyboard.press('Backspace'); expect( await page.evaluate(() => { return document.querySelector('textarea')!.value; }) ).toBe('Hello World!'); }); // @see https://github.com/puppeteer/puppeteer/issues/1313 it('should trigger commands of keyboard shortcuts', async () => { const {page, server} = await getTestState(); const cmdKey = os.platform() === 'darwin' ? 'Meta' : 'Control'; await page.goto(server.PREFIX + '/input/textarea.html'); await page.type('textarea', 'hello'); await page.keyboard.down(cmdKey); await page.keyboard.press('a', {commands: ['SelectAll']}); await page.keyboard.up(cmdKey); await page.keyboard.down(cmdKey); await page.keyboard.down('c', {commands: ['Copy']}); await page.keyboard.up('c'); await page.keyboard.up(cmdKey); await page.keyboard.down(cmdKey); await page.keyboard.press('v', {commands: ['Paste']}); await page.keyboard.press('v', {commands: ['Paste']}); await page.keyboard.up(cmdKey); expect( await page.evaluate(() => { return document.querySelector('textarea')!.value; }) ).toBe('hellohello'); }); it('should send a character with ElementHandle.press', async () => { const {page, server} = await getTestState(); await page.goto(server.PREFIX + '/input/textarea.html'); using textarea = (await page.$('textarea'))!; await textarea.press('a'); expect( await page.evaluate(() => { return document.querySelector('textarea')!.value; }) ).toBe('a'); await page.evaluate(() => { return window.addEventListener( 'keydown', e => { return e.preventDefault(); }, true ); }); await textarea.press('b'); expect( await page.evaluate(() => { return document.querySelector('textarea')!.value; }) ).toBe('a'); }); it('ElementHandle.press should not support |text| option', async () => { const {page, server} = await getTestState(); await page.goto(server.PREFIX + '/input/textarea.html'); using textarea = (await page.$('textarea'))!; await textarea.press('a', {text: 'ё'}); expect( await page.evaluate(() => { return document.querySelector('textarea')!.value; }) ).toBe('a'); }); it('should send a character with sendCharacter', async () => { const {page, server} = await getTestState(); await page.goto(server.PREFIX + '/input/textarea.html'); await page.focus('textarea'); await page.evaluate(() => { (globalThis as any).inputCount = 0; (globalThis as any).keyDownCount = 0; window.addEventListener( 'input', () => { (globalThis as any).inputCount += 1; }, true ); window.addEventListener( 'keydown', () => { (globalThis as any).keyDownCount += 1; }, true ); }); await page.keyboard.sendCharacter('嗨'); expect( await page.$eval('textarea', textarea => { return { value: textarea.value, inputs: (globalThis as any).inputCount, keyDowns: (globalThis as any).keyDownCount, }; }) ).toMatchObject({value: '嗨', inputs: 1, keyDowns: 0}); await page.keyboard.sendCharacter('a'); expect( await page.$eval('textarea', textarea => { return { value: textarea.value, inputs: (globalThis as any).inputCount, keyDowns: (globalThis as any).keyDownCount, }; }) ).toMatchObject({value: '嗨a', inputs: 2, keyDowns: 0}); }); it('should send a character with sendCharacter in iframe', async () => { this.timeout(2000); const {page} = await getTestState(); await page.setContent(`