Skip to content

[BUG] type() is misleading, does not actually press keys if character requires a modifier key held #27130

@WestonThayer

Description

@WestonThayer

System info

  • Playwright Version: v1.37.1
  • Operating System: All
  • Browser: All
  • Other info:

Source code

  • I provided exact source code that allows reproducing the issue locally.

Config file

// playwright.config.ts
import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  projects: [
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'], },
    },
  ]
});

Test file (self-contained)

it('should check the box using setChecked', async ({ page }) => {
  await page.setContent(`
<input id="input"></input>
<script>
window.__keyLog = "";
document.getElementById("input").addEventListener("keydown", (e) => {
  window.__keyLog += e.code + ",";
});
</script>
`);

  await page.locator("#input").type("ABC");
  const keyLog = await page.evaluate("window.__keyLog");
  expect(keyLog).toContain("Shift");
});

Steps

  • Run the test

Expected

keyLog is something like ShiftLeft,KeyA,ShiftLeft,KeyB,ShiftLeft,KeyC, .

Actual

keyLog is KeyA,KeyB,KeyC, .

Notes

I see that #24614 changes to pressSequentially(), but it appears to use type() under the hood, so shouldn't make a difference.

I think the issue is that

async type(text: string, options?: { delay?: number }) {
const delay = (options && options.delay) || undefined;
for (const char of text) {
if (usKeyboardLayout.has(char)) {
await this.press(char, { delay });
} else {
if (delay)
await new Promise(f => setTimeout(f, delay));
await this.insertText(char);
}
}
}

calls insertText when a modifier key would be required, which for Chromium eventually calls https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-insertText.

I find this misleading, especially for the Page.keyboard.type API — I expect it to exactly emulate the events a human typing on a real keyboard would generate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions