Sign-up form

A sign-up form, checked twice: on a laptop with clicks and keys, and on a phone with taps. The checks type into fields, press Enter, check the error and welcome messages with expect, and compare screenshots with their baselines.

Run it#

shell
cd examples/form
npx brokit check --verbose      # --verbose lists every step, as below
terminal
$ brokit check --verbose
brokit check: 2 scenarios, Chrome 158.0.8127.36, software drawing
✓ checks/signup-phone  12 steps, 1.1 s, 2 baselines written
    ✓ expect document.documentElement.scrollWidth <= innerWidth truthy
    ✓ tap #name  155 ms
    ✓ type "Ana Pop"
    ✓ tap #email
    ✓ type "ana@example.com"
    ✓ tap #password
    ✓ type "correct horse battery"
    ✓ screenshot filled → brokit-out/checks/signup-phone/filled.png (786×1704) [baseline: new]  269 ms
    ✓ tap #submit
    ✓ expect document.querySelector('#welcome h2').textContent equals "Welcome, Ana"
    ✓ expect document.querySelector('#form').checkVisibility() equals false
    ✓ screenshot welcome → brokit-out/checks/signup-phone/welcome.png (786×1704) [baseline: new]  158 ms
✓ checks/signup  17 steps, 0.6 s, 2 baselines written
    ✓ click #name
    ✓ type "Ana Pop"
    ✓ click #email
    ✓ type "ana@example"
    ✓ key Enter
    ✓ expect document.querySelector('#email-error').textContent equals "Enter a valid email, like ana@example.com"
    ✓ expect document.activeElement.id equals "email"
    ✓ screenshot error → brokit-out/checks/signup/error.png (380×453) [baseline: new]  161 ms
    ✓ click #email
    ✓ type "ana@example.com"
    ✓ click #password
    ✓ type "correct horse battery"
    ✓ click #submit
    ✓ expect document.querySelector('#welcome h2').textContent equals "Welcome, Ana"
    ✓ expect document.querySelector('#welcome p').textContent matches /^We sent a link to ana@example\.com\.$/
    ✓ expect document.querySelector('#form').checkVisibility() equals false
    ✓ screenshot welcome → brokit-out/checks/signup/welcome.png (800×600) [baseline: new]  128 ms
2 scenarios passed in 1.7 s

brokit serves this folder, so "url": "/index.html" in a check opens index.html. The first run writes the baselines in checks/baselines/; the runs after it compare against them. 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 sign-up card with the email field outlined in red, saying to enter a valid email A card with a green check mark: Welcome, Ana The sign-up form filled in on a phone screen

The baselines the checks wrote: a wrong email, the welcome card, and the phone check.

The page#

index.html is a form with a name, an email and a password. app.js checks the email when the form is sent: if it's wrong, it shows an error and puts the cursor back in the field; if it's right, it hides the form and says welcome.

The laptop check#

The check, checks/signup.check.json:

checks/signup.check.json
{
  "note": "The sign-up form on a laptop: a wrong email first, then the right one.",
  "url": "/index.html",
  "viewport": { "size": "800x600" },
  "ready": "document.readyState === 'complete'",
  "steps": [
    { "click": { "selector": "#name" } },
    { "type": "Ana Pop" },
    { "click": { "selector": "#email" } },
    { "type": "ana@example" },
    { "key": "Enter", "note": "Enter in a field submits the form" },
    {
      "expect": "document.querySelector('#email-error').textContent",
      "equals": "Enter a valid email, like ana@example.com"
    },
    {
      "expect": "document.activeElement.id",
      "equals": "email",
      "note": "the page puts the cursor back in the field to fix"
    },
    { "screenshot": "error", "selector": ".card", "baseline": true },
    { "click": { "selector": "#email" }, "count": 3, "note": "a triple click selects all the text in the field" },
    { "type": "ana@example.com", "note": "replaces the selection" },
    { "click": { "selector": "#password" } },
    { "type": "correct horse battery" },
    { "click": { "selector": "#submit" } },
    { "expect": "document.querySelector('#welcome h2').textContent", "equals": "Welcome, Ana" },
    { "expect": "document.querySelector('#welcome p').textContent", "matches": "^We sent a link to ana@example\\.com\\.$" },
    { "expect": "document.querySelector('#form').checkVisibility()", "equals": false, "note": "the form is gone, not just covered" },
    { "screenshot": "welcome", "baseline": true }
  ]
}

The phone check#

The check, checks/signup-phone.check.json:

checks/signup-phone.check.json
{
  "note": "The same form on a phone: a touch screen, so taps instead of clicks.",
  "url": "/index.html",
  "viewport": { "size": "393x852", "dpr": 2, "mobile": true },
  "ready": "document.readyState === 'complete'",
  "steps": [
    { "expect": "document.documentElement.scrollWidth <= innerWidth", "note": "nothing wider than the phone" },
    { "tap": { "selector": "#name" } },
    { "type": "Ana Pop" },
    { "tap": { "selector": "#email" } },
    { "type": "ana@example.com" },
    { "tap": { "selector": "#password" } },
    { "type": "correct horse battery" },
    { "screenshot": "filled", "baseline": true },
    { "tap": { "selector": "#submit" } },
    { "expect": "document.querySelector('#welcome h2').textContent", "equals": "Welcome, Ana" },
    { "expect": "document.querySelector('#form').checkVisibility()", "equals": false },
    { "screenshot": "welcome", "baseline": true }
  ]
}

Learn more#

Edit this page on GitHub