# cypress-highlight

> Highlights all elements on the page with good test selectors by injecting a CSS rule

Latest version **1.4.0** (published 2025-01-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install cypress-highlight
pnpm add cypress-highlight
yarn add cypress-highlight
bun add cypress-highlight
```

## Health

**Score 40/100 (D)** — status: stable.

Positive: no vulnerabilities; high maintenance score.

Warnings: low downloads; no types; no esm support.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 1.4.0 |
| Published | 2025-01-30 |
| First published | 2021-11-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 22 |
| Author | Gleb Bahmutov |
| Maintainers | bahmutov |
| Keywords | cypress-plugin |

## Links

- npm: https://www.npmjs.com/package/cypress-highlight
- Repository: https://github.com/bahmutov/cypress-highlight
- Homepage: https://github.com/bahmutov/cypress-highlight#readme
- Issues: https://github.com/bahmutov/cypress-highlight/issues
- npm.io page: https://npm.io/package/cypress-highlight

## Alternatives

- [@snazzah/davey](https://npm.io/package/@snazzah/davey.md) — 1.5M weekly downloads
- [@vendure/testing](https://npm.io/package/@vendure/testing.md) — 8.3K weekly downloads
- [vue-simple-context-menu](https://npm.io/package/vue-simple-context-menu.md) — 6.6K weekly downloads
- [cypress-webpack-preprocessor-v5](https://npm.io/package/cypress-webpack-preprocessor-v5.md) — 2.1K weekly downloads
- [@backstage/plugin-catalog-backend-module-puppetdb](https://npm.io/package/@backstage/plugin-catalog-backend-module-puppetdb.md) — 1.3K weekly downloads

## Recent versions

- 1.4.0 (latest) — 2025-01-30
- 1.3.0 — 2024-09-17
- 1.2.0 — 2021-11-01
- 1.1.0 — 2021-11-01
- 1.0.0 — 2021-11-01

## README

# cypress-highlight

[![ci status][ci image]][ci url] [![renovate-app badge][renovate-badge]][renovate-app] ![cypress version](https://img.shields.io/badge/cypress-13.14.2-brightgreen)

> Highlights all elements on the page with good test selectors by injecting a CSS rule

![Image with highlighted elements that have data-cy attribute](./images/app.png)

## Videos

- [Add A Red Border Around Elements That Have data-cy Attribute](https://youtu.be/pHzroBFY5V0)
- [Cypress-highlight Plugin Introduction](https://youtu.be/EIbSzT6QSxc)

## Install

```shell
$ npm install -D cypress-highlight
# or using Yarn
$ yarn add -D cypress-highlight
```

## Use

```js
import { highlight } from 'cypress-highlight'

it('loads an app', () => {
  cy.visit('/')
  highlight()
  // you can capture a screenshot to see the elements
  // with good test selectors
  cy.screenshot('highlights', { capture: 'runner' })
})
```

See [spec.js](./cypress/integration/spec.js)

### Multiple selectors

By default, this module highlight all elements with `data-cy` attribute. You can pass your own list of selectors to highlight. For example, to highlight all elements with data attribute `test-id` and all elements with class name `testable`, use

```js
highlight('[data-test-id]', '.testable')
```

### Options object

You can pass multiple options using an object

```js
// highlight all elements that have "data-cy" attribute
highlight({
  selectors: '[data-cy]',
})
// highlight all elements that have "data-cy" attribute
// plus all elements with class "todo-li"
highlight({
  selectors: ['[data-cy]', '.todo-li'],
})
```

#### failIfFound

You can highlight and then fail the test if any matching elements are found

```js
highlight({
  selectors: '[data-error]',
  failIfFound: true,
})
```

#### failIfNotFound

You can fail the test if no matching elements are found

```js
highlight({
  selectors: 'li.todo',
  failIfNotFound: true,
})
```

### highlightMissingTestIds

Highlights any buttons / input elements / anchor links without `data-cy` attribute. Fails by default if any are found.

```js
import { highlightMissingTestIds } from 'cypress-highlight'
// somewhere during the test
highlightMissingTestIds()
// disable throwing an error on a found element
highlightMissingTestIds(false)
```

![Found input with missing data-cy attribute](./images/missing.png)

## Read

[Cypress best practices for selecting elements](https://on.cypress.io/best-practices#Selecting-Elements)

## Small print

Author: Gleb Bahmutov &lt;gleb.bahmutov@gmail.com&gt; &copy; 2021

- [@bahmutov](https://twitter.com/bahmutov)
- [glebbahmutov.com](https://glebbahmutov.com)
- [blog](https://glebbahmutov.com/blog)
- [videos](https://www.youtube.com/glebbahmutov)
- [presentations](https://slides.com/bahmutov)
- [cypress.tips](https://cypress.tips)

License: MIT - do anything with the code, but don't blame me if it does not work.

Support: if you find any problems with this module, email / tweet /
[open issue](https://github.com/bahmutov/cypress-highlight/issues) on Github

## MIT License

Copyright (c) 2021 Gleb Bahmutov &lt;gleb.bahmutov@gmail.com&gt;

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

[ci image]: https://github.com/bahmutov/cypress-highlight/workflows/ci/badge.svg?branch=main
[ci url]: https://github.com/bahmutov/cypress-highlight/actions
[renovate-badge]: https://img.shields.io/badge/renovate-app-blue.svg
[renovate-app]: https://renovateapp.com/

---
_Source: https://npm.io/package/cypress-highlight · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
