如何在cypress中检查电子邮件验证

如何在cypress中检查电子邮件验证,cypress,e2e-testing,Cypress,E2e Testing,我想检查输入元素的有效性。我可以检查输入的电子邮件格式是否错误或有效。 像这样 cy.get('#email_signup').type(validateEmail()) var email = ""; var possible = "abcd@.gh"; for (var i = 0; i < 10; i++) email += possible.charA

我想检查输入元素的有效性。我可以检查输入的电子邮件格式是否错误或有效。 像这样

 cy.get('#email_signup').type(validateEmail())
         var email = "";
            var possible = "abcd@.gh";
            for (var i = 0; i < 10; i++)
            email += possible.charAt(Math.floor(Math.random() * possible.length));
            return email;
        }

        cy.get('.nextBtn').click();
        cy.get('.error-message').should('be.visible');
cy.get('#email_signup')。键入(validateEmail())
var email=“”;
变量可能=”abcd@.gh";
对于(变量i=0;i<10;i++)
email+=可能的.charAt(Math.floor(Math.random()*可能的.length));
回复邮件;
}
cy.get('.nextBtn')。单击();
cy.get('.error message')。应该('be.visible');

根据您的期望,您需要两个外部功能来创建电子邮件和获取电子邮件的有效状态。然后,您需要在所有电子邮件中循环it钩子

//Create Emails
//Did a small modification so that you can decied the email length you need
const emails = (val) => {
var email = "";
var possible = "abcd@.gh";
for (var i = 0; i < val; i++){
email += possible.charAt(Math.floor(Math.random() * possible.length));}
return email;
}


//validate emails
//I have used a general Regex here, use the regex you have used in your website insted

const validateEmail = (email) => {
var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
return re.test(email);
}

