---
id: api/cypress-api/dom
title: Cypress.dom
description: >-
  Cypress.dom is a set of helper functions that check DOM elements the same way
  Cypress commands do, such as whether an element is visible, detached, or
  focusable.
section: api
source_path: docs/api/cypress-api/dom.mdx
version: 1934fa485ad0afcbbd3bdb674d5c5f1f991b55cf
updated_at: '2026-11-14T05:15:51.878Z'
---
# Cypress.dom

`Cypress.dom` is a set of helper functions that answer questions about the DOM the same way Cypress does. Is this element visible? Is it still attached to the page? Can it receive focus? Cypress's own commands and assertions rely on these checks, so when you call them yourself, your answer matches the one Cypress uses. [`.should('be.visible')`](/llm/markdown/app/references/assertions.md#Visibility), for example, explains a failure with the message `getReasonIsHidden()` returns.

Each function takes a value, checks it, and returns the result right away. That makes `Cypress.dom` useful anywhere you already hold an element and need a `true` or `false` to act on:

*   Inspect an element's state while you debug a test.
*   Assert on a condition no built-in assertion covers, such as whether focus stays inside a dialog.
*   Make a [custom query](/llm/markdown/api/cypress-api/custom-queries.md) or [custom command](/llm/markdown/api/cypress-api/custom-commands.md) handle different kinds of subjects.

## Syntax

```
Cypress.dom.getReasonIsHidden(element)
Cypress.dom.isAttached(element)
Cypress.dom.isDescendent(element, descendent)
Cypress.dom.isDetached(element)
Cypress.dom.isDocument(obj)
Cypress.dom.isDom(obj)
Cypress.dom.isElement(obj)
Cypress.dom.isFocusable(element)
Cypress.dom.isFocused(element)
Cypress.dom.isHidden(element)
Cypress.dom.isInputType(element, type)
Cypress.dom.isJquery(obj)
Cypress.dom.isScrollable(element)
Cypress.dom.isVisible(element)
Cypress.dom.isWindow(obj)
Cypress.dom.stringify(element)
Cypress.dom.stringify(element, form)
Cypress.dom.unwrap(obj)
Cypress.dom.wrap(obj)
```

### Usage

**Correct Usage**

```
cy.get('#email').then(($input) => {
  Cypress.dom.isVisible($input) // true
  Cypress.dom.isFocused($input) // false
})
```

**Incorrect Usage**

```
// Cypress.dom functions are not commands, so they don't chain off cy
cy.get('#email').isVisible()

// They check an element, not a selector, and this call throws
Cypress.dom.isVisible('#email')
```

### Arguments

