Testing Forms and Input Fields with Cypress

Forms are one of the most critical components of any web application. They collect data, drive user interactions, and often play a key role in business workflows—whether it's user registration, login, checkout, or feedback submission. Ensuring that forms behave correctly under all conditions is essential for a seamless user experience. That’s where Cypress—a powerful, modern end-to-end testing framework—comes in.

In this blog, we'll walk through how to effectively test forms and input fields using Cypress, including techniques for validating user input, handling edge cases, and best practices to keep your tests clean and reliable.


Why Use Cypress for Form Testing?

Cypress is ideal for form testing because:

It runs in a real browser, offering a true user-like experience.

It automatically waits for elements to be visible or ready.

It provides easy access to form elements using selectors.

It offers built-in support for assertions, timeouts, and debugging.


Setting Up Cypress

If you haven’t already installed Cypress in your project:

bash

npm install cypress --save-dev

npx cypress open

Once Cypress opens, you can create a test file under cypress/e2e/ and start writing your form test cases.


Basic Form Testing Example

Let's assume we have a basic contact form with the following fields:

Name (#name)

Email (#email)

Message (#message)

Submit button (#submit)


Test: Fill and Submit the Form

javascript


describe('Contact Form', () => {

  it('fills out and submits the form successfully', () => {

    cy.visit('/contact');


    cy.get('#name').type('John Doe');

    cy.get('#email').type('john@example.com');

    cy.get('#message').type('This is a test message.');

    

    cy.get('#submit').click();


    cy.contains('Thank you for contacting us!').should('be.visible');

  });

});

This test checks whether the form can be filled out and submitted successfully, and verifies the success message afterward.


Testing Validation and Edge Cases

Forms should handle invalid inputs gracefully. Cypress can help you verify those validations.


Test: Empty Fields

javascript


it('shows error when submitting empty form', () => {

  cy.visit('/contact');

  cy.get('#submit').click();

  

  cy.get('.error').should('contain', 'Name is required');

  cy.get('.error').should('contain', 'Email is required');

});

Test: Invalid Email Format

javascript


it('displays error for invalid email', () => {

  cy.visit('/contact');

  cy.get('#email').type('invalid-email');

  cy.get('#submit').click();

  

  cy.get('.error').should('contain', 'Enter a valid email');

});

Testing Radio Buttons, Checkboxes, and Dropdowns

Cypress supports all input types:


javascript


cy.get('input[type="radio"][value="male"]').check();

cy.get('input[type="checkbox"]').check();

cy.get('select').select('Feedback');

You can chain assertions to ensure they are selected:


javascript

cy.get('input[type="checkbox"]').should('be.checked');

Best Practices

Use data- attributes* for stable selectors (e.g., data-cy="email").

Clear fields before typing to avoid test pollution.

Mock API calls for form submission using cy.intercept() to avoid hitting real servers in testing.

Group related tests logically using describe and context blocks.


Conclusion

Testing forms and input fields with Cypress is straightforward and highly effective. Cypress provides everything you need to simulate real user interactions and validate that your forms work as expected under all scenarios. By covering both positive and negative test cases, and adhering to best practices, you can ensure your forms are robust, user-friendly, and bug-free in production.


Learn Cypress Training

Read More: Writing Reusable Custom Commands in Cypress
Read More: Cypress vs Playwright: Which is Better?
Read More: Difference Between Cypress and Selenium

Visit IHUB Talent Training Institute in Hyderabad
Get Direction

Comments

Popular posts from this blog

How to Use Tosca's Test Configuration Parameters

Using Hibernate ORM for Fullstack Java Data Management

Creating a Test Execution Report with Charts in Playwright