我想检查输入元素的有效性。我可以检查输入中是否输入了错误或有效的电子邮件格式。像这样。
cy.get('#email_signup').type(validateEmail())
var email = "";
var possible = "[email protected]";
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');
根据您的期望,您需要两个外部功能来创建电子邮件和获取电子邮件的有效状态。然后,您需要在所有电子邮件中循环使用it挂钩。
//Create Emails
//Did a small modification so that you can decied the email length you need
const emails = (val) => {
var email = "";
var possible = "[email protected]";
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');
}
})
}
您创建电子邮件的方法很棒。但是请确保您添加单独的测试来检查特定和有效的方案,因为随机电子邮件可能无法涵盖所有情况
这就是测试的样子。
注意:正如我之前提到的。最好了解您的测试数据。因此,没有随机生成。尝试列出具有真实错误条件的有效,无效电子邮件方案,然后循环遍历它们。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句