auth.spec.js
5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* @Date: 2026-01-28 22:00:00
* @Description: 认证相关 E2E 测试示例
*/
import { test, expect } from '@playwright/test'
import { login, logout, isLoggedIn, quickLogin, TEST_ACCOUNT } from './helpers/auth'
test.describe('用户认证', () => {
test.beforeEach(async ({ page }) => {
// 每个测试前确保是登出状态
await logout(page)
})
test('应该成功登录', async ({ page }) => {
// 1. 访问登录页
await page.goto('/login')
// 2. 检查登录表单元素
const phoneInput = page.locator('input[name="phone"]')
await expect(phoneInput).toBeVisible()
// 3. 执行登录
await login(page)
// 4. 验证登录成功
await expect(page).toHaveURL(/\/(home|index)?/)
// 5. 验证用户信息已保存
const loggedIn = await isLoggedIn(page)
expect(loggedIn).toBe(true)
})
test('应该显示登录错误信息(手机号格式错误)', async ({ page }) => {
await page.goto('/login')
// 输入错误格式的手机号
await page.fill('input[name="phone"]', '12345')
// 点击发送验证码
await page.click('button:has-text("发送验证码")')
// 验证错误提示
const errorToast = page.locator('.van-toast--fail').or(page.locator('.van-toast--error'))
// 等待错误提示出现
await expect(errorToast).toBeVisible({ timeout: 3000 })
})
test('应该成功登出', async ({ page }) => {
// 先登录
await login(page)
// 验证已登录
let loggedIn = await isLoggedIn(page)
expect(loggedIn).toBe(true)
// 执行登出
await logout(page)
// 验证已登出
loggedIn = await isLoggedIn(page)
expect(loggedIn).toBe(false)
// 验证跳转到登录页
await expect(page).toHaveURL(/\/login/)
})
})
test.describe('需要登录的功能', () => {
test('访问受保护页面应跳转到登录页', async ({ page }) => {
// 直接访问需要登录的页面
await page.goto('/profile')
// 验证跳转到登录页
await expect(page).toHaveURL(/\/login/)
})
test('登录后可以访问个人中心', async ({ page }) => {
// 先登录
await login(page)
// 访问个人中心
await page.goto('/profile')
// 验证页面加载成功
await expect(page).toHaveURL(/\/profile/)
// 检查页面元素
const profileContent = page.locator('.profile').or(page.locator('[class*="profile"]'))
await expect(profileContent).toBeVisible()
})
test('登录后可以查看课程学习进度', async ({ page }) => {
// 登录
await login(page)
// 访问学习进度页面
await page.goto('/study/progress')
// 验证页面内容
const progressContent = page.locator('.study-progress').or(page.locator('[class*="progress"]'))
await expect(progressContent).toBeVisible({ timeout: 5000 })
})
})
test.describe('购买流程(需要登录)', () => {
test('登录后可以购买课程', async ({ page }) => {
// 登录
await login(page)
// 访问课程详情页(假设课程ID为123)
await page.goto('/courses/123')
// 点击购买按钮
const buyButton = page
.locator('button:has-text("购买")')
.or(page.locator('button:has-text("立即购买")'))
// 等待按钮可点击
await buyButton.waitFor({ state: 'visible', timeout: 5000 })
await buyButton.click()
// 验证跳转到结算页或支付页
await expect(page).toHaveURL(/\/(checkout|payment)/)
// 检查结算页内容
const checkoutContent = page.locator('.checkout').or(page.locator('[class*="checkout"]'))
await expect(checkoutContent).toBeVisible()
})
test('未登录购买课程应跳转到登录页', async ({ page }) => {
// 确保未登录
await logout(page)
// 访问课程详情页
await page.goto('/courses/123')
// 点击购买按钮
const buyButton = page
.locator('button:has-text("购买")')
.or(page.locator('button:has-text("立即购买")'))
await buyButton.click()
// 验证跳转到登录页
await expect(page).toHaveURL(/\/login/)
})
})
test.describe('打卡功能(需要登录)', () => {
test('登录后可以提交打卡', async ({ page }) => {
// 登录
await login(page)
// 访问打卡页面
await page.goto('/checkin')
// 填写打卡内容
const textarea = page.locator('textarea').or(page.locator('input[type="text"]'))
await textarea.fill('今天的打卡内容')
// 点击提交按钮
const submitButton = page
.locator('button:has-text("提交")')
.or(page.locator('button:has-text("打卡")'))
await submitButton.click()
// 验证提交成功(显示成功提示)
const successToast = page.locator('.van-toast--success')
await expect(successToast).toBeVisible({ timeout: 3000 })
})
test('未登录打卡应跳转到登录页', async ({ page }) => {
// 确保未登录
await logout(page)
// 访问打卡页面
await page.goto('/checkin')
// 验证跳转到登录页
await expect(page).toHaveURL(/\/login/)
})
})
test.describe('使用快速登录', () => {
test('快速登录跳过输入流程', async ({ page }) => {
// 使用快速登录(首次会执行正常登录获取 token)
await quickLogin(page)
// 访问需要登录的页面
await page.goto('/profile')
// 验证页面正常访问
await expect(page).toHaveURL(/\/profile/)
})
test('复用 token 多次测试', async ({ page }) => {
// 第一次登录获取 token
await login(page)
// 获取 token
const token = await page.evaluate(() => {
const userInfo = localStorage.getItem('user_info')
return JSON.parse(userInfo).token
})
// 登出
await logout(page)
// 使用 token 快速登录
await quickLogin(page, token)
// 验证登录状态
const loggedIn = await isLoggedIn(page)
expect(loggedIn).toBe(true)
})
})