Everything here is called inside display(...), the matcher that takes any number of expectations and checks them all:

render(view(form)) must display(
  title("x.title"),
  heading("x.heading"),
  submitButton()
)

render parses the view’s output into a Page. The expectations are plain values, so they can be kept in a val, put in a list, or bundled with expectations(...) and reused across specs.

Message keys and exact words

Every expectation that takes text takes a message key by default, because that is what template content is. The key is resolved in the page’s language, so the same spec holds in English and Welsh. Arguments follow the key:

title("livesInUk.title", "Ada")            // messages("livesInUk.title", "Ada")

Where you mean exact words, use the …Text form, or wrap the words:

titleText("What is your name?")
heading(literal("What is your name?"))

Three more Expected values exist for the expectations that take one: messageKey("x.lives", name), for a message key with its arguments, anyText, for “there is a heading, its wording is asserted elsewhere”, and matching("Sign\\s+up".r), for a regular expression.

Page text is normalised before comparison: whitespace collapsed, non-breaking spaces made plain, soft hyphens dropped, curly quotes straightened. The expected side is normalised the same way, so a curly apostrophe in a message file never fails against a straight one on the page. When you compare page text yourself, normalised(...) and messageText(key) do the same to your side.

Page framing

The parts of a page the layout is responsible for.

title("x.title")            // the browser title, ignoring " - Service - GOV.UK" and an "Error:" prefix
exactTitle("Full - Service - GOV.UK")
heading("x.heading")        // the single <h1>, ignoring a caption rendered inside it
caption("x.caption")        // the caption above or inside the h1
subheading("x.section")     // an h2 with this text
headingAtLevel(3, literal("Details"))
serviceName()               // "service.name" by default
backLink                    // a GOV.UK back link is present
backLink.to("/where")       // ... and points here
noBackLink                  // a page a citizen must not reverse out of
languageToggle              // a language switcher, however it is rendered
phaseBanner
timeoutDialog               // the HMRC timeout dialog is wired up
signOutLink

Content

content("x.p1")                          // somewhere in the visible text
noContent("x.gone")                      // nowhere in the visible text
paragraph("x.p1")                        // inside a paragraph
warning("x.warning")                     // a GOV.UK warning callout
insetText("x.inset")
notificationBanner("x.banner")
panelTitle("x.panel.title")
panelBody("x.panel.body")
detailsSummary("x.details")              // the visible summary of a details block
bullets("x.b1", "x.b2")                  // exactly these items, in order
numberedItems("x.step1", "x.step2")

Links can be found by text or by id, and pinned to a destination:

link("x.guidance").to("/guidance")
linkText("Read the guidance").to("/guidance")
linkWithId("guidance-link").to("/guidance").saying("x.guidance")

Check-your-answers pages have a summary list; assert the whole list or one row:

summaryList("x.name" -> "Ada", "x.dob" -> "27 March 1993")   // exactly these rows, in order
summaryList("x.name" -> "Ada", messageKey("x.lives", "Ada") -> "Yes")   // a key with arguments
summaryRow("x.name").withValue("Ada").withChangeLinkTo("/change/name")
summaryRow("x.name").withActions("site.change")

Tables:

tableHeaders("x.col1", "x.col2")         // the header cells, in order
columnHeaders("x.col1", "x.col2")        // less the header each row has of its own
tableRow("Ada", "1815")                  // a row whose cells read exactly like this

Structure

For anything the named expectations do not cover, fall back to ids and selectors:

element("submit")                        // exactly one element with this id
noElement("warning")
elementWithText("submit", "site.continue")
elementHasClass("tag", "govuk-tag--green")
cssSelector(".govuk-panel")              // at least one match
noCssSelector(".govuk-error-summary")
elementCount(".govuk-summary-list__row", 4)
appearsBefore(".govuk-error-summary", "form")   // document order

appearsBefore is worth knowing: reading order is not cosmetic, and an error summary announced after the form it describes is announced too late.

Finding things the way a screen reader does

Borrowed from Testing Library: if a control cannot be found by its role and the name it announces, a screen reader cannot find it either, so the query is itself an accessibility check.

role("button").named("site.continue")
role("textbox").namedText("Email address")
role("heading").namedMatching("Sign\\s+up".r)
role("radio").occurring(2)

Roles are resolved the way HTML-AAM defines them, implicit or explicit, and an element with an explicit role is matched only by that role, so <a role="button"> is a button and not a link. One limit: an input’s list is not followed, so one with suggestions keeps its usual role rather than combobox. The accessible name comes from aria-labelledby, aria-label, an associated <label>, a <legend>, alt, the value of a button, submit or reset input, the element’s own text (or the alt of an image standing in for it), and finally title — in that order. A <select>, <textarea> or <input> is never named by its own content, and neither is an image hidden from assistive technology. This is a working subset of the accessible name computation: one hop of aria-labelledby, and nothing that depends on CSS or JavaScript.

Forms

Every control expectation finds its control by name or id, then checks what the builders ask for.

textInput("email")
  .labelled("x.email")                   // label text, from a key; .labelledText for words
  .hinted("x.email.hint")                // hint text, and that the field announces it
  .withValue("ada@example.com")
  .withAutocomplete("email")
  .ofType("email")

textArea("notes").labelled("x.notes")

dropdown("country").labelled("x.country").withOptionValues("GB", "FR").withOptionCount(3)

