Selenium Tester
Selenium Tester

Reputation: 31

Cypress.io: How to read XML File and assign the contents to cy.request(body)

For one of my web service testing, I need to read an xml file and assign the contents of the same to cy.request body. How can I achieve this? I tried the below method and was not able to successfully pass the XML to the body. Please let me know.

eg:

cy.readFile('Desktop/Testing/W1.xml')
.then(text1 => {
console.log(text1);
  cy
  .request({
       url: 'my URL',
       method: 'POST',
       body: {text1},
       headers: {
         'Authorization':'Basic ........',
         'content-type': 'application/......-v1.0+xml',
         'Accept':'application/...v1.0+json,application/....-v1.0+json'
       }
   })
.then((response) => {
    assert.equal(response.status, 200, "status was 200");
    cy.log("Response Body",response.body);
    console.log("Response Body",response.body);
    })
 })

Upvotes: 3

Views: 6239

Answers (1)

Przemyslaw Jan Beigert
Przemyslaw Jan Beigert

Reputation: 2486

I suggest something like this:

Prepare function for fetching XML

function fetchXML(text) {
  return cy.request({
    url: 'my URL',
    method: 'POST',
    body: text,
    headers: { ... }
  })
}

Then call readFile and pass to promise callback result

cy
  .readFile('Desktop/Testing/W1.xml')
  .then(text => fetchXML(text)) // or just .then(fetchXML)
  .then(responseFromXML => { ... })

and i second callback you can use response from XML fetch

Link to docs about Cypress.Promise LINK

Upvotes: 2

Related Questions