How to Include a piece of Js code or Js file for Email Format Validation in Cypress

238 Views Asked by At

Update/Edit: I have a fixture file which contains firstname, lastname and email and it loops through beforeeach() and enters them in the respective fields. The verification i want to do is lets say a bad email is entered without '@','.com' or just '@' present or just '.com' these should fail when the field is verified.

const availablefixtures = [
{
    "name": "data3",
    "context": "1"
},
{
    "name": "data2",
    "context": "2"
}
]

data from fixture file is:

{
    "firstname": "Lambda",
    "lastname": "Jonas",
    "email": "[email protected]"
}

describe('demoniftyhappysuite', function () {
//loop through both the fixtues 
availablefixtures.forEach((afixture) => {
    describe(afixture.context, () => {
enter code here

cy.fixture(afixture.name).then(function (data) {
                this.data = data;
            })

Currently i am reading the email value from a data file in fixtures

it('demoniftyhappytest', {}, function () {
cy.get('input[name="personal.email"]').type(this.data.email, { 
log: false 
}).invoke('val').should('not.be.empty')

From: How can I check email validation in cypress the best way to do is have a set of emails in a array. (may be in a JSON) and loop through them and check for the email validity.

Eg:

{
"Email_1":"['[email protected]','valid']",
"Email_2":"['robotmail.com','InValid']",
}

Second Solution: Example Try the following code for email validation.

    <script type = "text/javascript">
    <!--
     function validateEmail() {
     var emailID = document.myForm.EMail.value;
     atpos = emailID.indexOf("@");
     dotpos = emailID.lastIndexOf(".");
     
     if (atpos < 1 || ( dotpos - atpos < 2 )) {
        alert("Please enter correct email ID")
        document.myForm.EMail.focus() ;
        return false;
     }
     return( true );
     }
    //-->
    </script>

I want it to be something simple like even if it verifies @symbol and .com in the email entered for time being.

1

There are 1 best solutions below

9
On

You can use a .forEach() to iterate through the array of emails and determine the behavior that way. As you've currently constructed your email json, we'll need to get the array from Object.values().

const emails = {
  "Email_1": ['[email protected]','valid'],
  "Email_2": ['robotmail.com','InValid'],
}

describe('tests', () => {
  it('validates the emails', () => {
    Object.values(emails).forEach((value) => {
      if (value[1] === 'valid') {
        // code to run if valid -- reference email address as `value[0]`
      } else if (value[1] === 'InValid') {
        // code to run if invalid -- reference email address as `value[0]
      }
    });
  });
});

If you wanted to change your object to be an array of arrays, it would be something like this:

const emails = [ ['[email protected]', 'valid'], ['foogmail.com', 'InValid'] ];
...
emails.forEach((email) => {
  if (email[1] === 'valid') {
    ...
  } else if (email[1] === 'InValid') {
    ...
  }
// reference email value as `email[0]`
})

Or if you wanted to make it an array of objects

const emails = [ { email: '[email protected]', isValid: true }, { email: 'foogmail.com', isValid: false } ];
emails.forEach((value) => {
  if (value.isValid) {
   ...
  } else {
   ...
  }
})
// email address is referenced by `value.email`