<?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: Gurupreeth Karnik</title>
    <description>The latest articles on DEV Community by Gurupreeth Karnik (@gurupreeth6).</description>
    <link>https://dev.to/gurupreeth6</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%2F1051314%2F9a8209f2-377d-4736-baa5-f53420361c3c.png</url>
      <title>DEV Community: Gurupreeth Karnik</title>
      <link>https://dev.to/gurupreeth6</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gurupreeth6"/>
    <language>en</language>
    <item>
      <title>HTML form validation using Cypress</title>
      <dc:creator>Gurupreeth Karnik</dc:creator>
      <pubDate>Mon, 03 Jul 2023 02:20:47 +0000</pubDate>
      <link>https://dev.to/gurupreeth6/html-form-validation-using-cypress-2ggd</link>
      <guid>https://dev.to/gurupreeth6/html-form-validation-using-cypress-2ggd</guid>
      <description>&lt;p&gt;This blog post will discuss what is HTML5 form validation is, and give a sneak peek as to how Cypress can be used to automate the same.&lt;/p&gt;

&lt;p&gt;Before we dive into how to handle html form validation using cypress, let us understand what is HTML form validation.&lt;/p&gt;

&lt;p&gt;HTML5 supports client side form validation, which means the information entered by the user gets validated in the client side without being sent to server.&lt;br&gt;
Below is the sample html inbuilt form validation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form action="/action_page.php" method="post"&amp;gt;
    &amp;lt;div&amp;gt;   
        &amp;lt;label for="fname"&amp;gt;FirstName&amp;lt;/label&amp;gt;
        &amp;lt;input id="fname" type="text" name="fname" required placeholder="First Name"/&amp;gt;
    &amp;lt;/div&amp;gt;

    &amp;lt;div&amp;gt;   
        &amp;lt;label for="lname"&amp;gt;LastName&amp;lt;/label&amp;gt;
        &amp;lt;input id="lname" type="text" name="lname" required placeholder="Last Name"/&amp;gt;
    &amp;lt;/div&amp;gt;

    &amp;lt;div&amp;gt;   
        &amp;lt;label for="email"&amp;gt;Email&amp;lt;/label&amp;gt;
        &amp;lt;input id="email" type="email" name="email" required placeholder="Email"/&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;div&amp;gt;
        &amp;lt;input type="submit" value="Submit"&amp;gt;
    &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EsuKYDOA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dupucpvpgfcy01gq8gir.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EsuKYDOA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dupucpvpgfcy01gq8gir.png" alt="Sample HTML form" width="800" height="307"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let us see how we can handle the above scenario using Cypress. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The inbuilt html validation errors are shown by the browsers and are not part of the DOM. There for we cannot check them from Cypress. We can make use of CSS pseudo-class defined by html standards for finding invalid and valid input fields&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Check Number of fields which are invalid
&lt;/h3&gt;

&lt;p&gt;When user submits the form without filling any of the input fields or by keeping them blank. Since all 3 fields are marked as required in the above example, the invalid field count should be 3. Let us check that using Cypress.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;it('Verify html form validation', () =&amp;gt; {
    cy.get('input[type="submit"]').click();
    cy.get('input:invalid').should('have.length', 3);
  })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jOzqQxU_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ctl7rrdcojxnjq5nr9nc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jOzqQxU_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ctl7rrdcojxnjq5nr9nc.png" alt="Validating the number of fields having the invalid pseudo-class" width="800" height="339"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All 3 input fields has a pseudo-class &lt;em&gt;:invalid&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How to Check Validation Messages
&lt;/h3&gt;

&lt;p&gt;In case of validation error each element will have a property called &lt;em&gt;validationMessage&lt;/em&gt;. Let's make use of that to validate the error messages.&lt;/p&gt;

&lt;p&gt;This time lets fill some data into the fields&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;it('Verify html form validation', () =&amp;gt; {

    //Enter valid format into the first name last name and invalid email format
    cy.get('#fname').type(123)
    cy.get('#lname').type('%^&amp;amp;^');
    cy.get('#email').type('test@');

    //Click on submit and check the number of invalid fields
    cy.get('input[type="submit"]').click();
    cy.get('input:invalid').should('have.length', 1);

    //Validate the error messages using validationMessage property   
    cy.get('#email').invoke('prop', 'validationMessage')
        .should('equal', "Please enter a part following '@'. 'test@' is incomplete.");
  })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7QMDK-zX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qxadxza7ykvpip3318x9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7QMDK-zX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qxadxza7ykvpip3318x9.png" alt="Validating the error message using validateMessage property" width="800" height="419"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that's it. As you can see, it is pretty simple to automate html form-validation with Cypress. Hopefully this guide helped you get on the right track with writing tests for your website. Make sure to follow Cypress &lt;a class="mentioned-user" href="https://dev.to/cypress_io"&gt;@cypress_io&lt;/a&gt; and check out their list of courses at &lt;a href="https://learn.cypress.io/"&gt;Cypress Learn&lt;/a&gt; for more tutorials and guides like this one!&lt;/p&gt;

</description>
      <category>cypress</category>
      <category>typescript</category>
      <category>html</category>
      <category>formvalidation</category>
    </item>
  </channel>
</rss>
