|
| 1 | +--- |
| 2 | +title: Cypress.ensure |
| 3 | +--- |
| 4 | + |
| 5 | +`Cypress.ensure` is a collection of helper methods for making assertions. They |
| 6 | +are mostly useful when writing [custom queries](/api/cypress-api/custom-queries) |
| 7 | +or [custom commands](/api/cypress-api/custom-commands). |
| 8 | + |
| 9 | +Most functions on `Cypress.ensure` accept a |
| 10 | +[`subject`](/guides/core-concepts/introduction-to-cypress#Subject-Management) |
| 11 | +argument, check an assertion, and throw an error if the assertion fails. These |
| 12 | +functions have no return value. |
| 13 | + |
| 14 | +## Syntax |
| 15 | + |
| 16 | +```javascript |
| 17 | +// Type of argument |
| 18 | +Cypress.ensure.isType(subject, type, commandName, cy) |
| 19 | +Cypress.ensure.isElement(subject, commandName, cy) |
| 20 | +Cypress.ensure.isWindow(subject, commandName, cy) |
| 21 | +Cypress.ensure.isDocument(subject, commandName, cy) |
| 22 | + |
| 23 | +// State of DOM element |
| 24 | +Cypress.ensure.isAttached(subject, commandName, cy) |
| 25 | +Cypress.ensure.isNotDisabled(subject, commandName) |
| 26 | +Cypress.ensure.isNotHiddenByAncestors(subject, commandName) |
| 27 | +Cypress.ensure.isNotReadonly(subject, commandName) |
| 28 | +Cypress.ensure.isScrollable(subject, commandName) |
| 29 | +Cypress.ensure.isStrictlyVisible(subject, commandName) |
| 30 | +Cypress.ensure.isVisible(subject, commandName) |
| 31 | +``` |
| 32 | + |
| 33 | +:::caution |
| 34 | + |
| 35 | +Many of these functions accept an optional `onFail` argument. This is a legacy |
| 36 | +feature used to customize the thrown error, and may be removed in a future |
| 37 | +release; we recommend against relying on it. If you need more control over the |
| 38 | +error thrown, write your own `ensure` function instead. |
| 39 | + |
| 40 | +::: |
| 41 | + |
| 42 | +### Usage |
| 43 | + |
| 44 | +**<Icon name="check-circle" color="green" /> Correct Usage** |
| 45 | + |
| 46 | +```javascript |
| 47 | +Cypress.Commands.addQuery('getChildById', function (id) { |
| 48 | + return (subject) => { |
| 49 | + // Verify that the subject is an element, document, or window object |
| 50 | + Cypress.ensure.isType( |
| 51 | + subject, |
| 52 | + ['element', 'document', 'window'], |
| 53 | + 'getChildById', |
| 54 | + cy |
| 55 | + ) |
| 56 | + |
| 57 | + return $$(`#${id}`, subject) |
| 58 | + } |
| 59 | +}) |
| 60 | + |
| 61 | +const queryName = 'verifyElementActionable' |
| 62 | + |
| 63 | +Cypress.Commands.addQuery(queryName, function (...args) { |
| 64 | + return (subject) => { |
| 65 | + // Verify that the subject fulfills a variety of conditions |
| 66 | + Cypress.ensure.isElement(subject, queryName, cy) |
| 67 | + Cypress.ensure.isVisible(subject, queryName, cy) |
| 68 | + Cypress.ensure.isNotDisabled(subject, queryName, cy) |
| 69 | + Cypress.ensure.isNotReadonly(subject, queryName, cy) |
| 70 | + |
| 71 | + return subject |
| 72 | + } |
| 73 | +}) |
| 74 | +``` |
| 75 | + |
| 76 | +## See also |
| 77 | + |
| 78 | +- ["Custom Queries"](/api/cypress-api/custom-queries) contains more information |
| 79 | + about writing custom queries, which is the main use-case for the `ensure` |
| 80 | + functions. |
0 commit comments