Keyboard and input methods

A to-do list used with the keyboard only. The check moves focus with Tab, types text at once and a key at a time, composes Japanese with an input method, ticks a task off with Space, removes it with Enter and brings it back with undo (Mod+z).

Run it#

shell
cd examples/keyboard
npx brokit check --verbose      # --verbose lists every step, as below
terminal
$ brokit check --verbose
brokit check: 1 scenario, Chrome 158.0.8127.36, software drawing
✓ checks/todo  30 steps, 1.0 s, 1 baseline written
    ✓ key Tab
    ✓ expect document.activeElement.id equals "new"
    ✓ type "Buy milk"
    ✓ expect app.keys equals 0
    ✓ key Enter  160 ms
    ✓ type "Call Ana"
    ✓ expect app.keys equals 9
    ✓ key Enter
    ✓ expect app.tasks.map((t) => t.text) equals ["Buy milk","Call Ana"]
    ✓ type "Pay rent"
    ✓ key Mod+z
    ✓ expect document.querySelector('#new').value equals ""
    ✓ ime "かいもの"
    ✓ expect [document.querySelector('#new').value, app.composing] equals ["かいもの",true]
    ✓ type "買い物"
    ✓ expect [document.querySelector('#new').value, app.composing] equals ["買い物",false]
    ✓ key Enter
    ✓ expect app.tasks.length equals 3
    ✓ key Tab
    ✓ key Tab
    ✓ expect document.activeElement.matches('#list li:first-child input') truthy
    ✓ key Space
    ✓ expect app.tasks[0].done equals true
    ✓ key Tab
    ✓ key Enter
    ✓ expect app.tasks.map((t) => t.text) equals ["Call Ana","買い物"]
    ✓ key Mod+z
    ✓ expect app.tasks.map((t) => t.text) equals ["Buy milk","Call Ana","買い物"]
    ✓ expect app.tasks[0].done equals true
    ✓ screenshot list → brokit-out/checks/todo/list.png (800×650) [baseline: new]  199 ms
1 scenario passed in 1.0 s

The first run writes the baseline (checks/baselines/todo/list.png); the runs after it compare against it. In a real project you commit the baselines. They're not committed here, since they depend on the fonts of the machine that made them (the last task needs a Japanese font).

A to-do list: Buy milk ticked and struck through, then Call Ana and 買い物

The baseline: the list after the keyboard and the input method were done with it.

The page#

index.html is a field, an Add button and a list. app.js keeps a copy of the list before each change, so Ctrl+Z (Cmd+Z on a Mac) undoes the last one, except in the text field, where it's the field's own undo. For the check to read, it keeps window.app: the tasks, how many keys were pressed in the field, and whether an input method is composing.

The check#

The check, checks/todo.check.json:

checks/todo.check.json
{
  "note": "The to-do list with the keyboard only: Tab, typing, an input method, Space, Enter and undo.",
  "url": "/index.html",
  "viewport": { "size": "640x520", "dpr": 2 },
  "ready": "document.readyState === 'complete'",
  "steps": [
    { "key": "Tab", "note": "from nothing focused, Tab goes to the first field" },
    { "expect": "document.activeElement.id", "equals": "new" },

    { "type": "Buy milk", "note": "at once, as a paste: no key events" },
    { "expect": "app.keys", "equals": 0 },
    { "key": "Enter" },
    { "type": "Call Ana", "perKey": true, "note": "a key press per character" },
    { "expect": "app.keys", "equals": 9, "note": "Enter, then 8 keys" },
    { "key": "Enter" },
    { "expect": "app.tasks.map((t) => t.text)", "equals": ["Buy milk", "Call Ana"] },

    { "type": "Pay rent" },
    { "key": "Mod+z", "note": "Ctrl+Z, or Cmd+Z on macOS: in the field, it undoes the typing" },
    { "expect": "document.querySelector('#new').value", "equals": "" },

    { "ime": "かいもの", "note": "an input method composing, not committed yet" },
    { "expect": "[document.querySelector('#new').value, app.composing]", "equals": ["かいもの", true] },
    { "type": "買い物", "note": "commits the composition" },
    { "expect": "[document.querySelector('#new').value, app.composing]", "equals": ["買い物", false] },
    { "key": "Enter" },
    { "expect": "app.tasks.length", "equals": 3 },

    { "key": "Tab", "note": "to the Add button" },
    { "key": "Tab", "note": "to the first task" },
    { "expect": "document.activeElement.matches('#list li:first-child input')" },
    { "key": "Space", "note": "ticks it off" },
    { "expect": "app.tasks[0].done", "equals": true },
    { "key": "Tab", "note": "to its remove button" },
    { "key": "Enter" },
    { "expect": "app.tasks.map((t) => t.text)", "equals": ["Call Ana", "買い物"] },
    { "key": "Mod+z", "note": "outside the field, the list's own undo brings the task back" },
    { "expect": "app.tasks.map((t) => t.text)", "equals": ["Buy milk", "Call Ana", "買い物"] },
    { "expect": "app.tasks[0].done", "equals": true },

    { "screenshot": "list", "selector": ".card", "baseline": true }
  ]
}

Learn more#

Edit this page on GitHub