//Test Cases (I have added 10 loops so it will create 10 test cases)
//Change the test case count as much as you need
for (let index = 0; index < 10; index++) {
const TestEmail = emails(10)
const EmailState = validateEmail(TestEmail)
it("EmailTest -"+ TestEmail +" - " + EmailState,() => {
cy.get('#email_signup').type(TestEmail)
cy.get('.nextBtn').click();
    if(!EmailState){
         cy.get('.error-message').should('be.visible');
    }else{
         cy.get('.error-message').should('not.be.visible');
    }
})
}
//创建电子邮件
//做了一个小的修改,这样你就可以决定你需要的电子邮件长度
常量电子邮件=(val)=>{
var email=“”;
变量可能=”abcd@.gh";
对于(变量i=0;i{
变量re=/^\w+([\.-]?\w+*@\w+([\.-]?\w+*(\.\w{2,3})+$/;
返回重新测试(电子邮件);
}
//测试用例(我添加了10个循环,因此它将创建10个测试用例)
//根据需要更改测试用例计数
对于(让索引=0;索引<10;索引++){
const TestEmail=电子邮件(10)
const EmailState=validateEmail(TestEmail)
它(“EmailTest-”+TestEmail+“-”+EmailState,()=>{
cy.get(“#email_signup”).type(TestEmail)
cy.get('.nextBtn')。单击();
如果(!EmailState){
cy.get('.error message')。应该('be.visible');
}否则{
cy.get('.error message')。应该('not.be.visible');
}
})
}
你创建电子邮件的方法真是太棒了。但请确保您添加了一个单独的测试来检查特定和有效的场景,因为随机电子邮件可能无法涵盖所有场景

这就是测试的样子


注:正如我前面提到的。了解测试数据总是更好的。因此,没有随机生成。尝试创建一个包含真-假条件的有效、无效电子邮件场景列表,并在其中循环。

根据您的预期操作,您需要两个外部函数来创建电子邮件和获取电子邮件的有效状态。然后,您需要在所有电子邮件中循环it钩子

//Create Emails
//Did a small modification so that you can decied the email length you need
const emails = (val) => {
var email = "";
var possible = "abcd@.gh";
for (var i = 0; i < val; i++){
email += possible.charAt(Math.floor(Math.random() * possible.length));}
return email;
}


//validate emails
//I have used a general Regex here, use the regex you have used in your website insted

const validateEmail = (email) => {
var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
return re.test(email);
}

//Test Cases (I have added 10 loops so it will create 10 test cases)
//Change the test case count as much as you need
for (let index = 0; index < 10; index++) {
const TestEmail = emails(10)
const EmailState = validateEmail(TestEmail)
it("EmailTest -"+ TestEmail +" - " + EmailState,() => {
cy.get('#email_signup').type(TestEmail)
cy.get('.nextBtn').click();
    if(!EmailState){
         cy.get('.error-message').should('be.visible');
    }else{
         cy.get('.error-message').should('not.be.visible');
    }
})
}
//创建电子邮件
//做了一个小的修改,这样你就可以决定你需要的电子邮件长度
常量电子邮件=(val)=>{
var email=“”;
变量可能=”abcd@.gh";
对于(变量i=0;i{
变量re=/^\w+([\.-]?\w+*@\w+([\.-]?\w+*(\.\w{2,3})+$/;
返回重新测试(电子邮件);
}
//测试用例(我添加了10个循环,因此它将创建10个测试用例)
//根据需要更改测试用例计数
对于(让索引=0;索引<10;索引++){
const TestEmail=电子邮件(10)
const EmailState=validateEmail(TestEmail)
它(“EmailTest-”+TestEmail+“-”+EmailState,()=>{
cy.get(“#email_signup”).type(TestEmail)
cy.get('.nextBtn')。单击();
如果(!EmailState){
cy.get('.error message')。应该('be.visible');
}否则{
cy.get('.error message')。应该('not.be.visible');
}
})
}
你创建电子邮件的方法真是太棒了。但请确保您添加了一个单独的测试来检查特定和有效的场景,因为随机电子邮件可能无法涵盖所有场景

这就是测试的样子


注:正如我前面提到的。了解测试数据总是更好的。因此,没有随机生成。尝试创建一个包含真、假条件的有效、无效电子邮件场景列表,并循环查看它们。

首先,生成随机电子邮件是好的,但最好的方法是将一组电子邮件放在一个数组中。(可能在JSON中)并循环浏览它们并检查电子邮件的有效性

例如:


因为这样你就知道你正在测试的电子邮件条件了。但是如果你想使用随机电子邮件生成方法。我完全同意穆迪莎·佩雷拉的回答。它工作得很好。

首先,生成随机电子邮件很好,但最好的方法是将一组电子邮件放在一个数组中。(可能在JSON中)并循环浏览它们并检查电子邮件的有效性

例如:


因为这样你就知道你正在测试的电子邮件条件了。但是如果你想使用随机电子邮件生成方法。我完全同意穆迪莎·佩雷拉的回答。它工作得很好。

你的目的是什么?您是要验证是否显示了错误消息,还是只想验证是否插入了正确的格式?我不明白你想从代码中做什么?我想检查,是否粘贴了有效的电子邮件。如果粘贴了错误的格式,则应显示错误消息。如果粘贴了正确格式的电子邮件,则错误消息不应可见。为什么要尝试随机生成电子邮件?你不认为你会失去一些机会吗?在cy.get(“#email_signup”).type(validate email())行之后,您的步骤不清楚。您是否错误地添加了函数的步骤?您的目的是什么?您是要验证是否显示了错误消息,还是只想验证是否插入了正确的格式?我不明白你想从代码中做什么?我想检查,是否粘贴了有效的电子邮件。如果粘贴了错误的格式,则应显示错误消息。如果粘贴了正确格式的电子邮件,则错误消息不应可见。为什么要尝试随机生成电子邮件?你不认为你会失去一些机会吗?在cy.get(“#email_signup”).type(validate email())行之后,您的步骤不清楚。您是否错误地添加了函数的步骤?