DEV Community

Cover image for How to fill out and submit forms with Cypress

How to fill out and submit forms with Cypress

Walmyr Filho
I'm a software engineer that loves testing. I'm also a clean coder, blogger, YouTuber, Ambassador, writer, online teacher, mentor, consultant, speaker, an active member of tech communities.
Updated on ・2 min read

Welcome to the “Pinches of Cypress” series! 🧂

In this series of content in text format with code snippets, you will learn several tricks of the automated testing framework, to make your life easier in writing test scripts.

I will start with something simple, and we will evolve throughout the series, okay?

Let's say you are testing an application that sells event tickets.

In this example application, for a user to buy a ticket, he must fill in some mandatory data, and after submitting the form, he will receive a success message.

Let's say that the mandatory fields are as follows:

  • Full name
  • Email
  • Checkbox that you agree to the terms of service

Let's also imagine that the elements above have unique IDs.

The test would look something like this:

describe('Online tickets selling app', () => {
  beforeEach(() => {

  it('successfully orders a ticket', () => {
      .type('John Doe')

    cy.contains('You will receive an email to finish the purchase.')
Enter fullscreen mode Exit fullscreen mode

Note that before typing (type), checking (check), or clicking on elements (click), I am ensuring that they are visible (.should('be.visible')). This makes the test more robust.

Finally, I verify that the text "You will receive an email to finalize your purchase" is contained in some element and is visible.

That's it!

I hope you enjoyed it, and stay tuned for the next content in the series. 👋

This post was originally published in Portuguese on the Talking About Testing blog.

Would you like to learn about test automation with Cypress? Get to know my online courses on Udemy.

Discussion (0)