Cypress Studio AI helps you generate robust selectors for your end-to-end tests by analyzing your application’s DOM and suggesting stable locators, while also providing mechanisms to handle sensitive data like passwords and tokens securely. This guide walks you through how it works, how to use it effectively, and how to keep your test data safe.
What You’ll Need
- A Cypress project (version 10 or later) with Cypress Studio enabled in your config file (
experimentalStudio: true). - Access to the application you want to test, either locally or via a URL.
- Basic familiarity with Cypress commands and test structure.
- For sensitive data handling, an environment variable manager (like a
.envfile or CI secrets) and the Cypresscy.env()command.
Step-by-Step
- Enable Cypress Studio – In your
cypress.config.jsorcypress.config.ts, setexperimentalStudio: trueunder thee2eobject. Save the file and restart Cypress. - Open Cypress Studio – Run
npx cypress open, select your spec file, and click the “Studio” button in the top-right corner of the test runner. This launches the interactive recording mode. - Interact with your app – In the Studio window, your app loads in an iframe. Click, type, and interact with elements just as a user would. Cypress Studio records your actions and automatically generates selectors for each element.
- Review the generated selectors – After each action, Cypress Studio writes a command like
cy.get('[data-cy=submit]'). It prioritizes attributes likedata-cy,data-testid, oridif present; otherwise it falls back to CSS selectors. The AI component analyzes the DOM to choose the most stable selector, avoiding index-based or overly specific paths. - Refine selectors manually if needed – If the generated selector looks fragile (e.g.,
.container > div:nth-child(2)), replace it with a more semantic one. Right-click the command in the Studio editor and choose “Edit” to modify it. - Save the test – Once you’ve recorded all steps, click “Save” in the Studio toolbar. Cypress writes the test to your spec file with the generated selectors.
- Handle sensitive data – For any input that contains secrets (passwords, API keys, tokens), do not type them directly into Studio. Instead, after saving, edit the test to replace the literal value with
Cypress.env('MY_SECRET'). For example, changecy.get('#password').type('supersecret')tocy.get('#password').type(Cypress.env('PASSWORD')). - Store secrets in environment variables – Create a
cypress.env.jsonfile (git-ignored) or use your CI’s secret management. Add your variables there. In the test, reference them viaCypress.env(). - Use aliases for repeated sensitive values – If you use the same token in multiple places, assign it to an alias:
cy.wrap(Cypress.env('TOKEN')).as('authToken')and then usecy.get('@authToken')in subsequent commands. - Mask sensitive values in reports – If you use Cypress Dashboard or a custom reporter, ensure that any console logs or command arguments that include secrets are redacted. Cypress automatically masks values typed via
.type()when usingCypress.env()in recent versions, but verify your reporter settings.
Common Mistakes
- Hardcoding secrets – Typing a password directly in Studio and saving it without replacing it with an environment variable. This exposes secrets in your repository.
- Over-relying on auto-generated selectors – Studio may generate selectors based on CSS classes that change frequently. Always prefer stable attributes like
data-cy. - Ignoring the AI’s suggestions – The AI sometimes suggests a selector that looks odd but is actually more robust. Read the reasoning before overriding.
- Not checking for duplicate selectors – If multiple elements share the same class or attribute, Studio might pick the first one, leading to flaky tests. Verify uniqueness.
- Storing secrets in spec files – Even if you replace the value later, if you commit the file before editing, the secret is exposed. Always edit before committing.
Tips
- Add
data-cyattributes to your app – This is the single best way to make Studio generate reliable selectors. It also improves test readability. - Use the Studio’s “Add Assertion” feature – After recording an action, you can add assertions to verify text or state, which helps catch regressions.
- Combine Studio with manual test writing – Use Studio for exploratory recording, then refine the generated code manually for complex logic.
- Keep environment variables out of version control – Add
cypress.env.jsonto your.gitignoreand document the required variables in a README. - Use Cypress’s
cy.session()for login flows – This caches authentication state, reducing the need to repeatedly type credentials in tests.
Troubleshooting
- Studio doesn’t appear – Ensure you’ve enabled
experimentalStudioand restarted Cypress. Check the Cypress version; Studio is stable in v10+. - Generated selectors are flaky – The AI might have picked a selector that works now but breaks on dynamic content. Inspect the element in DevTools and add a stable attribute if possible.
- Can’t type into a hidden input – Studio may fail if the element is covered or not visible. Use
{force: true}in the generated command, but only after confirming it’s safe. - Environment variable returns undefined – Check that your
cypress.env.jsonfile is in the project root and that the key matches exactly. Also ensure the file is not git-ignored incorrectly. - Secrets appear in test logs – If you’re using a custom reporter, it might log command arguments. Use Cypress’s built-in redaction or configure your reporter to strip sensitive values.
Frequently Asked Questions
Does Cypress Studio AI work with any framework?
Yes, it works with any web app that Cypress can load. The AI analyzes the rendered DOM, so framework-specific attributes are not required, though they help.
Can I use Cypress Studio for API testing?
No, Studio is for UI interactions only. For API tests, use Cypress’s cy.request() commands manually.
How do I prevent sensitive data from being recorded in Studio?
You can’t prevent recording, but you can replace the values immediately after saving. Alternatively, avoid typing real secrets during recording; use dummy values and swap them later.
Is Cypress Studio free?
Yes, it’s part of the open-source Cypress test runner. No additional license is required.
Can I use environment variables in Studio’s recording mode?
No, Studio only records literal values. You must edit the test after saving to use Cypress.env().
Will the AI always pick the best selector?
It’s designed to pick a stable one, but it’s not perfect. Review and adjust selectors, especially for elements that change frequently.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Feet and Inches to cm Converter: How to Calculate167.64 cm
- Enshrouded Palm Wood Logs: How to Get Them
- Grounded 2: Diving Bell Bed in Into the Abyss
- CPM Calculation for Budgeting5.00 $
- 1 Ton to kW Calculator: Convert Tons to Kilowatts3.5169 kW
- Convert Hz to Milliseconds: Formula & Calculator20.000000 ms