|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Cypress AI command |
| 4 | +date: 2025-06-01 19:14:20 |
| 5 | +excerpt: How to generate E2E tests with the Cypress AI command. |
| 6 | +categories: cypress e2e test cypress ai llm ollama |
| 7 | +--- |
| 8 | + |
| 9 | +This post goes over how to generate E2E tests with the [Cypress AI](https://github.com/ai-action/cy-ai) command. |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +You have [Cypress](https://docs.cypress.io/app/get-started/install-cypress) installed and set up: |
| 14 | + |
| 15 | +```sh |
| 16 | +npm install cypress |
| 17 | +``` |
| 18 | + |
| 19 | +```sh |
| 20 | +npx cypress open |
| 21 | +``` |
| 22 | + |
| 23 | +You have [Ollama](https://ollama.com/download) installed and running: |
| 24 | + |
| 25 | +```sh |
| 26 | +curl -fsSL https://ollama.com/install.sh | sh |
| 27 | +``` |
| 28 | + |
| 29 | +```sh |
| 30 | +ollama serve |
| 31 | +``` |
| 32 | + |
| 33 | +## Install |
| 34 | + |
| 35 | +Install the [package](https://www.npmjs.com/package/cy-ai): |
| 36 | + |
| 37 | +```sh |
| 38 | +npm install cy-ai |
| 39 | +``` |
| 40 | + |
| 41 | +Download the [LLM](https://ollama.com/library/qwen2.5-coder) (large language model): |
| 42 | + |
| 43 | +```sh |
| 44 | +ollama pull qwen2.5-coder |
| 45 | +``` |
| 46 | + |
| 47 | +## Setup |
| 48 | + |
| 49 | +Import the command in `cypress/support/commands.js`: |
| 50 | + |
| 51 | +```js |
| 52 | +// cypress/support/commands.js |
| 53 | +require('cy-ai'); |
| 54 | +``` |
| 55 | + |
| 56 | +If you're running Chrome, disable `chromeWebSecurity` so the LLM requests aren't blocked by CORS: |
| 57 | + |
| 58 | +```js |
| 59 | +// cypress.config.js |
| 60 | +import { defineConfig } from 'cypress'; |
| 61 | + |
| 62 | +export default defineConfig({ |
| 63 | + chromeWebSecurity: false, |
| 64 | +}); |
| 65 | +``` |
| 66 | + |
| 67 | +Write a test: |
| 68 | + |
| 69 | +```js |
| 70 | +// cypress/e2e/example.cy.js |
| 71 | +it('visits example.com', () => { |
| 72 | + cy.ai('go to https://example.com and see heading "Example Domain"'); |
| 73 | +}); |
| 74 | +``` |
| 75 | + |
| 76 | +If you'd like to configure the Cypress AI command (e.g., replace the LLM), check out the [options](https://github.com/ai-action/cy-ai#cyai). |
| 77 | + |
| 78 | +## How It Works |
| 79 | + |
| 80 | +1. A prompt is created from your task, the HTML body, and the template. |
| 81 | +2. The prompt is sent to the LLM server. |
| 82 | +3. The LLM server responds with Cypress code. |
| 83 | +4. The Cypress code is cleaned and run. |
| 84 | +5. If the steps pass, the code is saved to `cypress/e2e/**/__generated__/*.json`. |
| 85 | +6. If the steps fail, an error is thrown and the LLM response can be inspected in the browser `Console`. |
| 86 | + |
| 87 | +When running tests, if the generated Cypress code exists, the command will reuse the existing code. |
| 88 | + |
| 89 | +To regenerate a step, enable the [regenerate](https://github.com/ai-action/cy-ai#regenerate) option or delete the generated code in `cypress/e2e/**/__generated__/*.json`. |
| 90 | + |
| 91 | +## Links |
| 92 | + |
| 93 | +- [GitHub](https://github.com/ai-action/cy-ai) |
| 94 | +- [Documentation](https://ai-action.github.io/cy-ai/) |
0 commit comments