Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk6 min

How to Test Form Submission in Cypress

A practical Cypress form-testing guide with runnable code, request assertions, validation and failure cases, and fixes for common intercept and redirect problems.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To test a form submission in Cypress, fill in the form as a user would, click its submit button, and assert the result the user should see. If you also need to verify the browser’s request, register cy.intercept() before clicking, wait for its alias, and check the request data. Use direct form submission or Enter-key tests for those specific interactions; neither replaces testing the visible submit flow.

Test the user flow and the result

Start with the behavior that matters to the user: the form is available, valid values can be entered, submission occurs, and the application responds correctly. The selectors, endpoint, payload, and success state below are examples; replace them with the ones your application actually uses.

As an Amazon Associate I earn from qualifying purchases.

describe('new user form', () => {
  beforeEach(() => {
    cy.visit('/users/new')
    cy.intercept('POST', '/users', {
      statusCode: 201,
      body: { id: 123, name: 'Ada Lovelace' },
    }).as('createUser')
  })

  it('submits valid values and shows the created user', () => {
    cy.get('[data-testid="name"]').type('Ada Lovelace')
    cy.get('[data-testid="email"]').type('[email protected]')
    cy.get('button[type="submit"]').click()

    cy.wait('@createUser').its('request.body').should('include', {
      name: 'Ada Lovelace',
      email: '[email protected]',
    })
    cy.contains('Ada Lovelace').should('be.visible')
  })
})

This example stubs a successful response with status 201, so it tests the front end against a controlled result rather than depending on a live service. Cypress can also spy on a request without stubbing it, allowing it to reach the real server. Choose deliberately: a controlled response isolates UI behavior, while a real request checks more of the integration path. See Cypress’s network requests guide.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why the intercept belongs before the click

Register the route before taking the action that should trigger it. Alias the route and wait for that alias rather than inserting an arbitrary delay. After the wait, Cypress exposes request and response details for assertions. Intercepts are cleared before each test, so create the alias in that test’s setup. See cy.intercept() and Cypress’s guide to aliases.

Choose the submission method that matches the behavior

Approach What it verifies well Consideration
Click the submit button and observe with cy.intercept() The visible user interaction and the browser request it produces Requires the application UI and a matching request; decide whether to stub or use the real service.
cy.get('form').submit() The form’s submit behavior directly Does not establish that a user can operate the visible controls successfully.
.type('{enter}') Keyboard submission where the application supports it Implicit submission depends on the form structure and submit-button configuration and state.
cy.request() Direct endpoint behavior, response assertions, or API setup and teardown Bypasses the browser UI and is not observed by cy.intercept().

Direct form submission

Use cy.get('form').submit() when the purpose is specifically to exercise the form’s submit behavior. Cypress’s cross-origin testing guide describes direct form submission and ordinary browser handling of the resulting HTTP navigation. For an end-to-end test of what a user can do, prefer entering values and activating the actual submit control.

Enter-key submission

If keyboard submission is a supported interaction, focus an appropriate input and use .type('{enter}'), then assert the request or resulting UI. Cypress documents that Enter follows the browser’s implicit form submission rules; whether it submits depends on the HTML form and submit-button configuration, including whether the button is disabled. See cy.type().

Direct API requests

Use cy.request() when you want to test an endpoint directly or prepare and clean up server state. It does not simulate a user completing the browser form, and Cypress explicitly distinguishes direct requests from application traffic: cy.intercept() does not catch a direct cy.request(). See cy.request().

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Build a useful form test set

Keep each test focused on a distinct expected behavior, and initialize the page or component and its data for each test rather than relying on a previous test’s state. Cypress’s best practices explain why tests that depend on one another can fail when run alone.

  • Successful submission: enter valid values, submit through the UI, and assert a meaningful success state. If the request matters, also assert its method, endpoint, and relevant payload fields.
  • Validation: submit with a required value missing or invalid, then assert the application’s actual validation message or field state. Keep this separate from the success case.
  • Keyboard path: when supported, submit with Enter and assert the behavior users should experience.
  • Failure response: stub or produce a server failure and assert the application’s error handling.
  • Redirect: assert the destination. If the redirect crosses to another origin and Cypress commands must continue there, use cy.origin() for those subsequent interactions.

For redirect and cross-origin details, consult the cross-origin testing guide. Cypress’s network guide covers stubbing requests and allowing them to reach a real server.

Troubleshoot common failures

The test waits for an intercept that never arrives

  • Confirm the cy.intercept() is registered before the submit action and that its method and URL match the request. Supplying a method matters: without one, an intercept may match requests of any method.
  • Check that clicking the control actually submits the form and that validation or a disabled button is not preventing submission.
  • Consider browser caching: a cached resource may not reach the network layer and therefore may not trigger an intercept. This is more likely to matter for resources than for a form’s submission request, but it can explain other unexpected missing intercepts. Cypress documents this behavior in cy.intercept().

The request assertion fails

Inspect the intercepted request’s method, URL, and body to determine whether the application sends a different shape than the example. Assert only the fields that are part of the behavior or contract you intend to protect. If the form uses a different encoding or payload format, adapt the assertion to the actual request.

The success UI assertion fails

Check whether the test stubbed the response the application expects, whether the UI renders a different message or record, and whether the assertion targets the right element. Do not treat a successful request alone as proof that the user received the expected result.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Cross-origin redirect breaks later commands

When submission navigates to another origin and the test needs to interact with that page, place the subsequent cross-origin commands in cy.origin(). A redirect to another origin is different from a same-origin success screen.

Tests pass only in a particular order

Make each test initialize its own page and required data. Cypress recommends avoiding cross-test coupling so a form test can run independently rather than inheriting state left by an earlier test.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If you also need a screenshot of the form or its resulting page, ScreenshotNeo is a website screenshot API and MCP server; it does not replace Cypress’s form interaction and assertions. Its one-call API can capture a page as an image or PDF:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for request options. Cookie banners, popups, and chat widgets are removed before the shot; bot checks, blank pages, and failed loads are never billed. Its MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000. Sign up for free and get 1,000 screenshots a month with no card.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Frequently Asked Questions

Should a Cypress form test stub the server response?

Stub it when isolating front-end behavior; allow the real service when the purpose is to exercise the integration. A suite can use both approaches for different cases.

Can cy.intercept() observe cy.request()?

No. cy.request() makes a direct request rather than application browser traffic. Use cy.intercept() for the browser flow and cy.request() for direct endpoint work.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.