|
| 1 | +--- |
| 2 | +outline: [2, 3] |
| 3 | +prev: |
| 4 | + text: "Actions" |
| 5 | + link: "/en/v0/api/actions" |
| 6 | +next: |
| 7 | + text: "API Reference" |
| 8 | + link: "/en/v0/api/" |
| 9 | +description: "Overview of the ready-to-use assertions provided by Assertions in @duplojs/playwright." |
| 10 | +--- |
| 11 | + |
| 12 | +# Assertions |
| 13 | + |
| 14 | +`Assertions` groups the ready-to-use checks applied to a component and one of its element keys. |
| 15 | + |
| 16 | +In practice, this namespace makes it possible to write expectations that are more readable and more consistent than a sequence of Playwright calls scattered across the tests. |
| 17 | + |
| 18 | +## Simple example |
| 19 | + |
| 20 | +```ts twoslash |
| 21 | +// @version: 0 |
| 22 | +<!--@include: @/examples/v0/api/assertions/main.ts--> |
| 23 | +``` |
| 24 | +::: tip What is happening here |
| 25 | +- the component exposes named elements |
| 26 | +- `Assertions.toHaveValue(...)` and `Assertions.toBeVisible(...)` target these elements directly |
| 27 | +- the test stays focused on business verification rather than technical details |
| 28 | +::: |
| 29 | + |
| 30 | +## What is it for? |
| 31 | + |
| 32 | +`Assertions` is mainly used to: |
| 33 | + |
| 34 | +- share frequent expectations |
| 35 | +- keep a consistent vocabulary in tests |
| 36 | +- avoid repeating the same assertion sequences |
| 37 | +- centralize enriched checks such as prior visibility |
| 38 | + |
| 39 | +## toBeVisible |
| 40 | + |
| 41 | +```ts |
| 42 | +Assertions.toBeVisible(component, elementKey) |
| 43 | +``` |
| 44 | + |
| 45 | +Checks that an element is visible. |
| 46 | + |
| 47 | +## toHaveText |
| 48 | + |
| 49 | +```ts |
| 50 | +Assertions.toHaveText(component, elementKey, text) |
| 51 | +``` |
| 52 | + |
| 53 | +Checks that an element has exactly the expected text. |
| 54 | + |
| 55 | +## toContainText |
| 56 | + |
| 57 | +```ts |
| 58 | +Assertions.toContainText(component, elementKey, text) |
| 59 | +``` |
| 60 | + |
| 61 | +Checks that an element contains the expected text. |
| 62 | + |
| 63 | +## toHaveNoText |
| 64 | + |
| 65 | +```ts |
| 66 | +Assertions.toHaveNoText(component, elementKey) |
| 67 | +``` |
| 68 | + |
| 69 | +Checks that an element has no text. |
| 70 | + |
| 71 | +## toBeHidden |
| 72 | + |
| 73 | +```ts |
| 74 | +Assertions.toBeHidden(component, elementKey) |
| 75 | +``` |
| 76 | + |
| 77 | +Checks that an element is hidden. |
| 78 | + |
| 79 | +## toHaveQuantity |
| 80 | + |
| 81 | +```ts |
| 82 | +Assertions.toHaveQuantity(component, elementKey, { |
| 83 | + quantity, |
| 84 | + operator?, |
| 85 | +}) |
| 86 | +``` |
| 87 | + |
| 88 | +Checks the number of elements matching a locator. |
| 89 | + |
| 90 | +## toBeEnabled |
| 91 | + |
| 92 | +```ts |
| 93 | +Assertions.toBeEnabled(component, elementKey) |
| 94 | +``` |
| 95 | + |
| 96 | +Checks that an element is enabled. |
| 97 | + |
| 98 | +## toBeChecked |
| 99 | + |
| 100 | +```ts |
| 101 | +Assertions.toBeChecked(component, elementKey) |
| 102 | +``` |
| 103 | + |
| 104 | +Checks that an element is checked. |
| 105 | + |
| 106 | +## toBeDisabled |
| 107 | + |
| 108 | +```ts |
| 109 | +Assertions.toBeDisabled(component, elementKey) |
| 110 | +``` |
| 111 | + |
| 112 | +Checks that an element is disabled. |
| 113 | + |
| 114 | +## toHaveAttribute |
| 115 | + |
| 116 | +```ts |
| 117 | +Assertions.toHaveAttribute(component, elementKey, name, value?) |
| 118 | +``` |
| 119 | +
|
| 120 | +Checks that an element has an expected attribute. |
| 121 | +
|
| 122 | +## toHaveClass |
| 123 | +
|
| 124 | +```ts |
| 125 | +Assertions.toHaveClass(component, elementKey, value) |
| 126 | +``` |
| 127 | +
|
| 128 | +Checks that an element has the expected class. |
| 129 | +
|
| 130 | +## toHaveValue |
| 131 | +
|
| 132 | +```ts |
| 133 | +Assertions.toHaveValue(component, elementKey, value) |
| 134 | +``` |
| 135 | +
|
| 136 | +Checks that an element has the expected value. |
| 137 | +
|
| 138 | +## toBeBusy |
| 139 | +
|
| 140 | +```ts |
| 141 | +Assertions.toBeBusy(component, elementKey) |
| 142 | +``` |
| 143 | +
|
| 144 | +Checks that an element has `aria-busy="true"`. |
| 145 | +
|
| 146 | +## toBeNotBusy |
| 147 | +
|
| 148 | +```ts |
| 149 | +Assertions.toBeNotBusy(component, elementKey) |
| 150 | +``` |
| 151 | +
|
| 152 | +Checks that an element has `aria-busy="false"`. |
| 153 | +
|
| 154 | +## withStep |
| 155 | +
|
| 156 | +```ts |
| 157 | +Assertions.withStep(label).assertion(component, elementKey, ...args) |
| 158 | +``` |
| 159 | +
|
| 160 | +Returns the same assertions, but grouped under a custom `test.step(...)`. |
| 161 | +The useful call is then chained on the returned wrapper. |
| 162 | +
|
| 163 | +## See also |
| 164 | +
|
| 165 | +- [`Actions`](/en/v0/api/actions) - for ready-to-use interactions on components. |
| 166 | +- [`Component`](/en/v0/api/component) - to define the elements the assertions apply to. |
| 167 | +- [`Component Interaction`](/en/v0/api/componentInteraction) - to create custom checks if the provided assertions are not enough. |
0 commit comments