Cypress im Kopflosenmodus in der Lage, Versprechen nicht durch Warten auf Anrufe zu lösenJavaScript

Javascript-Forum
Anonymous
 Cypress im Kopflosenmodus in der Lage, Versprechen nicht durch Warten auf Anrufe zu lösen

Post by Anonymous »

Ich entwickle ein API -Test -Backend mit Cypress mit Cy.Request. Ich bin mir bewusst, dass Cy.Request nativ integrierte Versprechen verwendet (Cypress.chainable). Daher müssen sie nicht mit JavaScript-Versprechen behandeln. Im kopflosen Modus schlägt die zweite Anforderung jedoch mit dem Fehler fehl: "Der Zypress -Test wurde gestoppt, während dieses Befehl ausgeführt wurde." Ich vermute, dass das Problem mit der Handhabung von JavaScript -Versprechen und dem asynchronen Verhalten zusammenhängt. Es funktioniert einfach beim Ausführen des Tests im offenen Modus und der Verwendung von Chrome als Browser.

Code: Select all

const newPet= {
"name": "jkysssssss",
"data": {
"year": 2017,
"price": 4589,
"CPU model": "Intel eeeeeee",
"Hard disk size": "1 TB"
}
}

export function makeRequest(requestOptions :any) {
return new Promise((resolve, reject) => {
// Make the request using cy.request with the provided requestOptions
cy.request(requestOptions).then((res) => {
if (!res || !res.body) {
return reject(new Error('No response body returned from request.'));
}

// Ensure response body is an object
if (typeof res.body !== 'object' || res.body === null) {
res.body = { data: res.body };
}

// Add status to the response
res.body.status = res.status;

// Log the response
cy.log(JSON.stringify(res.body));

// Resolve the promise with the response body
resolve(res);
})
});
}

describe('API Tests', () => {
it('should make a POST request and handle the response', async () => {
const requestOptions = {
method: 'POST',  // HTTP method
url: 'https://api.restful-api.dev/objects',  // API URL
headers: {
'Content-Type': 'application/json',
},
body: newPet,
};

//request calls
await  makeRequest(requestOptions).then((res :any)=>{
expect(res.body.status).to.eq(200);
});
await  makeRequest(requestOptions).then((res :any)=>{
expect(res.body.status).to.eq(200);
});

});
});

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post