Reputation: 31
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
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