-
Notifications
You must be signed in to change notification settings - Fork 133
/
create.ts
108 lines (91 loc) 路 2.61 KB
/
create.ts
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
import { buildCode, stepToSelector } from "@qawolf/build-code";
import { buildWorkflow } from "@qawolf/build-workflow";
import { launch } from "@qawolf/browser";
import { CONFIG } from "@qawolf/config";
import { logger } from "@qawolf/logger";
import { outputFile, outputJson } from "fs-extra";
import { Url } from "url";
type RecordOptions = {
debug?: boolean;
device?: string;
name: string;
path?: string;
test?: boolean;
url: Url;
};
export const create = async (options: RecordOptions): Promise<void> => {
const Listr = require("listr");
const input = require("listr-input");
const { name } = options;
const browser = await launch({
device: options.device,
recordEvents: true,
timeout: 0,
url: options.url.href
});
const qawolfPath = options.path || `${process.cwd()}/.qawolf`;
const saveJson = (type: string, data: any) => {
const path = `${qawolfPath}/${type}/${name}.json`;
logger.verbose(`save ${path}`);
return outputJson(path, data, { spaces: " " });
};
const codeFileName = options.test ? `${name}.test.js` : `${name}.js`;
const codePath = options.test
? `${qawolfPath}/tests/${codeFileName}`
: `${qawolfPath}/scripts/${codeFileName}`;
let skipSave = false;
const tasks = new Listr([
{
title: `Capturing browser actions for "${codeFileName}"`,
task: () =>
input("Save [Y/n]", {
done: (value: string) => {
skipSave = value.toLowerCase().trim() === "n";
}
})
},
{
title: `Saving "${codePath}"`,
task: async (_: any, task: any) => {
await browser.close();
if (skipSave) {
task.skip();
return;
}
if (options.debug) {
await saveJson("events", browser.qawolf.events);
}
const workflow = buildWorkflow({
device: options.device,
events: browser.qawolf.events,
name: name,
url: options.url.href!
});
if (options.debug) {
await saveJson("workflows", workflow);
}
await saveJson(
"selectors",
workflow.steps.map((step, index) => ({
// inline index so it is easy to correlate with the test
index,
...stepToSelector(step)
}))
);
logger.verbose(`save ${codePath}`);
await outputFile(
codePath,
buildCode({ test: options.test, workflow }),
"utf8"
);
}
}
]);
tasks
.run()
.then(() => process.exit(0))
.catch((err: Error) => {
console.error(err);
process.exit(1);
});
};