@be-link/smart-test 1.0.1-beta.0 → 1.0.1-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +269 -34
- package/bin/smart-test.js +15 -0
- package/dist/cli/commands/generate.d.ts +16 -0
- package/dist/cli/commands/generate.d.ts.map +1 -0
- package/dist/cli/commands/init.d.ts +5 -0
- package/dist/cli/commands/init.d.ts.map +1 -0
- package/dist/cli/index.d.ts +10 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +618 -0
- package/dist/config/config-loader.d.ts +35 -0
- package/dist/config/config-loader.d.ts.map +1 -0
- package/dist/config/config-schema.d.ts +85 -0
- package/dist/config/config-schema.d.ts.map +1 -0
- package/dist/core/ai-assistant.d.ts +0 -1
- package/dist/core/ai-assistant.d.ts.map +1 -1
- package/dist/core/config.d.ts +0 -1
- package/dist/core/config.d.ts.map +1 -1
- package/dist/core/types.d.ts +0 -15
- package/dist/core/types.d.ts.map +1 -1
- package/dist/index.esm.js +0 -29
- package/dist/index.js +0 -49
- package/dist/utils/error-handler.d.ts +41 -0
- package/dist/utils/error-handler.d.ts.map +1 -0
- package/dist/utils/logger.d.ts +88 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/screenshot.d.ts +0 -9
- package/dist/utils/screenshot.d.ts.map +1 -1
- package/package.json +18 -3
package/README.md
CHANGED
|
@@ -6,17 +6,36 @@
|
|
|
6
6
|
|
|
7
7
|
## ✨ 特性
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
|
|
9
|
+
### 🤖 AI 视觉检查
|
|
10
|
+
|
|
11
|
+
- 基于通义千问等视觉模型,智能检查页面是否符合预期
|
|
12
|
+
- 支持自然语言描述测试预期
|
|
13
|
+
- 自动识别页面问题并给出详细反馈
|
|
14
|
+
|
|
15
|
+
### 🎯 零代码测试生成(开发中)
|
|
16
|
+
|
|
17
|
+
- **CLI 工具**:一条命令自动生成测试代码
|
|
18
|
+
- **录制模式**:访问页面自动录制接口和交互
|
|
19
|
+
- **代码分析**:扫描项目代码自动生成 Mock 数据
|
|
20
|
+
- **AI 生成**:基于自然语言描述生成完整测试
|
|
21
|
+
|
|
22
|
+
### 🔧 灵活易用
|
|
23
|
+
|
|
24
|
+
- 与 Playwright 无缝集成,零侵入设计
|
|
25
|
+
- 支持全局配置和单次调用配置
|
|
26
|
+
- 优雅降级:无 API Key 时自动跳过
|
|
27
|
+
- 完整的 TypeScript 类型支持
|
|
28
|
+
|
|
29
|
+
### 📸 自动化
|
|
30
|
+
|
|
31
|
+
- 自动截图并保存
|
|
32
|
+
- 自动生成 Mock 数据
|
|
33
|
+
- 自动生成测试用例
|
|
15
34
|
|
|
16
35
|
## 📦 安装
|
|
17
36
|
|
|
18
37
|
```bash
|
|
19
|
-
# 使用 pnpm
|
|
38
|
+
# 使用 pnpm(推荐)
|
|
20
39
|
pnpm add -D @be-link/smart-test
|
|
21
40
|
|
|
22
41
|
# 使用 npm
|
|
@@ -28,7 +47,56 @@ yarn add -D @be-link/smart-test
|
|
|
28
47
|
|
|
29
48
|
## 🚀 快速开始
|
|
30
49
|
|
|
31
|
-
###
|
|
50
|
+
### 方式一:使用 CLI 工具(推荐)⭐
|
|
51
|
+
|
|
52
|
+
#### 1. 初始化配置
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
npx smart-test init
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
交互式配置向导会引导你完成设置:
|
|
59
|
+
|
|
60
|
+
```
|
|
61
|
+
━━━ 初始化 Smart Test 配置 ━━━
|
|
62
|
+
|
|
63
|
+
? 请输入 AI API Key: sk-xxxxx
|
|
64
|
+
? 选择 AI 模型: 通义千问 (qwen3-vl-plus)
|
|
65
|
+
? 测试文件目录: ./tests/e2e
|
|
66
|
+
? Helpers 目录: ./tests/helpers
|
|
67
|
+
? Playwright Base URL: http://localhost:8080
|
|
68
|
+
? 文件命名风格: kebab-case
|
|
69
|
+
|
|
70
|
+
✓ 配置文件已创建: .smarttestrc.json
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
#### 2. 生成测试代码(开发中)
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
# 录制模式 - 访问页面自动录制
|
|
77
|
+
npx smart-test record --url http://localhost:8080/coupon
|
|
78
|
+
|
|
79
|
+
# 代码分析模式 - 扫描页面代码
|
|
80
|
+
npx smart-test generate --page src/pages/coupon/index.tsx
|
|
81
|
+
|
|
82
|
+
# 描述生成模式 - 基于自然语言
|
|
83
|
+
npx smart-test generate --desc "优惠券列表页,有可用券和已用券两个 tab"
|
|
84
|
+
|
|
85
|
+
# 交互式模式 - 引导式生成
|
|
86
|
+
npx smart-test generate --interactive
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
#### 3. 运行测试
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
npx playwright test
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
### 方式二:手动编程
|
|
98
|
+
|
|
99
|
+
#### 1. 配置 API Key
|
|
32
100
|
|
|
33
101
|
在项目根目录创建 `.env` 文件:
|
|
34
102
|
|
|
@@ -36,7 +104,7 @@ yarn add -D @be-link/smart-test
|
|
|
36
104
|
AI_API_KEY=sk-your-api-key-here
|
|
37
105
|
```
|
|
38
106
|
|
|
39
|
-
|
|
107
|
+
#### 2. 初始化配置
|
|
40
108
|
|
|
41
109
|
在 `playwright.config.ts` 中配置:
|
|
42
110
|
|
|
@@ -55,7 +123,7 @@ export default defineConfig({
|
|
|
55
123
|
});
|
|
56
124
|
```
|
|
57
125
|
|
|
58
|
-
|
|
126
|
+
#### 3. 在测试中使用
|
|
59
127
|
|
|
60
128
|
```typescript
|
|
61
129
|
import { test, expect } from '@playwright/test';
|
|
@@ -78,7 +146,95 @@ test.describe('优惠券页面', () => {
|
|
|
78
146
|
});
|
|
79
147
|
```
|
|
80
148
|
|
|
81
|
-
## 📖
|
|
149
|
+
## 📖 CLI 命令参考
|
|
150
|
+
|
|
151
|
+
### `smart-test init`
|
|
152
|
+
|
|
153
|
+
初始化配置文件,交互式设置 AI API Key、模型、输出目录等。
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
smart-test init
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### `smart-test generate` (开发中)
|
|
160
|
+
|
|
161
|
+
生成测试代码。
|
|
162
|
+
|
|
163
|
+
```bash
|
|
164
|
+
# 基础用法
|
|
165
|
+
smart-test generate
|
|
166
|
+
|
|
167
|
+
# 指定页面(代码分析模式)
|
|
168
|
+
smart-test generate --page <path>
|
|
169
|
+
|
|
170
|
+
# 指定 URL(录制模式)
|
|
171
|
+
smart-test generate --url <url>
|
|
172
|
+
|
|
173
|
+
# 基于描述(自然语言模式)
|
|
174
|
+
smart-test generate --desc <description>
|
|
175
|
+
|
|
176
|
+
# 交互式模式
|
|
177
|
+
smart-test generate --interactive
|
|
178
|
+
|
|
179
|
+
# 指定输出目录
|
|
180
|
+
smart-test generate --page <path> --output tests/custom
|
|
181
|
+
|
|
182
|
+
# 预览模式(不写入文件)
|
|
183
|
+
smart-test generate --page <path> --dry-run
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
**选项:**
|
|
187
|
+
|
|
188
|
+
| 选项 | 简写 | 说明 |
|
|
189
|
+
| ---------------------- | ---- | ---------------------------- |
|
|
190
|
+
| `--page <path>` | `-p` | 页面文件路径(代码分析模式) |
|
|
191
|
+
| `--url <url>` | `-u` | 页面 URL(录制模式) |
|
|
192
|
+
| `--desc <description>` | `-d` | 页面描述(自然语言模式) |
|
|
193
|
+
| `--output <dir>` | `-o` | 输出目录 |
|
|
194
|
+
| `--interactive` | `-i` | 交互式模式 |
|
|
195
|
+
| `--dry-run` | | 预览模式,不写入文件 |
|
|
196
|
+
|
|
197
|
+
### `smart-test record` (开发中)
|
|
198
|
+
|
|
199
|
+
录制模式的快捷命令(`generate --url` 的别名)。
|
|
200
|
+
|
|
201
|
+
```bash
|
|
202
|
+
# 基础录制
|
|
203
|
+
smart-test record --url http://localhost:8080/coupon
|
|
204
|
+
|
|
205
|
+
# 手动操作模式
|
|
206
|
+
smart-test record --url http://localhost:8080/coupon --manual
|
|
207
|
+
|
|
208
|
+
# 交互式引导
|
|
209
|
+
smart-test record --url http://localhost:8080/coupon --interactive
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
**选项:**
|
|
213
|
+
|
|
214
|
+
| 选项 | 简写 | 说明 |
|
|
215
|
+
| ---------------- | ---- | ------------------------ |
|
|
216
|
+
| `--url <url>` | `-u` | 要录制的页面 URL(必需) |
|
|
217
|
+
| `--output <dir>` | `-o` | 输出目录 |
|
|
218
|
+
| `--manual` | | 手动操作模式 |
|
|
219
|
+
| `--interactive` | | 交互式引导 |
|
|
220
|
+
|
|
221
|
+
### `smart-test config`
|
|
222
|
+
|
|
223
|
+
查看当前配置。
|
|
224
|
+
|
|
225
|
+
```bash
|
|
226
|
+
smart-test config
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### 全局选项
|
|
230
|
+
|
|
231
|
+
| 选项 | 简写 | 说明 |
|
|
232
|
+
| ----------- | ---- | -------------------- |
|
|
233
|
+
| `--version` | `-V` | 显示版本号 |
|
|
234
|
+
| `--verbose` | `-v` | 详细输出(调试模式) |
|
|
235
|
+
| `--help` | `-h` | 显示帮助信息 |
|
|
236
|
+
|
|
237
|
+
## 📖 编程 API 文档
|
|
82
238
|
|
|
83
239
|
### `configure(config)`
|
|
84
240
|
|
|
@@ -91,8 +247,6 @@ configure({
|
|
|
91
247
|
apiKey: 'sk-xxx', // AI API 密钥
|
|
92
248
|
baseURL: 'https://...', // AI 服务地址(可选)
|
|
93
249
|
model: 'qwen3-vl-plus', // AI 模型(可选)
|
|
94
|
-
screenshotDir: './screenshots', // 截图保存目录(可选)
|
|
95
|
-
saveScreenshots: true, // 是否保存截图(可选)
|
|
96
250
|
timeout: 30000, // 超时时间(可选)
|
|
97
251
|
});
|
|
98
252
|
```
|
|
@@ -135,8 +289,6 @@ const result = await aiReviewScreenshot(page, '页面应该显示登录表单');
|
|
|
135
289
|
const result = await aiReviewScreenshot(page, {
|
|
136
290
|
expected: '页面应该显示商品列表',
|
|
137
291
|
model: 'gpt-4-vision-preview', // 使用其他模型
|
|
138
|
-
saveScreenshots: false, // 不保存截图
|
|
139
|
-
screenshotPrefix: 'product', // 自定义截图前缀
|
|
140
292
|
});
|
|
141
293
|
```
|
|
142
294
|
|
|
@@ -183,26 +335,48 @@ test.describe('商品页面测试', () => {
|
|
|
183
335
|
});
|
|
184
336
|
```
|
|
185
337
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
338
|
+
## 🔧 配置文件
|
|
339
|
+
|
|
340
|
+
### `.smarttestrc.json`
|
|
341
|
+
|
|
342
|
+
使用 `smart-test init` 命令生成的配置文件:
|
|
343
|
+
|
|
344
|
+
```json
|
|
345
|
+
{
|
|
346
|
+
"version": "1.0.0",
|
|
347
|
+
"ai": {
|
|
348
|
+
"model": "qwen3-vl-plus",
|
|
349
|
+
"apiKey": "${AI_API_KEY}",
|
|
350
|
+
"baseURL": "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
|
351
|
+
"timeout": 30000
|
|
352
|
+
},
|
|
353
|
+
"output": {
|
|
354
|
+
"testsDir": "./tests/e2e",
|
|
355
|
+
"helpersDir": "./tests/helpers",
|
|
356
|
+
"naming": "kebab-case"
|
|
357
|
+
},
|
|
358
|
+
"templates": {
|
|
359
|
+
"mock": "default",
|
|
360
|
+
"test": "default"
|
|
361
|
+
},
|
|
362
|
+
"playwright": {
|
|
363
|
+
"baseURL": "http://localhost:8080",
|
|
364
|
+
"viewport": {
|
|
365
|
+
"width": 375,
|
|
366
|
+
"height": 667
|
|
367
|
+
},
|
|
368
|
+
"browser": "chromium"
|
|
202
369
|
}
|
|
203
|
-
}
|
|
370
|
+
}
|
|
204
371
|
```
|
|
205
372
|
|
|
373
|
+
### 支持的配置格式
|
|
374
|
+
|
|
375
|
+
- `.smarttestrc.json`
|
|
376
|
+
- `.smarttestrc.js`
|
|
377
|
+
- `smarttest.config.js`
|
|
378
|
+
- `package.json` 中的 `smarttest` 字段
|
|
379
|
+
|
|
206
380
|
## 🔧 高级配置
|
|
207
381
|
|
|
208
382
|
### 支持多种 AI 模型
|
|
@@ -263,9 +437,17 @@ A: 理论上支持所有兼容 OpenAI Chat Completions API 的视觉模型,包
|
|
|
263
437
|
- OpenAI GPT-4 Vision
|
|
264
438
|
- 其他兼容的模型服务
|
|
265
439
|
|
|
266
|
-
### Q:
|
|
440
|
+
### Q: CLI 生成的测试代码需要手动修改吗?
|
|
441
|
+
|
|
442
|
+
A: 生成的代码是可以直接运行的,但建议根据实际需求进行调整。AI 会尽量生成合理的测试用例,但可能无法覆盖所有业务场景。
|
|
443
|
+
|
|
444
|
+
### Q: 录制模式如何处理需要点击才能触发的接口?
|
|
445
|
+
|
|
446
|
+
A: 录制模式支持三种方式:
|
|
267
447
|
|
|
268
|
-
|
|
448
|
+
1. **手动操作**:`--manual` 参数,用户手动操作浏览器
|
|
449
|
+
2. **交互式引导**:`--interactive` 参数,AI 引导用户操作
|
|
450
|
+
3. **脚本引导**:`--actions` 参数,提供操作脚本自动执行
|
|
269
451
|
|
|
270
452
|
### Q: 如何查看 AI 的原始响应?
|
|
271
453
|
|
|
@@ -276,6 +458,55 @@ const result = await aiReviewScreenshot(page, '...');
|
|
|
276
458
|
console.log('AI 原始响应:', result.rawResponse);
|
|
277
459
|
```
|
|
278
460
|
|
|
461
|
+
### Q: 生成的代码文件如何组织?
|
|
462
|
+
|
|
463
|
+
A: 默认按页面分组:
|
|
464
|
+
|
|
465
|
+
- `tests/helpers/mock-{页面名}.ts` - Mock 数据
|
|
466
|
+
- `tests/e2e/{页面名}.spec.ts` - 测试用例
|
|
467
|
+
|
|
468
|
+
可以通过 `--output` 参数自定义输出目录。
|
|
469
|
+
|
|
470
|
+
## 🗺️ 开发路线图
|
|
471
|
+
|
|
472
|
+
### ✅ 已完成
|
|
473
|
+
|
|
474
|
+
- [x] AI 视觉检查核心功能
|
|
475
|
+
- [x] CLI 基础架构
|
|
476
|
+
- [x] 配置系统
|
|
477
|
+
- [x] 交互式初始化
|
|
478
|
+
|
|
479
|
+
### 🚧 开发中
|
|
480
|
+
|
|
481
|
+
- [ ] 录制模式(Phase 2)
|
|
482
|
+
- [ ] 网络请求录制
|
|
483
|
+
- [ ] HAR 文件解析
|
|
484
|
+
- [ ] AI 生成 Mock 代码
|
|
485
|
+
- [ ] 交互操作录制
|
|
486
|
+
|
|
487
|
+
### 📅 计划中
|
|
488
|
+
|
|
489
|
+
- [ ] 代码分析模式(Phase 3)
|
|
490
|
+
- [ ] TypeScript AST 解析
|
|
491
|
+
- [ ] API 调用提取
|
|
492
|
+
- [ ] 类型定义分析
|
|
493
|
+
|
|
494
|
+
- [ ] 描述生成模式(Phase 4)
|
|
495
|
+
- [ ] 自然语言理解
|
|
496
|
+
- [ ] 交互式问答
|
|
497
|
+
- [ ] 代码生成
|
|
498
|
+
|
|
499
|
+
- [ ] 增强功能(Phase 5+)
|
|
500
|
+
- [ ] 增量更新
|
|
501
|
+
- [ ] 自定义模板
|
|
502
|
+
- [ ] 测试覆盖率分析
|
|
503
|
+
- [ ] VS Code 插件
|
|
504
|
+
|
|
505
|
+
## 📄 文档
|
|
506
|
+
|
|
507
|
+
- [CLI 设计文档](./docs/CLI_DESIGN.md) - 完整的 CLI 工具设计方案
|
|
508
|
+
- [使用示例](./examples/) - 更多使用示例
|
|
509
|
+
|
|
279
510
|
## 📝 License
|
|
280
511
|
|
|
281
512
|
MIT
|
|
@@ -287,3 +518,7 @@ MIT
|
|
|
287
518
|
## 📮 联系方式
|
|
288
519
|
|
|
289
520
|
如有问题,请在 [GitHub Issues](https://github.com/snowmountain-top/be-link/issues) 中提出。
|
|
521
|
+
|
|
522
|
+
---
|
|
523
|
+
|
|
524
|
+
**版本**:v1.0.0 | **最后更新**:2025-12-30
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Smart Test CLI 入口文件
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// 使用 Node.js 原生 ESM 加载器
|
|
8
|
+
import('../dist/cli/index.js')
|
|
9
|
+
.then((module) => {
|
|
10
|
+
module.run(process.argv);
|
|
11
|
+
})
|
|
12
|
+
.catch((error) => {
|
|
13
|
+
console.error('Failed to load CLI:', error);
|
|
14
|
+
process.exit(1);
|
|
15
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 生成命令选项
|
|
3
|
+
*/
|
|
4
|
+
export interface GenerateOptions {
|
|
5
|
+
page?: string;
|
|
6
|
+
url?: string;
|
|
7
|
+
desc?: string;
|
|
8
|
+
output?: string;
|
|
9
|
+
interactive?: boolean;
|
|
10
|
+
dryRun?: boolean;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* 生成命令
|
|
14
|
+
*/
|
|
15
|
+
export declare function generateCommand(options: GenerateOptions): Promise<void>;
|
|
16
|
+
//# sourceMappingURL=generate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/generate.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED;;GAEG;AACH,wBAAsB,eAAe,CAAC,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC,CAgD7E"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/init.ts"],"names":[],"mappings":"AAQA;;GAEG;AACH,wBAAsB,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC,CA2HjD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/cli/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAOpC;;GAEG;AACH,wBAAgB,SAAS,IAAI,OAAO,CAgEnC;AAED;;GAEG;AACH,wBAAsB,GAAG,CAAC,IAAI,GAAE,MAAM,EAAiB,GAAG,OAAO,CAAC,IAAI,CAAC,CAGtE"}
|