Interacting with Forms: Typing, Selecting, Checking
You'll learn: How to use Cypress commands to fill out and submit forms in your React app, simulating real user input.
The Core Idea
Forms are the heart of user interaction in most apps. Cypress provides three essential commands that mirror how users actually fill out forms: cy.type() for text inputs, cy.select() for dropdown menus, and cy.check() for checkboxes and radio buttons. These commands don't just set values programmatically—they simulate real keyboard input and mouse clicks, triggering all the same events your React components would receive from actual users.
How It Works
cy.type() simulates typing character by character into input fields and textareas:
cy.get('input[name="email"]').type('user@example.com');
cy.get('textarea').type('This is my message');
cy.select() picks options from <select> dropdowns by visible text or value:
cy.get('select[name="country"]').select('United States');
cy.get('select#size').select('medium'); // by value attribute
cy.check() and cy.uncheck() toggle checkboxes and radio buttons:
cy.get('input[type="checkbox"]').check();
cy.get('input[value="newsletter"]').uncheck();
cy.get('input[type="radio"][value="monthly"]').check();
These commands automatically wait for elements to become actionable and trigger the same onChange, onInput, and other events that React's controlled components rely on. You can chain them with assertions to verify the form state updates correctly.
Real-World Usage
cy.get('input[name="username"]').type('johndoe');
cy.get('input[name="password"]').type('secret123');
cy.get('input[type="checkbox"][name="remember"]').check();
cy.get('button[type="submit"]').click();
cy.contains('Welcome back, johndoe').should('be.visible');
Key Takeaway: Use cy.type(), cy.select(), and cy.check() to interact with forms exactly as users would, ensuring your React form logic responds correctly to real input events.