The functions take the value to check as their first argument. Every function that checks an element accepts either a DOM element or a jQuery object; the Accepts column of the [Functions](#Functions) table lists what each one takes. Three functions take a second argument: [`isDescendent()`](#isDescendent) takes a `descendent`, [`isInputType()`](#isInputType) takes a `type`, and [`stringify()`](#stringify) takes a `form`.

**element** **_(Element, jQuery)_**

A DOM element, or a jQuery object that wraps one or more elements. Element queries such as [`cy.get()`](/llm/markdown/api/commands/get.md) yield a jQuery object, so in a [`.then()`](/llm/markdown/api/commands/then.md) callback, pass the subject as it is.

**obj** **_(any)_**

Any value. The type checks, such as `isElement()` and `isWindow()`, accept anything and return `false` for a value that isn't the type they check for.

### Return value

Every function except `getReasonIsHidden()`, `stringify()`, `unwrap()`, and `wrap()` returns a boolean. None of them yield a subject, so to use a result in a Cypress chain, call the function inside a callback such as [`.then()`](/llm/markdown/api/commands/then.md) or [`.should()`](/llm/markdown/api/commands/should.md).

## Functions

| Function | Returns… | Accepts |
| --- | --- | --- |
| [`getReasonIsHidden()`](#getReasonIsHidden) | why Cypress considers an element hidden | element, jQuery |
| [`isAttached()`](#isAttached) | whether an element is attached to the page | element, jQuery |
| [`isDescendent()`](#isDescendent) | whether one element is inside another | element, jQuery |
| [`isDetached()`](#isDetached) | whether an element is no longer attached to the page | element, jQuery |
| [`isDocument()`](#isDocument) | whether a value is a `document` | any value |
| [`isDom()`](#isDom) | whether a value is an element, `document`, or `window` | any value |
| [`isElement()`](#isElement) | whether a value is a DOM element | any value |
| [`isFocusable()`](#isFocusable) | whether an element can receive focus | element, jQuery |
| [`isFocused()`](#isFocused) | whether an element has focus | element, jQuery |
| [`isHidden()`](#isHidden) | whether Cypress considers an element hidden | element, jQuery |
| [`isInputType()`](#isInputType) | whether an `<input>` or `<button>` has a given `type` | element, jQuery |
| [`isJquery()`](#isJquery) | whether a value is a jQuery object | any value |
| [`isScrollable()`](#isScrollable) | whether an element or the window can scroll | element, jQuery, window |
| [`isVisible()`](#isVisible) | whether Cypress considers an element visible | element, jQuery |
| [`isWindow()`](#isWindow) | whether a value is a `window` | any value |
| [`stringify()`](#stringify) | a short HTML string that describes an element | element, jQuery, window, document |
| [`unwrap()`](#unwrap) | the DOM elements inside a jQuery object, as an array | any value |
| [`wrap()`](#wrap) | a jQuery object that wraps the value | element, HTML |

### getReasonIsHidden()

```
Cypress.dom.getReasonIsHidden(element)
```

Returns a sentence that explains why Cypress considers an element hidden. It is the reason that follows a failed `.should('be.visible')` assertion, so you can read it without failing the test.

*   The reason follows your [`visibilityStrategy`](/llm/markdown/app/core-concepts/interacting-with-elements.md#Visibility-Strategy). Under the default `'modern'` strategy, it names the computed `display`, `visibility`, `opacity`, and `content-visibility` values, or the element's size when it has no width or height.
*   For a visible element, it returns a sentence that doesn't name a cause, such as ``This element `<button#place-order>` is not visible.`` Check [`isHidden()`](#isHidden) first.

For a `<button id="place-order">` with `display: none`, it returns:

```
This element `<button#place-order>` is not visible per `Element.checkVisibility()`. Computed: `display: none`, `visibility: visible`, `opacity: 1`, `content-visibility: visible`.
```

Used in: [Inspect an element's state while debugging a test](#Inspect-an-elements-state-while-debugging-a-test)

### isAttached()

```
Cypress.dom.isAttached(element)
```

Returns `true` when the element is attached to a page with an active window. This is the check behind Cypress's [detached element](/llm/markdown/app/core-concepts/interacting-with-elements.md#Detached) errors.

*   For a jQuery object, it returns `true` only when every element in it is attached, and `false` for an empty jQuery object.
*   A `window` or `document` counts as attached.

Used in: [Inspect an element's state while debugging a test](#Inspect-an-elements-state-while-debugging-a-test)

### isDescendent()

```
Cypress.dom.isDescendent(element, descendent)
```

Returns `true` when `descendent` is inside `element`, at any depth.

**descendent** **_(Element, jQuery)_**

The element to look for inside `element`.

*   Each argument can be a DOM element or a jQuery object. For a jQuery object, it compares the first element in it.
*   It returns `false` when `descendent` is missing.
*   An element counts as its own descendent, so passing the same element twice returns `true`.

Used in: [Check that keyboard focus stays inside a dialog](#Check-that-keyboard-focus-stays-inside-a-dialog)

### isDetached()

```
Cypress.dom.isDetached(element)
```

Returns `true` when the element is no longer attached to the page, for example because the app re-rendered it. It is the opposite of [`isAttached()`](#isAttached), so an empty jQuery object counts as detached.

Used in: [Check whether the app re-rendered an element](#Check-whether-the-app-re-rendered-an-element)

### isDocument()

```
Cypress.dom.isDocument(obj)
```

Returns `true` when the value is a `document`, such as the one [`cy.document()`](/llm/markdown/api/commands/document.md) yields, or a jQuery object that wraps one.

*   A document fragment, such as an element's `shadowRoot`, also returns `true`.

Used in: [Log any subject in a custom command](#Log-any-subject-in-a-custom-command)

### isDom()

```
Cypress.dom.isDom(obj)
```

Returns `true` when the value is a DOM element, a `document`, or a `window`, or a jQuery object whose first item is one of them. A text node or an HTML string returns `false`.

Used in: [Log any subject in a custom command](#Log-any-subject-in-a-custom-command)

### isElement()

```
Cypress.dom.isElement(obj)
```

Returns `true` when the value is a DOM element, or a jQuery object whose first item is a DOM element. A `window`, a `document`, a text node, and an empty jQuery object return `false`.

Used in: [Log any subject in a custom command](#Log-any-subject-in-a-custom-command)

### isFocusable()

```
Cypress.dom.isFocusable(element)
```

Returns `true` when the element is a kind that can receive focus: a link or `<area>` with an `href`, an enabled `<input>`, `<select>`, `<textarea>`, or `<button>`, an `<iframe>`, or any element with a `tabindex` or `contenteditable` attribute.

*   It checks the element's type and attributes, not whether it is visible, so an `<input>` inside a `display: none` container still returns `true`.
*   It counts `tabindex="-1"`, which can receive focus from a script but not from the Tab key.

Used in: [Inspect an element's state while debugging a test](#Inspect-an-elements-state-while-debugging-a-test), [Check that keyboard focus stays inside a dialog](#Check-that-keyboard-focus-stays-inside-a-dialog)

### isFocused()

```
Cypress.dom.isFocused(element)
```

Returns `true` when the element is the document's active element, including inside a shadow root.

*   For a jQuery object, it checks the first element.
*   It returns `false` for `<body>` when nothing else has focus.
*   In a test, `.should('have.focus')` or [`cy.focused()`](/llm/markdown/api/commands/focused.md) asserts focus and retries. Use `isFocused()` when you need the answer in your own logic.

Used in: [Inspect an element's state while debugging a test](#Inspect-an-elements-state-while-debugging-a-test)

### isHidden()

```
Cypress.dom.isHidden(element)
```

Returns `true` when Cypress considers the element hidden, following the [visibility rules](/llm/markdown/app/core-concepts/interacting-with-elements.md#Visibility) of your [`visibilityStrategy`](/llm/markdown/app/core-concepts/interacting-with-elements.md#Visibility-Strategy). It is the opposite of [`isVisible()`](#isVisible); see that entry for the behavior both share.

When the value isn't a DOM element or a jQuery object, it throws:

```
`Cypress.dom.isHidden()` failed because it requires a DOM element. The subject received was: `#email`
```

Used in: [Inspect an element's state while debugging a test](#Inspect-an-elements-state-while-debugging-a-test)

### isInputType()

```
Cypress.dom.isInputType(element, type)
```

Returns `true` when the element is an `<input>` or `<button>` whose `type` matches. Any other element returns `false`.

**type** **_(String, Array)_**

The type to match, such as `'email'`, or an array of types, any of which can match.

*   It reads the element's `type` property, not its attribute. A browser treats an `<input>` with an unknown `type`, such as `type="nickname"`, as `type="text"`, and so does `isInputType()`.
*   For a jQuery object, it checks the first element.

Used in: [Fill in each field of a form by its input type](#Fill-in-each-field-of-a-form-by-its-input-type)

### isJquery()

```
Cypress.dom.isJquery(obj)
```

Returns `true` when the value is a jQuery object, such as the subject an element query yields. It checks the object itself, so it also recognizes jQuery objects created by a different copy of jQuery, such as your app's.

Used in: [Log any subject in a custom command](#Log-any-subject-in-a-custom-command)

### isScrollable()

```
Cypress.dom.isScrollable(element)
```

Returns `true` when the element's content overflows it and its `overflow` is `scroll` or `auto`, so the element can scroll. This is the check [`.scrollTo()`](/llm/markdown/api/commands/scrollto.md) runs before it scrolls an element.

*   For a jQuery object, it checks the first element.
*   For a `window`, or for the `<html>` element, it returns `true` when the page is taller or wider than the window.

Used in: [Inspect an element's state while debugging a test](#Inspect-an-elements-state-while-debugging-a-test), [Scroll a container only when its content overflows](#Scroll-a-container-only-when-its-content-overflows)

### isVisible()

```
Cypress.dom.isVisible(element)
```

Returns `true` when Cypress considers the element visible, following the [visibility rules](/llm/markdown/app/core-concepts/interacting-with-elements.md#Visibility) of your [`visibilityStrategy`](/llm/markdown/app/core-concepts/interacting-with-elements.md#Visibility-Strategy). It is the opposite of [`isHidden()`](#isHidden).

*   It treats an element with `opacity: 0` as hidden. The actionability checks that run before Cypress clicks or types ignore opacity, so an element can be hidden by this check and still be actionable.
*   Under the default `'modern'` strategy, a jQuery object with several elements counts as visible only when every element in it is visible.
*   It checks the element's state at the moment you call it and doesn't retry.

When the value isn't a DOM element or a jQuery object, it throws:

```
`Cypress.dom.isVisible()` failed because it requires a DOM element. The subject received was: `#email`
```

Used in: [Inspect an element's state while debugging a test](#Inspect-an-elements-state-while-debugging-a-test), [Dismiss an optional banner only when it is visible](#Dismiss-an-optional-banner-only-when-it-is-visible)

### isWindow()

```
Cypress.dom.isWindow(obj)
```

Returns `true` when the value is a `window`, such as the one [`cy.window()`](/llm/markdown/api/commands/window.md) yields, or a jQuery object that wraps one.

Used in: [Log any subject in a custom command](#Log-any-subject-in-a-custom-command)

### stringify()

```
Cypress.dom.stringify(element, form)
```

Returns a string that describes the element, the format Cypress uses in its Command Log and error messages.

**form** **_(String)_**

`'long'` (the default) or `'short'`. Any other value throws.

| `form` | Returns | Example |
| --- | --- | --- |
| `'long'` | the element's opening and closing tags, with its text cut to 10 characters | `<p id="intro">Hello w...</p>` |
| `'short'` | the tag name with its `id` and classes | `<p#intro.lead>` |

*   For a jQuery object with several elements, `'short'` describes the first and counts the rest, as in `[ <li.item>, 2 more... ]`.
*   A `window` returns `<window>` and a `document` returns `<document>`.

Used in: [Inspect an element's state while debugging a test](#Inspect-an-elements-state-while-debugging-a-test), [Log any subject in a custom command](#Log-any-subject-in-a-custom-command)

### unwrap()

```
Cypress.dom.unwrap(obj)
```

Returns the DOM elements inside a jQuery object as an array, even when it holds a single element. Any other value comes back unchanged.

Used in: [Log any subject in a custom command](#Log-any-subject-in-a-custom-command)

### wrap()

```
Cypress.dom.wrap(obj)
```

Returns a jQuery object that wraps the value, the same as calling [`Cypress.$`](/llm/markdown/api/utilities/$.md) with it. Use it when you hold a DOM element and need jQuery methods on it, such as `.find()` or `.attr()`.

*   A selector string searches the spec's document, not your app's, so it finds nothing on the page you visited. To find elements in your app, use [`cy.$$`](/llm/markdown/api/utilities/$.md) or a query such as [`cy.get()`](/llm/markdown/api/commands/get.md).

## Examples

### Inspect an element's state while debugging a test

When a command fails on an element and the error doesn't explain why, log the checks Cypress runs. This prints a table of them to the browser's DevTools console, followed by the reason when the element is hidden:

cypress/e2e/checkout.cy.js

```
cy.get('#place-order').then(($button) => {
  console.table({
    element: Cypress.dom.stringify($button, 'short'),
    attached: Cypress.dom.isAttached($button),
    visible: Cypress.dom.isVisible($button),
    focusable: Cypress.dom.isFocusable($button),
    focused: Cypress.dom.isFocused($button),
    scrollable: Cypress.dom.isScrollable($button),
  })

  if (Cypress.dom.isHidden($button)) {
    console.log(Cypress.dom.getReasonIsHidden($button))
  }
})
```

For a button with `display: none`, the console shows `visible` as `false` and names the computed style that hides it:

[`cy.get()`](/llm/markdown/api/commands/get.md) waits only for the element to exist, so the callback runs as soon as the button is in the DOM, whether or not it is visible.

### Dismiss an optional banner only when it is visible

Some apps show a banner on some visits and not others, such as a promotion that appears on a schedule. When the test can't control whether it shows, check for it and dismiss it only when it's there:

cypress/e2e/home.cy.js

```
cy.get('body').then(($body) => {
  const $banner = $body.find('#promo-banner')

  if ($banner.length && Cypress.dom.isVisible($banner)) {
    cy.wrap($banner).find('button.dismiss').click()
  }
})

cy.contains('h1', 'Welcome').should('be.visible')
```

The check runs once, when the callback runs, and doesn't retry. A banner that appears a moment later is missed, and the test carries on without dismissing it. When you can, control the banner from the test instead, for example with a cookie or a stubbed response. The [conditional testing guide](/llm/markdown/app/guides/conditional-testing.md) covers the alternatives.

### Check whether the app re-rendered an element

Some apps update an element in place, and others replace it with a new one. To test that an action replaces the element, hold on to the original and check that it detaches:

cypress/e2e/cart.cy.js

```
cy.get('#cart-total').then(($oldTotal) => {
  cy.get('#add-to-cart').click()

  cy.get('#cart-total').should(($newTotal) => {
    expect(Cypress.dom.isDetached($oldTotal)).to.be.true
    expect($newTotal).to.have.text('$20.00')
  })
})
```

[`.should()`](/llm/markdown/api/commands/should.md) retries the callback, and [`cy.get()`](/llm/markdown/api/commands/get.md) queries the page again on each retry, so the assertion passes once the old element has detached and the new one shows the updated total.

### Scroll a container only when its content overflows

[`.scrollTo()`](/llm/markdown/api/commands/scrollto.md) fails on an element that can't scroll. When the amount of content varies, such as an activity feed, scroll only when there is somewhere to scroll to:

cypress/e2e/activity.cy.js

```
cy.get('#activity-feed').then(($feed) => {
  if (Cypress.dom.isScrollable($feed)) {
    cy.wrap($feed).scrollTo('bottom')
  }
})
```

### Fill in each field of a form by its input type

To fill in a form whose fields change, such as a sign-up form behind a feature flag, iterate over its inputs and choose a value by type:

cypress/e2e/signup.cy.js

```
cy.get('#signup input').each(($input) => {
  if (Cypress.dom.isInputType($input, 'email')) {
    cy.wrap($input).type('jane@example.com')
  } else if (Cypress.dom.isInputType($input, ['text', 'search'])) {
    cy.wrap($input).type('Jane')
  }
})
```

Inputs of other types, such as checkboxes, are left as they are. An input with an unknown `type` counts as `text`, the same way the browser treats it, so it gets `'Jane'`.

### Check that keyboard focus stays inside a dialog

An accessible modal dialog traps focus: pressing Tab on its last control moves focus back to its first, rather than to the page behind it. This test counts the dialog's focusable elements, presses Tab once more than that, and checks that focus never leaves the dialog:

cypress/e2e/settings.cy.js

```
cy.get('#open-settings').click()

cy.get('[role="dialog"]').then(($dialog) => {
  const $focusable = $dialog
    .find('*')
    .filter((_, el) => Cypress.dom.isFocusable(el))

  // Press Tab once more than there are focusable elements,
  // so focus has to wrap around to the start
  for (let i = 0; i <= $focusable.length; i++) {
    cy.press(Cypress.Keyboard.Keys.TAB)
    cy.focused().should(($focused) => {
      expect(Cypress.dom.isDescendent($dialog, $focused)).to.be.true
    })
  }
})
```

If focus moves outside the dialog, the assertion fails. `isFocusable()` doesn't check visibility, so if the dialog holds hidden controls, filter them out with [`isVisible()`](#isVisible) before you count.

### Log any subject in a custom command

A custom command that accepts any subject can use the type checks to describe what it received. The `.logSubject()` command below writes the subject to the Command Log in the same short format Cypress uses, and lists its type and its elements when you click the log entry:

*   JavaScript
*   TypeScript

cypress/support/commands.js

```
const describeType = (subject) => {
  if (Cypress.dom.isWindow(subject)) return 'window'
  if (Cypress.dom.isDocument(subject)) return 'document'
  if (Cypress.dom.isElement(subject)) return 'element'

  return typeof subject
}

Cypress.Commands.add('logSubject', { prevSubject: true }, (subject) => {
  Cypress.log({
    name: 'logSubject',
    message: Cypress.dom.isDom(subject)
      ? Cypress.dom.stringify(subject, 'short')
      : String(subject),
    consoleProps: () => ({
      Type: describeType(subject),
      Elements: Cypress.dom.isJquery(subject)
        ? Cypress.dom.unwrap(subject)
        : subject,
    }),
  })

  return subject
})
export {}
```

cypress/support/commands.ts

```
declare global {
  namespace Cypress {
    interface Chainable<Subject> {
      logSubject(): Chainable<Subject>
    }
  }
}

const describeType = (subject: unknown) => {
  if (Cypress.dom.isWindow(subject)) return 'window'
  if (Cypress.dom.isDocument(subject)) return 'document'
  if (Cypress.dom.isElement(subject)) return 'element'

  return typeof subject
}

Cypress.Commands.add('logSubject', { prevSubject: true }, (subject) => {
  Cypress.log({
    name: 'logSubject',
    message: Cypress.dom.isDom(subject)
      ? Cypress.dom.stringify(subject, 'short')
      : String(subject),
    consoleProps: () => ({
      Type: describeType(subject),
      Elements: Cypress.dom.isJquery(subject)
        ? Cypress.dom.unwrap(subject)
        : subject,
    }),
  })

  return subject
})

export {}
```

Chain it off any command that yields a subject:

cypress/e2e/signup.cy.js

```
cy.get('#signup input').logSubject()
cy.document().logSubject()
cy.title().logSubject()
```

The Command Log shows `[ <input#name>, 4 more... ]` for the five inputs, `<document>` for the document, and the page title as plain text:

Clicking the first `logSubject` row prints its type and all five inputs to the DevTools console:

## Notes

### Cypress.dom checks don't retry on their own

Each function checks the DOM once, at the moment you call it. In a [`.then()`](/llm/markdown/api/commands/then.md) callback, the result can be out of date by the time the next command runs. To wait for a condition, call the function inside a [`.should()`](/llm/markdown/api/commands/should.md) callback or a [custom query](/llm/markdown/api/cypress-api/custom-queries.md), which Cypress retries until it passes or times out.

### Cypress.dom compared with Cypress.ensure and assertions

*   An assertion such as `.should('be.visible')` checks state in a test, and retries.
*   [`Cypress.ensure`](/llm/markdown/api/cypress-api/ensure.md) throws a Cypress error when a subject fails a check, for validating the subject of a custom query or command.
*   `Cypress.dom` returns the answer and leaves what to do with it to you.

## History

| Version | Changes |
| --- | --- |
| [16.2.1](/llm/markdown/app/references/changelog.md#16-2-1) | `getReasonIsHidden()`, `isDescendent()`, `isFocusable()`, `isFocused()`, and `isScrollable()` accept either a DOM element or a jQuery object |
| [16.0.0](/llm/markdown/app/references/changelog.md#16-0-0) | `isVisible()`, `isHidden()`, and `getReasonIsHidden()` follow the [`visibilityStrategy`](/llm/markdown/app/core-concepts/interacting-with-elements.md#Visibility-Strategy), which defaults to `'modern'` |
| [6.2.1](/llm/markdown/app/references/changelog.md#6-2-1) | `isJquery()` always returns a boolean |
| [0.20.0](/llm/markdown/app/references/changelog.md#0-20-0) | Renamed `Cypress.Dom` to `Cypress.dom` |

## See also

*   [`Cypress.ensure`](/llm/markdown/api/cypress-api/ensure.md) - checks that throw a Cypress error instead of returning a boolean
*   [Custom Queries](/llm/markdown/api/cypress-api/custom-queries.md) - retry a `Cypress.dom` check until it passes
*   [Interacting with Elements](/llm/markdown/app/core-concepts/interacting-with-elements.md) - the visibility and actionability rules behind these checks
*   [`Cypress.$`](/llm/markdown/api/utilities/$.md)
