Ilesh Darji


Testing AI-Driven and Dynamic UI Applications with Cypress

Modern web applications are increasingly powered by AI and dynamic UIs — from personalized dashboards to content that updates based on machine learning models. While this makes the user experience more adaptive, it also creates challenges for automated testing.

In this post, we’ll look at how to test AI-generated and dynamic UI using Cypress, while avoiding flakiness and instability.


🤖 What Makes AI UIs Hard to Test?

  • Elements change based on unpredictable data
  • Text or layout may vary across sessions
  • DOM changes often depend on asynchronous logic
  • Machine learning models may introduce randomization

✅ Strategies for Testing Dynamic UIs in Cypress

1. Use Stable Selectors

Avoid relying on dynamic IDs or classes. Use:

  • data-testid
  • ARIA roles (cy.findByRole())
  • Custom stable attributes
cy.get('[data-testid="recommendation-card"]').should('exist')

2. Test Behavior, Not Exact Content

Instead of asserting exact dynamic text, assert existence or structure:

cy.get('.recommendation').should('have.length.greaterThan', 0)

3. Leverage Retries for Dynamic Content

Cypress automatically retries assertions. Take advantage of it:

cy.get('.chat-response').should('contain.text', 'Your answer')

4. Mock AI Outputs When Needed

Use cy.intercept() to replace unpredictable AI responses with known values:

cy.intercept('POST', '/api/generate', {
  body: { response: 'This is a test reply' }
}).as('mockAI')

cy.visit('/chat')
cy.wait('@mockAI')

5. Snapshot Testing for Visual UI

If your AI UI produces structured cards or tables, consider snapshot or image comparison plugins like:

  • cypress-image-snapshot
  • @james-cypress/snapshot

These help catch unintentional visual regressions.


🧠 Best Practices

  • Build testable AI features (e.g. expose test endpoints or mock flags)
  • Use feature flags to disable AI in test environments if needed
  • Test key UX behaviors (e.g. “user sees feedback” not exact text)

🚀 Final Thoughts

AI-powered apps don’t have to be testing nightmares. By focusing on UI behavior, using mocked data, and embracing Cypress’s powerful tooling, you can build reliable test coverage even for non-deterministic interfaces.