-
Notifications
You must be signed in to change notification settings - Fork 7
/
run.ts
157 lines (136 loc) · 4.15 KB
/
run.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
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
import * as fs from "fs";
import { currentPlatform, spawnChecked } from "./utils";
const { uploadMetadata } = require("./src/metadata");
const Usage = `
Usage: ts-node run.ts options
Options:
--recordings <path> File to store recording IDs.
--container Use docker container for running tests.
--env <key> <value> Set environment variable in tests.
<test> Run given test once.
Set $PLAYWRIGHT_CHROMIUM to use chromium.
Set $PLAYWRIGHT_HEADLESS to run tests with a headless browser.
`;
let gRecordingFile: string | undefined;
let gMetadataFile = "../metadata.log";
let gUseContainer = false;
const gChromium = !!process.env.PLAYWRIGHT_CHROMIUM;
const gTests: string[] = [];
const gEnvironment: Record<string, string> = {};
for (let i = 2; i < process.argv.length; i++) {
const arg = process.argv[i];
if (arg == "--recordings") {
gRecordingFile = process.argv[++i];
} else if (arg == "--container") {
gUseContainer = true;
} else if (arg == "--env") {
gEnvironment[process.argv[++i]] = process.argv[++i];
} else if (arg === "--metadata") {
gMetadataFile = process.argv[++i];
} else {
if (fs.existsSync(`${__dirname}/${arg}`)) {
gTests.push(arg);
} else {
console.log(`Unknown test ${arg}`);
console.log(Usage);
process.exit(1);
}
}
}
if (!gTests.length) {
console.log(Usage);
process.exit(1);
}
if (gTests.length && !gRecordingFile) {
console.log("Recordings file must be specified to run tests");
console.log(Usage);
process.exit(1);
}
function mkdirSyncIfNotExists(dir: string) {
try {
fs.mkdirSync(dir);
} catch (e) {}
}
async function runTest(test: string) {
const server = process.env.RECORD_REPLAY_SERVER || "wss://dispatch.replay.io";
if (gUseContainer) {
const tmpRecordingFile = `recordings-${(Math.random() * 1e9) | 0}.log`;
const tmpRecordingPath = `${__dirname}/${tmpRecordingFile}`;
if (fs.existsSync(gRecordingFile || "")) {
fs.copyFileSync(gRecordingFile || "", tmpRecordingPath);
}
const script = `
npm i -g typescript ts-node [email protected]
npm i --save-dev @types/node
ts-node playwright-tests/${test}
`;
const tmpScriptFile = `run-${(Math.random() * 1e9) | 0}`;
const tmpScriptPath = `${__dirname}/${tmpScriptFile}`;
fs.writeFileSync(tmpScriptPath, script);
const envArgs = [];
for (const [key, value] of Object.entries(gEnvironment)) {
envArgs.push("-e", `${key}=${value}`);
}
spawnChecked(
"docker",
[
"run",
"-v",
`${__dirname}:/playwright-tests`,
"-e",
`RECORD_REPLAY_RECORDING_ID_FILE=/playwright-tests/${tmpRecordingFile}`,
"-e",
`RECORD_REPLAY_SERVER=${server}`,
"-e",
"RECORD_ALL_CONTENT=1",
"-e",
"PLAYWRIGHT_HEADLESS=1",
...envArgs,
"recordreplayinc/playwright:latest",
"bash",
`/playwright-tests/${tmpScriptFile}`,
],
{ stdio: "inherit" }
);
if (fs.existsSync(tmpRecordingPath)) {
fs.copyFileSync(tmpRecordingPath, gRecordingFile || "");
fs.unlinkSync(tmpRecordingPath);
}
fs.unlinkSync(tmpScriptPath);
return;
}
try {
const env: NodeJS.ProcessEnv = {
...process.env,
...gEnvironment,
RECORD_REPLAY_RECORDING_ID_FILE: gRecordingFile,
RECORD_REPLAY_RECORDING_METADATA_FILE: gMetadataFile,
RECORD_REPLAY_SERVER: server,
};
spawnChecked("ts-node", [`${__dirname}/${test}`], {
stdio: "inherit",
env,
});
if (!env.RECORD_REPLAY_NO_RECORD) {
const recordingId = await uploadMetadata(gRecordingFile, gMetadataFile);
const replayHost = server.match(/.*dispatch\.(.*)/)![1];
console.log(
new Date(),
`New Replay for ${test} available at https://${replayHost}/view?id=${recordingId}`
);
}
} catch (e: any) {
const t = new Date();
console.error(t, "Test failed:", e.message);
if ("stack" in e && typeof e.stack === "string") {
e.stack.split("\n").forEach((s: string) => console.error(t, s));
}
process.exit(1);
}
}
async function main() {
for (const test of gTests) {
await runTest(test);
}
}
main();