radioGroup("value")                      // "value" is the default name, as Play names it
  .legendIs("x.legend")
  .hinted("x.hint")
  .withOptions("yes" -> "site.yes", "no" -> "site.no")   // exactly these, labels checked
  .selectedIs("yes")                     // or .nothingSelected

checkboxGroup("colours").withOptionValues("red", "blue").selectedIs("red", "blue")
checkboxGroup("terms").containingOption("agree", "x.agree")   // among possibly others

dateInput("dob").legendIs("x.dob").hinted("x.dob.hint")       // day, month, year
dateInput("dob").withParts("month", "year")

fileUpload("evidence")
hiddenInput("csrfToken", token)

submitButton()                           // says "site.continue" by default
submitButton("site.saveAndContinue")
submitButtonText("Continue")
hasSubmitButton                          // present, whatever it says
button("save-for-later", "x.saveForLater")

formPostsTo("/register/name")
formGetsFrom("/search")

A control that is legitimately labelled by a legend rather than a label, as the single field of a fieldset sometimes is, says so with labelledByLegend.

Labels are checked the way an assistive technology reads them: a <label> whose for matches the control’s id. Hints are checked for their words and for being announced, that is, referenced from the control’s aria-describedby, or from the fieldset’s for a group. The GOV.UK components wire both up when you pass hint; the checks catch a hand-written template that does not.

Control state, form values and reading order

Borrowed from jest-dom, keeping what means something for server-rendered HTML:

formValues("email" -> "ada@example.com", "country" -> "GB", "contact" -> "email")

disabled("locked")                       // on itself, or through an enclosing fieldset (not in its first legend)
enabled("email")
required("email")                        // required attribute or aria-required
invalid("email")                         // aria-invalid
describedAs("email", "x.email.hint")     // what is read after the control's name

Errors

noErrors                                 // no summary, no inline errors, no "Error:" in the title
errorTitlePrefix                         // the title carries the error prefix
errorSummaryTitle()                      // "error.summary.title" by default
errorSummary("email" -> "x.error.email.required")           // exactly these entries, in order
errorSummaryContaining("email", "x.error.email.required")   // this one, whatever else
fieldError("email", "x.error.email.required")               // the inline message against the field

errorSummary also checks that every entry links to an element that exists, because a link to #firstName when the input is id="value" leaves a keyboard user stranded.

One match, or a failure

Most expectations that name a single element fail when the page has more than one match, as well as when they find none. fileUpload and hiddenInput do not, as two file inputs with one name are a way to take several files, and unambiguous-field-names reports two hidden inputs with one name:

x input(email) — 2 elements matched, so this assertion is ambiguous
      expected  "exactly one match"
      actual    "<input#email[name=email]> | <input#email2[name=email]>"
      hint      name the one you mean, or assert on the group with cssSelector and elementCount

This follows Testing Library’s getBy: quietly taking the first of several is how a test ends up asserting against something other than the thing it names. Expectations that are plural by nature, such as radioGroup, bullets, link and tableRow, are unaffected.

Where a page really has two of something, within asks of the one you mean:

within("#results")(columnHeaders("x.name", "x.born"), tableRow("Ada", "1815"))
within("form[action='/save']")(formPostsTo("/save"), submitButton())

What it names has to be one element. What an element says of another by id, such as for or aria-describedby, still reaches the whole page, and so do the standards. What is asserted inside counts for the page’s coverage.

House rules

A service usually has a few expectations every page must meet. Bundle them once:

trait ViewSpecBase extends AnyWordSpec with Matchers with TwirlSpec with AllChecks {
  val standardFurniture: Expectation = expectations(serviceName(), languageToggle, signOutLink)
}

render(view(form)) must display(standardFurniture, title("x.title"), heading("x.heading"))

An expectation can also be softened with .asWarning, so it is reported without failing, or made conditional with .when(page => ...).

Raw Jsoup, one call away

The page model is there when the DSL is not enough:

val page = render(view(form))
page.doc                                 // the Jsoup Document
page.css(".govuk-summary-list__row").size
page.byId("value").attr("aria-describedby")
page.summaryRows                         // (key, value, action texts)
page.fieldErrors                         // field -> inline message
page.errorSummaryLinks                   // (target id, text)
page.formValues                          // name -> value, the last one for a repeated name
page.formSubmission                      // every (name, value) a browser would send, in order
page.accessibleName(page.byId("submit")(0))
page.outline                             // the skeleton, printable, and snapshotted below

Assertions made through the page model count towards coverage the same way DSL expectations do; see Coverage and entry points.

Snapshotting the outline

matchOutlineSnapshot holds a page to the outline saved in a file, so a change to the page’s structure shows as the lines that changed:

render(view(form)) must matchOutlineSnapshot(Path.of("test/resources/outlines/name.txt"))

A missing file fails with the page’s outline. Run with TWIRLSPEC_UPDATE_SNAPSHOTS=true to write it, or to write it again after changing the page on purpose, and commit what it writes; a spec base can override updateOutlineSnapshots instead. Never set it in CI, where it would pass whatever a page had become. A relative path is read from where the tests run, and a failure names the file in full.

A snapshot asserts nothing for coverage, so assertEverything still asks for what the page shows to be asserted. The outline’s format changes only in a minor version, which says so in the changelog.

Checking outside a matcher

checkPage(page, Seq(...)) runs expectations and returns a CheckReport with passed, errors, warnings and message, for a spec that wants to report rather than fail, or to assert on the result itself.