---
name: pw_keyboard_sequence
kind: function
lang: ts
domain: browser
version: "1.0.0"
purity: impure
signature: "async (page: Page, sequence: KbStep[]) => Promise<void>"
description: "Executes a sequence of keyboard interactions (focus, type, key press, wait) on a Playwright Page. Use to script realistic input flows like typing then navigating autocomplete dropdowns with arrow keys."
tags: ["playwright", "e2e", "browser", "keyboard"]
params:
  - name: page
    desc: "Playwright Page."
  - name: sequence
    desc: "Array of steps: focus/type/press/wait. Executed in order."
output: "void; mutates focused element / page state."
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: ["playwright"]
tested: true
tests:
  - "focus calls focus"
  - "type uses default delay"
  - "press routes correctly"
  - "wait calls waitForTimeout"
  - "unknown kind throws"
  - "order preserved"
test_file_path: "frontend/functions/browser/pw_keyboard_sequence.test.ts"
file_path: "frontend/functions/browser/pw_keyboard_sequence.ts"
---

## Ejemplo

```ts
import { pw_keyboard_sequence } from "./pw_keyboard_sequence";

await pw_keyboard_sequence(page, [
  { kind: "focus", selector: "input[name=requester]" },
  { kind: "type", text: "Enma" },
  { kind: "wait", ms: 200 },
  { kind: "press", key: "ArrowDown" },
  { kind: "press", key: "ArrowDown" },
  { kind: "press", key: "Enter" },
]);
```

## Cuando usarla

Cuando necesites encadenar foco + escritura + teclas especiales sobre un elemento (autocompletado, selects, modales de búsqueda). Úsala en tests E2E con Playwright en lugar de mezclar calls sueltas de `keyboard.type` y `keyboard.press`.

## Gotchas

- `focus` usa `.first()` — si el selector coincide con múltiples elementos, foca el primero. Afinar el selector si es necesario.
- `type` simula escritura carácter a carácter con `delay` (default 30 ms). Para input más rápido pasar `delayMs: 0`.
- `press` espera un nombre de tecla Playwright válido: `"ArrowDown"`, `"Enter"`, `"Escape"`, `"Tab"`, etc.
- Un `kind` desconocido lanza en runtime (útil para detectar typos en JS sin TypeScript).
