cypher_null
cypher_null

Reputation: 672

Cypress - triggering POST request opens URL of response

I have a test where I want to create a new document. In order to create the document I need to click on a "Save" button. Clicking "Save" triggers a POST request and opens the newly created document in a portlet on the same page.

However, when I do this in Cypress, clicking the save button opens the URL of the POST request in the browser. This page simply displays the response of the POST request.

{"response":"248"}

How is it possible that clicking the button manually opens the newly created document, while clicking the button in cypress opens a new URL?

This is the cypress code:

cy.visit(url_document_sets)
cy.get('.is_flatten').click({force:true}
cy.get('#TcDocumentFile_valid_from').type(current_date)
cy.get('.k-button').contains('Speichern') //Speichern = Save

Which is exactly what I do manually. I go the the URL, click 2 buttons, then click the Save button. Doing this in Cypress though will open the URL of the POST request https://**********/index.php?r=tc/documentSet/documentSet/saveFile&language=de in the browser.

Cypress does not throw an error, it just opens the URL.

I know this question is very hard to reproduce, so if there is anything I can provide to make the question clearer, please help me out.

Upvotes: 1

Views: 507

Answers (1)

Fody
Fody

Reputation: 32138

I don't know why Cypress doesn't just handle it, could make a guess but it would not help.

To handle it yourself, you could try to stub the call and manually set the document to what is returned.

I'm assuming when you say "opens the newly created document" it replaces the whole browser document and not just a part of the page.

const routeMatcher = {
  method: 'POST',
  url: '**/index.php?r=tc/documentSet/documentSet/saveFile&language=de',
  hostname: 'localhost'
}

cy.intercept(routeMatcher, (req) => {
  req.continue((res) => {
    console.log(res.body)      // double-check in console you have "document"
                               // you may have to get it from a property of body
    cy.state('document', res.body)
  })
}).as('newDoc')

cy.visit(url_document_sets)
cy.get('.is_flatten').click({force:true}
cy.get('#TcDocumentFile_valid_from').type(current_date)
cy.get('.k-button').contains('Speichern') //Speichern = Save

cy.wait('@newDoc')
// continue testing

If you have difficulty finding the new document property, please add res to the question and I'll take a look.

Upvotes: 1

Related Questions