Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Sign InSign Up

Softans

Softans Logo Softans Logo
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
Home/ Questions/Q 1017
In Process
Ghulam Nabi
Ghulam Nabi
Asked: December 28, 20212021-12-28T04:48:47+00:00 2021-12-28T04:48:47+00:00

Submit a POST form in Cypress and navigate to the resulting page

I’m having issues with Cypress loading the response body when I utilize the cy.request() command.

In our application, when a form is filled out and submitted, it POSTs, and the response body is the new page.

When I’m trying to do in Cypress is programmatically fill out the form. So I set up a cy.request() command, with the body filled with the form fields, which is the same as what happens when you fill it out manually. When I run the command, I can view the console and see that the correct body is being returned, but the new document page doesn’t load. So I’m left just sitting on the old empty form page.

cy.request({
        url: "company-webpage-form-url.com",
        method: "POST",
        form: true,
        body: {
            first_name: "first_name",
            last_name: "last_name",
            company_name: "company_name",
            address1: "address1",
            address2: "address2",
            city: "city",
            state: "NY",
            zip: "13903",
            country: "US",
            phone_number: "607-555-5555",
            phone_ext: "555",
            fax_number: "fax_number",
            fax_ext: "fax_ext",
            email: "developer@company.com",
            email_2: "developer@company.com",
            user_data: "Continue"
        }
    });

All of the data is correct, and I get the correct response body, but I can only see it in the console. I have no idea how to get it to load, like it would when I submit the form. All I get right now is a 200 response, and the test ends.

I’ve tried visiting the next URL right after, but I get an error that the page for that URL doesn’t exist. I’ve tried clicking the submit button after the POST, but that just results in an empty form being submitted, which causes a validation error.

I’m at a loss for how to get cypress to load the response body, which is in the form of a document (the new page). Anyone have any tips?

Edit: I should add that – the reason I am looking to fill the form from a POST is because the form is necessary to fill out for me to test whether certain options work or not. I have a single test that ensures the form fields and submission work as required, but for the 30+ options that need to be checked on the other side of this form, I wanted to follow Cypress’ best practice of not manually filling the form every single time (they show an example with login on the website).

post api in cypresspost form in cypresssubmit a post form in cypress
  • 1
  • 1 1 Answer
  • 38 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

1 Answer

  • Voted
  • Oldest
  • Recent
  1. Luci
    2021-12-28T04:49:56+00:00Added an answer on December 28, 2021 at 4:49 am

    If you’d like to simulate a form POST navigating to a new page, you can use cy.visit() to do this! Just change your request to visit and it should work:

    cy.visit({
            url: "company-webpage-form-url.com",
            method: "POST",
            body: {
                first_name: "first_name",
                last_name: "last_name",
                company_name: "company_name",
                address1: "address1",
                address2: "address2",
                city: "city",
                state: "NY",
                zip: "13903",
                country: "US",
                phone_number: "607-555-5555",
                phone_ext: "555",
                fax_number: "fax_number",
                fax_ext: "fax_ext",
                email: "developer@company.com",
                email_2: "developer@company.com",
                user_data: "Continue"
            }
        });
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • Popular
  • Answers
  • Ghulam Nabi

    Why are the British confused about us calling bread rolls ...

    • 5 Answers
  • Alex

    application has failed to start because no appropriate graphics hardware ...

    • 4 Answers
  • Jerry

    Add file to native target programmatically via tuist/XcodeProj

    • 4 Answers
  • Ghulam Nabi
    Ghulam Nabi added an answer To resolve the NullPointerException, you need to identify the variable… March 15, 2023 at 8:25 am
  • Ghulam Nabi
    Ghulam Nabi added an answer You can replace the PnP code in your Azure Function… February 13, 2023 at 7:11 am
  • Ghulam Nabi
    Ghulam Nabi added an answer You can use the $match stage in the aggregate pipeline… February 10, 2023 at 6:20 am

Trending Tags

android c++ cypress flutter java javascript python selenium testng webdriver

Top Members

Robert

Robert

  • 3 Questions
  • 1k Points
Luci

Luci

  • 5 Questions
  • 1k Points
Kevin O Brien

Kevin O Brien

  • 2 Questions
  • 1k Points

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Softans

Softans is a social questions & Answers Engine which will help you establish your community and connect with other people.

About Us

  • Blog
  • Jobs
  • About Us
  • Meet The Team
  • Contact Us

Legal Stuff

Help

Follow

© 2021 Softans. All Rights Reserved
With Love by Softans.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.