<?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>Forem: Radu Popescu</title>
    <description>The latest articles on Forem by Radu Popescu (@radu_popescu).</description>
    <link>https://forem.com/radu_popescu</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%2F3013273%2F915bbf15-e9e2-4425-8fa6-b22a6f241efe.png</url>
      <title>Forem: Radu Popescu</title>
      <link>https://forem.com/radu_popescu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/radu_popescu"/>
    <language>en</language>
    <item>
      <title>Cypress Automation - Verify if your SSL Certificates are Valid</title>
      <dc:creator>Radu Popescu</dc:creator>
      <pubDate>Thu, 24 Apr 2025 05:57:08 +0000</pubDate>
      <link>https://forem.com/cypress/cypress-automation-verify-if-your-ssl-certificates-are-valid-2kij</link>
      <guid>https://forem.com/cypress/cypress-automation-verify-if-your-ssl-certificates-are-valid-2kij</guid>
      <description>&lt;p&gt;Recently I had to create a PoC where in the project's automation testing I needed to add a test that checks the validity of a &lt;a href="https://www.cloudflare.com/learning/ssl/what-is-an-ssl-certificate/" rel="noopener noreferrer"&gt;SSL Certificate&lt;/a&gt; for a specific API. I was not sure how this could be achieved and didn't find useful tutorials. I had to dig deeper and test a few external libs/modules until I found the right one. Let me show you what I did...&lt;/p&gt;

&lt;h2&gt;
  
  
  Install SSL-Checker
&lt;/h2&gt;

&lt;p&gt;SSL-Checker is a small Node.js module that checks the validity of an SSL certificate. We can use this with Cypress in order to create small tests....&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install ssl-checker&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a Cypress task
&lt;/h2&gt;

&lt;p&gt;The module above, being a Node.js one, will not run in a browser, so we need to address this. Cypress provides an event "task" which can be used in conjunction with the &lt;a href="https://docs.cypress.io/api/node-events/overview#cytask" rel="noopener noreferrer"&gt;cy.task()&lt;/a&gt; command in order to execute arbitrary Node.js code.&lt;/p&gt;

&lt;p&gt;The first step is to build a new task that will run the sslChecker method and return the response, which we'll use in our test for an assertion:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { defineConfig } from 'cypress';
// Import the SSL-Check module
import sslCheck from 'ssl-checker'; 

export default defineConfig({
  e2e: {
    // Other configurations can go here
    setupNodeEvents (on) {
      // Create the new task
      on('task', { 
          // Task name will be "getSSLValidity"
          getSSLValidity: (host) =&amp;gt; { 
            // The "host" param will be the URL we need to verify
            return sslCheck(host) 
          }
      })
    },
  },
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;*In order to be able to use 'import' for our module, we need to add this line to package.json : &lt;code&gt;"type": "module"&lt;/code&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the test
&lt;/h2&gt;

&lt;p&gt;Our Cypress test would be fairly simple. Using the above task we get the SSL information and use it in a simple assertion.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;describe('BlogDeIT.ro Tests', () =&amp;gt; {
    it('Should be more than 30 days before SSL Certificate expires', async () =&amp;gt; {            
        // Using the task "getSSLValidity" on "blogdeit.ro"
        cy.task('getSSLValidity', 'blogdeit.ro').then((cert) =&amp;gt; {
            // Check if the SSL Certificate is valid for more than 30 days
            expect(cert.daysRemaining).to.be.greaterThan(30);
        }); 
    });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The video version of this tutorial can be found &lt;a href="https://youtu.be/1WUtxkTc0NA" rel="noopener noreferrer"&gt;here&lt;/a&gt;, but please note that it is in my native language, Romanian.&lt;/p&gt;

</description>
      <category>qa</category>
      <category>automation</category>
      <category>ssl</category>
      <category>cypress</category>
    </item>
  </channel>
</rss>
