<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: rangana dilhara</title>
    <description>The latest articles on DEV Community by rangana dilhara (@rangana_dilhara_33a926cb5).</description>
    <link>https://dev.to/rangana_dilhara_33a926cb5</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2101957%2F9154828a-415b-4a5b-b6c0-68e85f69428b.jpg</url>
      <title>DEV Community: rangana dilhara</title>
      <link>https://dev.to/rangana_dilhara_33a926cb5</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rangana_dilhara_33a926cb5"/>
    <language>en</language>
    <item>
      <title>Cypress Tests failing in Headless mode for API execution</title>
      <dc:creator>rangana dilhara</dc:creator>
      <pubDate>Mon, 10 Mar 2025 05:11:51 +0000</pubDate>
      <link>https://dev.to/rangana_dilhara_33a926cb5/cypress-tests-failing-in-headless-mode-for-api-execution-5c12</link>
      <guid>https://dev.to/rangana_dilhara_33a926cb5/cypress-tests-failing-in-headless-mode-for-api-execution-5c12</guid>
      <description>&lt;p&gt;I am developing an API testing backend using Cypress with cy.request. I am aware that cy.request natively uses built-in promises (Cypress.Chainable), so there is no need to handle it with JavaScript promises.&lt;/p&gt;

&lt;p&gt;However, my requirement is to call test APIs with single calls, without chaining .then conditions.&lt;/p&gt;

&lt;p&gt;Currently, my code works properly in Cypress UI mode. But in headless mode, the second request fails with the error: "Cypress test was stopped while running this command." I suspect the issue is related to JavaScript promise handling and asynchronous behavior.&lt;/p&gt;

&lt;p&gt;Despite trying multiple solutions, I haven't been able to fix this issue. I need to make this work using a promise-based approach. Please help me or suggest a fix.&lt;/p&gt;

&lt;p&gt;headless run command : npx cypress run --headless --spec 'pathtofile'&lt;/p&gt;

&lt;p&gt;//following is the full working code with a public API &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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) =&amp;gt; {
      // Make the request using cy.request with the provided requestOptions
      cy.request(requestOptions).then((res) =&amp;gt; {
          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.body);
      })
  });
}


describe('API Tests', () =&amp;gt; {
    it('should make a POST request and handle the response', async () =&amp;gt; {
        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)=&amp;gt;{
    expect(res.body.status).to.eq(200);
   });
await  makeRequest(requestOptions).then((res :any)=&amp;gt;{
    expect(res.body.status).to.eq(200);
   });

    });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>javascript</category>
      <category>npm</category>
      <category>programming</category>
      <category>cypress</category>
    </item>
  </channel>
</rss>
