Cypress Command Syntax: cy.get(), cy.contains(), cy.click()
You'll learn how Cypress uses a jQuery-like chaining API to find and interact with DOM elements in your React app.
The jQuery-Inspired Design
Cypress commands work like a fluent, chainable API. You start with cy, select elements, then chain actions or assertions. Unlike React Testing Library's immediate queries, Cypress commands are asynchronous by default and automatically retry until elements appear.
Core Selection Commands
cy.get(selector) finds elements using CSS selectors, data attributes, or test IDs:
cy.get('button') // Find by tag
cy.get('.submit-btn') // Find by class
cy.get('[data-testid="login-form"]') // Find by test ID
cy.contains(text) finds elements containing specific text:
cy.contains('Sign In') // Any element with this text
cy.contains('button', 'Submit') // Specific element type
Chaining Actions
Once you've selected an element, chain interaction commands:
cy.get('input[name="email"]')
.type('user@example.com')
cy.contains('Submit')
.click()
cy.get('[data-testid="dropdown"]')
.select('Option 2')
Why This Syntax Matters
Think of Cypress commands like giving directions: "Find the button that says 'Submit,' then click it." Each command waits for the element to be actionable before proceeding—no manual waitFor needed. This makes tests more reliable and readable.
The chaining pattern means you can build complex interactions step-by-step:
cy.get('form')
.within(() => {
cy.get('input[name="username"]').type('admin')
cy.get('input[name="password"]').type('secret')
})
.submit()
Key Takeaway: Cypress commands use jQuery-like selectors with automatic waiting and chaining, making element selection and interaction both powerful and readable without manual async handling.