Text assertion (textIs, textContains, ...)
Checks on an element's label (its displayed text).
The target is always "the element grabbed last" — selectors are not passed to these
commands. First grab the element with select (or exist / tap / etc.), then verify it.
The following three forms mean exactly the same thing, with the same recorded steps and the
same number of device round trips:
select("#msg").textIs("Done") // chain onto the return value
select("#msg"); lastElement.textIs("Done") // explicit lastElement
select("#msg"); textIs("Done") // implicit (the last grabbed element)
Arguments are (expected, requireVisible:, strict:, waitSeconds:) for the positive forms and (expected, strict:, waitSeconds:) for the negative forms, and (strict:, waitSeconds:) for the empty forms (textMatchesDateFormat takes no strict: — see the table below). There is
no form that takes a selector — textIs("#msg", "Done") does not compile.
In the implicit form (e.g. textIs("…") without lastElement), a string that is # followed only by letters/digits, or that contains || / >>, is treated as a mistyped selector and fails before running. To expect such a string, use the chained form (select("#tag").textIs("#swift")).
Functions
| function | negation | comparison |
|---|---|---|
select(selector).textIs(expected, requireVisible:, strict:, waitSeconds:) |
textIsNot(expected, strict:, waitSeconds:) |
exact match |
textContains(expected, requireVisible:, strict:, waitSeconds:) |
textContainsNot(expected, strict:, waitSeconds:) |
substring match |
textStartsWith(expected, requireVisible:, strict:, waitSeconds:) |
textStartsWithNot(expected, strict:, waitSeconds:) |
prefix match |
textEndsWith(expected, requireVisible:, strict:, waitSeconds:) |
textEndsWithNot(expected, strict:, waitSeconds:) |
suffix match |
textMatches(pattern, requireVisible:, strict:, waitSeconds:) |
textMatchesNot(pattern, strict:, waitSeconds:) |
regular expression (substring; use ^…$ for a full match) |
textMatchesDateFormat(format, requireVisible:, waitSeconds:) |
— | DateFormatter format string, e.g. "yyyy/MM/dd" |
textIsNotEmpty(strict:, waitSeconds:) |
textIsEmpty(strict:, waitSeconds:) |
non-empty / empty |
All of the above are chainable on the return value of exist / select, and each also has an
implicit form (a free function that takes no selector; the same arguments as the chained form) that acts on the last grabbed element.
Example
select("#msg").textIs("Done")
exist("#total")
.textStartsWith("Total")
.textEndsWith("USD")
Notes
- Four rules for comparing text.
- Text is compared as it looks on the screen (characters you cannot see are ignored).
- Half-width spaces, full-width spaces and line breaks are told apart (a selector that finds an element does not tell them apart).
- An empty expected value cannot be used with contains / starts-with / ends-with (it fails as an error).
- A regular expression is used exactly as written, and matches the text as it looks even when the screen text contains invisible characters.
- An empty regular expression fails before comparing in
textMatches/valueMatches/thisMatchesand theirNotforms (empty, only spaces, or only invisible characters would match any text). - An empty expected value fails before comparing in
textContains/textStartsWith/textEndsWithand theirNotforms (and thevalue…/this…ones): an empty string, only spaces, or only invisible characters (such as a zero-width space) would hold for any text. Characters you cannot see are removed from both the screen text and the expected value before comparing. - Comparison is "same if it looks the same." Invisible characters (zero-width, bidi control,
soft hyphen, etc.) are ignored, but visually different strings are treated as different —
a half-width and a full-width space (U+3000) are different, and leading/trailing/repeated
spaces are preserved. The one exception is the no-break space (NBSP, U+00A0), which is treated
the same as a half-width space. Pass
strict: trueto disable this normalization (including the NBSP handling) (textIs("Done", strict: true)). A mismatch failure message says whether the normalized or strict comparison would have matched. - The element is assumed to already exist; these commands wait up to
waitSecondsfor the value to change, so they can be used to wait for a value update. - Negative forms and the empty checks (
textIsNot,textIsEmpty, etc.) do not consider visibility — "not visible" cannot be confirmed from a screen match. - There is no
scroll:on any of these. They verify a static screen; scroll the target into view first withselect(selector, scroll: .down)or similar. - When chained onto
exist(…)/select(…)/lastElement, the check first evaluates against the value already grabbed. If that already satisfies the assertion, the step is recorded but no device round trip happens (the message shows(from the grabbed value)). Otherwise it polls the device up towaitSeconds.