bk-press 0.0.4 → 0.0.5
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/lib/command/fixture.js +42 -0
- package/lib/command/index.js +1 -0
- package/lib/command/init.js +1 -1
- package/lib/command/run.js +1 -1
- package/lib/index.js +14 -4
- package/package.json +1 -1
- package/src/command/fixture.ts +58 -0
- package/src/command/index.ts +1 -0
- package/src/index.ts +18 -6
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.fixture = fixture;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const util_1 = require("../util");
|
|
9
|
+
async function fixture() {
|
|
10
|
+
console.log("开始生成 fixture 文件...");
|
|
11
|
+
const logJsonFile = path_1.default.join(process.cwd(), "web-interaction-log.json");
|
|
12
|
+
const isExistJsonFile = await (0, util_1.isFileOrFolderExist)(logJsonFile);
|
|
13
|
+
if (!isExistJsonFile) {
|
|
14
|
+
throw new Error("web-interaction-log.json 文件不存在,请在 Chrome 应用商店安装 Web Interaction Logger 插件后,录制并导出 web-interaction-log.json 到项目根目录下。");
|
|
15
|
+
}
|
|
16
|
+
const logList = (await (0, util_1.readFile)(logJsonFile));
|
|
17
|
+
const validLogList = logList.filter((log) => log.type === "network" &&
|
|
18
|
+
log.subtype === "response-body" &&
|
|
19
|
+
log.url.startsWith("/"));
|
|
20
|
+
const commonPath = path_1.default.join(process.cwd(), "cypress_fixtures");
|
|
21
|
+
const isExistFolder = await (0, util_1.isFileOrFolderExist)(commonPath);
|
|
22
|
+
if (!isExistFolder) {
|
|
23
|
+
await (0, util_1.createFolder)(commonPath);
|
|
24
|
+
console.log(`创建 fixtures 文件夹: ${commonPath}`);
|
|
25
|
+
}
|
|
26
|
+
validLogList.forEach(async (log) => {
|
|
27
|
+
const jsonContent = log.responseBody;
|
|
28
|
+
const jsonString = JSON.stringify(jsonContent, null, 2);
|
|
29
|
+
const isExistQuery = log.url.includes("?");
|
|
30
|
+
let fileName = "";
|
|
31
|
+
if (isExistQuery) {
|
|
32
|
+
fileName = log.url.split("?")[0].replace(/\//g, "_");
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
fileName = log.url.replace(/\//g, "_");
|
|
36
|
+
}
|
|
37
|
+
fileName = `${log.method}_${fileName.substring(1, fileName.length - 1)}.json`;
|
|
38
|
+
console.log(`生成 fixture 文件: ${fileName}`);
|
|
39
|
+
const fixtureFile = path_1.default.join(commonPath, fileName);
|
|
40
|
+
await (0, util_1.writeFile)(fixtureFile, jsonString);
|
|
41
|
+
});
|
|
42
|
+
}
|
package/lib/command/index.js
CHANGED
package/lib/command/init.js
CHANGED
|
@@ -10,7 +10,7 @@ const path_1 = __importDefault(require("path"));
|
|
|
10
10
|
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
11
11
|
const util_1 = require("../util");
|
|
12
12
|
async function init() {
|
|
13
|
-
console.log("开始初始化Cypress
|
|
13
|
+
console.log("开始初始化Cypress自动化测试环境...");
|
|
14
14
|
console.log("安装相关依赖...");
|
|
15
15
|
await installDependencies();
|
|
16
16
|
console.log("添加类型到 tsconfig.json 文件...");
|
package/lib/command/run.js
CHANGED
|
@@ -9,7 +9,7 @@ const login_1 = require("./login");
|
|
|
9
9
|
const os_1 = __importDefault(require("os"));
|
|
10
10
|
async function run() {
|
|
11
11
|
console.log("开始运行自动化测试...");
|
|
12
|
-
console.log("当前工作目录: ", process.cwd());
|
|
12
|
+
// console.log("当前工作目录: ", process.cwd());
|
|
13
13
|
const config = await (0, login_1.readCypressEnvConfig)();
|
|
14
14
|
const cpuCount = os_1.default.cpus().length;
|
|
15
15
|
console.log("当前CPU核心数: ", cpuCount, ", 设置测试并发数: ", cpuCount);
|
package/lib/index.js
CHANGED
|
@@ -7,9 +7,19 @@ const program = new commander_1.Command();
|
|
|
7
7
|
program
|
|
8
8
|
.name("bk-press")
|
|
9
9
|
.description("一个用于 Cypress 自动化测试的 CLI 工具,帮助快速初始化和运行端到端测试")
|
|
10
|
-
.version("0.0.
|
|
11
|
-
program
|
|
12
|
-
|
|
10
|
+
.version("0.0.5");
|
|
11
|
+
program
|
|
12
|
+
.command("init")
|
|
13
|
+
.description("初始化 Cypress 自动化测试环境")
|
|
14
|
+
.action(command_1.init);
|
|
15
|
+
program
|
|
16
|
+
.command("login")
|
|
17
|
+
.description("登录本地开发环境并保存 Cookie")
|
|
18
|
+
.action(command_1.login);
|
|
13
19
|
// @ts-ignore
|
|
14
|
-
program.command("run").action(command_1.run);
|
|
20
|
+
program.command("run").description("并行运行自动化测试").action(command_1.run);
|
|
21
|
+
program
|
|
22
|
+
.command("fixture")
|
|
23
|
+
.description("根据 Chrome 插件 Web Interaction Logger 导出的 web-interaction-log.json 生成 fixture 拦截数据")
|
|
24
|
+
.action(command_1.fixture);
|
|
15
25
|
program.parse(process.argv);
|
package/package.json
CHANGED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import {
|
|
3
|
+
isFileOrFolderExist,
|
|
4
|
+
readFile,
|
|
5
|
+
writeFile,
|
|
6
|
+
createFolder,
|
|
7
|
+
} from "../util";
|
|
8
|
+
|
|
9
|
+
interface WebInteractionLog {
|
|
10
|
+
type: string;
|
|
11
|
+
subtype: string;
|
|
12
|
+
requestId: string;
|
|
13
|
+
url: string;
|
|
14
|
+
method: string;
|
|
15
|
+
status: number;
|
|
16
|
+
statusText: string;
|
|
17
|
+
responseHeaders: string;
|
|
18
|
+
responseBody: Record<string, any>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function fixture() {
|
|
22
|
+
console.log("开始生成 fixture 文件...");
|
|
23
|
+
const logJsonFile = path.join(process.cwd(), "web-interaction-log.json");
|
|
24
|
+
const isExistJsonFile = await isFileOrFolderExist(logJsonFile);
|
|
25
|
+
if (!isExistJsonFile) {
|
|
26
|
+
throw new Error(
|
|
27
|
+
"web-interaction-log.json 文件不存在,请在 Chrome 应用商店安装 Web Interaction Logger 插件后,录制并导出 web-interaction-log.json 到项目根目录下。",
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
const logList = (await readFile(logJsonFile)) as WebInteractionLog[];
|
|
31
|
+
const validLogList = logList.filter(
|
|
32
|
+
(log) =>
|
|
33
|
+
log.type === "network" &&
|
|
34
|
+
log.subtype === "response-body" &&
|
|
35
|
+
log.url.startsWith("/"),
|
|
36
|
+
);
|
|
37
|
+
const commonPath = path.join(process.cwd(), "cypress_fixtures");
|
|
38
|
+
const isExistFolder = await isFileOrFolderExist(commonPath);
|
|
39
|
+
if (!isExistFolder) {
|
|
40
|
+
await createFolder(commonPath);
|
|
41
|
+
console.log(`创建 fixtures 文件夹: ${commonPath}`);
|
|
42
|
+
}
|
|
43
|
+
validLogList.forEach(async (log) => {
|
|
44
|
+
const jsonContent = log.responseBody;
|
|
45
|
+
const jsonString = JSON.stringify(jsonContent, null, 2);
|
|
46
|
+
const isExistQuery = log.url.includes("?");
|
|
47
|
+
let fileName = "";
|
|
48
|
+
if (isExistQuery) {
|
|
49
|
+
fileName = log.url.split("?")[0].replace(/\//g, "_");
|
|
50
|
+
} else {
|
|
51
|
+
fileName = log.url.replace(/\//g, "_");
|
|
52
|
+
}
|
|
53
|
+
fileName = `${log.method}_${fileName.substring(1, fileName.length - 1)}.json`;
|
|
54
|
+
console.log(`生成 fixture 文件: ${fileName}`);
|
|
55
|
+
const fixtureFile = path.join(commonPath, fileName);
|
|
56
|
+
await writeFile(fixtureFile, jsonString);
|
|
57
|
+
});
|
|
58
|
+
}
|
package/src/command/index.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,19 +1,31 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
import { Command } from "commander";
|
|
4
|
-
import { login, init, run } from "./command";
|
|
4
|
+
import { login, init, run, fixture } from "./command";
|
|
5
5
|
|
|
6
6
|
const program = new Command();
|
|
7
7
|
|
|
8
8
|
program
|
|
9
9
|
.name("bk-press")
|
|
10
10
|
.description(
|
|
11
|
-
"一个用于 Cypress 自动化测试的 CLI 工具,帮助快速初始化和运行端到端测试"
|
|
11
|
+
"一个用于 Cypress 自动化测试的 CLI 工具,帮助快速初始化和运行端到端测试",
|
|
12
12
|
)
|
|
13
|
-
.version("0.0.
|
|
13
|
+
.version("0.0.5");
|
|
14
14
|
|
|
15
|
-
program
|
|
16
|
-
|
|
15
|
+
program
|
|
16
|
+
.command("init")
|
|
17
|
+
.description("初始化 Cypress 自动化测试环境")
|
|
18
|
+
.action(init);
|
|
19
|
+
program
|
|
20
|
+
.command("login")
|
|
21
|
+
.description("登录本地开发环境并保存 Cookie")
|
|
22
|
+
.action(login);
|
|
17
23
|
// @ts-ignore
|
|
18
|
-
program.command("run").action(run);
|
|
24
|
+
program.command("run").description("并行运行自动化测试").action(run);
|
|
25
|
+
program
|
|
26
|
+
.command("fixture")
|
|
27
|
+
.description(
|
|
28
|
+
"根据 Chrome 插件 Web Interaction Logger 导出的 web-interaction-log.json 生成 fixture 拦截数据",
|
|
29
|
+
)
|
|
30
|
+
.action(fixture);
|
|
19
31
|
program.parse(process.argv);
|