// 生成随机手机号 const generatePhone = () => { const timestamp = Date.now().toString().slice(-8); return `138${timestamp}`; }; describe('用户认证测试', () => { let testPhone; let testPassword = 'Test123456'; beforeEach(() => { testPhone = generatePhone(); cy.visit('/'); cy.wait(1000); // 等待页面加载 }); it('应该能够成功注册新用户', () => { // 点击注册按钮 - 使用class选择器 cy.get('.btn-register').click(); // 等待注册模态框出现 cy.get('.auth-modal', { timeout: 10000 }).should('be.visible'); // 填写注册表单 cy.get('input[placeholder*="手机号"]').type(testPhone); cy.get('input[placeholder*="昵称"]').type('E2E测试用户'); cy.get('input[placeholder*="设置密码"]').type(testPassword); cy.get('input[placeholder*="确认密码"]').type(testPassword); // 点击提交注册 cy.get('.auth-modal .auth-submit-btn').click(); // 验证注册成功 - 等待页面刷新并检查登录状态 // 注册成功后会自动刷新页面,所以等待头像出现即可 cy.get('.ant-avatar', { timeout: 15000 }).should('exist'); cy.log(`✅ 注册测试通过 - 手机号: ${testPhone}`); }); it('应该能够使用已注册账号登录', () => { // 先注册 cy.get('.btn-register').click(); cy.get('.auth-modal').should('be.visible'); cy.get('input[placeholder*="手机号"]').type(testPhone); cy.get('input[placeholder*="设置密码"]').type(testPassword); cy.get('input[placeholder*="确认密码"]').type(testPassword); cy.get('.auth-modal .auth-submit-btn').click(); // 等待注册成功和页面刷新 - 等待头像出现 cy.get('.ant-avatar', { timeout: 15000 }).should('exist'); // 退出登录 cy.get('.ant-avatar').click(); cy.contains('退出登录').click(); cy.wait(2000); // 重新登录 cy.visit('/'); cy.wait(1000); cy.get('.btn-login').click(); cy.get('.auth-modal').should('be.visible'); cy.get('input[placeholder*="手机号"]').type(testPhone); cy.get('input[placeholder*="密码"]').first().type(testPassword); cy.get('.auth-modal .auth-submit-btn').click(); // 验证登录成功 - 等待页面刷新并检查登录状态 cy.get('.ant-avatar', { timeout: 15000 }).should('exist'); cy.log(`✅ 登录测试通过 - 手机号: ${testPhone}`); }); it('应该验证手机号格式', () => { cy.get('.btn-register').click(); cy.get('.auth-modal').should('be.visible'); // 输入无效手机号 cy.get('input[placeholder*="手机号"]').type('123456'); cy.get('input[placeholder*="设置密码"]').type(testPassword); cy.get('input[placeholder*="确认密码"]').type(testPassword); // 尝试提交 cy.get('.auth-modal .auth-submit-btn').click(); // 应该显示错误提示 cy.contains('请输入正确的11位手机号').should('be.visible'); cy.log('✅ 手机号验证测试通过'); }); it('应该验证密码确认', () => { cy.get('.btn-register').click(); cy.get('.auth-modal').should('be.visible'); // 输入不匹配的密码 cy.get('input[placeholder*="手机号"]').type(testPhone); cy.get('input[placeholder*="设置密码"]').type(testPassword); cy.get('input[placeholder*="确认密码"]').type('DifferentPassword123'); // 尝试提交 cy.get('.auth-modal .auth-submit-btn').click(); // 应该显示密码不一致错误 cy.contains('两次输入的密码不一致').should('be.visible'); cy.log('✅ 密码确认测试通过'); }); });