forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmoke_test.js
368 lines (293 loc) · 9.73 KB
/
smoke_test.js
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* eslint no-console: "off" */
const args = process.argv.slice(2);
if (args.length < 1 || args.length > 2) {
console.log("Expecting: node {smoke_test.js} {url}");
process.exit(1);
}
const url = args[0];
console.log(`Starting Discourse Smoke Test for ${url}`);
const chromeLauncher = require("chrome-launcher");
const puppeteer = require("puppeteer-core");
const path = require("path");
(async () => {
const browser = await puppeteer.launch({
executablePath: chromeLauncher.Launcher.getInstallations()[0],
// when debugging locally setting the SHOW_BROWSER env variable can be very helpful
headless: process.env.SHOW_BROWSER === undefined,
args: ["--disable-local-storage", "--no-sandbox"],
});
const page = await browser.newPage();
await page.setViewport({
width: 1366,
height: 768,
});
const takeFailureScreenshot = function () {
const screenshotPath = `${
process.env.SMOKE_TEST_SCREENSHOT_PATH || "tmp/smoke-test-screenshots"
}/smoke-test-${Date.now()}.png`;
console.log(`Screenshot of failure taken at ${screenshotPath}`);
return page.screenshot({ path: screenshotPath, fullPage: true });
};
const exec = (description, fn, assertion) => {
const start = +new Date();
return fn
.call()
.then(async (output) => {
if (assertion) {
if (assertion.call(this, output)) {
console.log(`PASSED: ${description} - ${+new Date() - start}ms`);
} else {
console.log(`FAILED: ${description} - ${+new Date() - start}ms`);
await takeFailureScreenshot();
console.log("SMOKE TEST FAILED");
process.exit(1);
}
} else {
console.log(`PASSED: ${description} - ${+new Date() - start}ms`);
}
})
.catch(async (error) => {
console.log(
`ERROR (${description}): ${error.message} - ${+new Date() - start}ms`
);
await takeFailureScreenshot();
console.log("SMOKE TEST FAILED");
process.exit(1);
});
};
const assert = (description, fn, assertion) => {
return exec(description, fn, assertion);
};
page.on("console", (msg) => console.log(`PAGE LOG: ${msg.text()}`));
page.on("response", (resp) => {
if (resp.status() !== 200 && resp.status() !== 302) {
console.log(
"FAILED HTTP REQUEST TO " + resp.url() + " Status is: " + resp.status()
);
if (resp.status() === 429) {
const headers = resp.headers();
console.log("Response headers:");
Object.keys(headers).forEach((key) => {
console.log(`${key}: ${headers[key]}`);
});
}
}
return resp;
});
if (process.env.AUTH_USER && process.env.AUTH_PASSWORD) {
await exec("basic authentication", () => {
return page.authenticate({
username: process.env.AUTH_USER,
password: process.env.AUTH_PASSWORD,
});
});
}
const login = async function () {
await exec("open login modal", () => {
return page.click(".login-button");
});
await exec("login modal is open", () => {
return page.waitForSelector(".login-modal", { visible: true });
});
await exec("type in credentials & log in", () => {
let promise = page.type(
"#login-account-name",
process.env.DISCOURSE_USERNAME || "smoke_user"
);
promise = promise.then(() => {
return page.type(
"#login-account-password",
process.env.DISCOURSE_PASSWORD || "P4ssw0rd"
);
});
promise = promise.then(() => {
return page.click(".login-modal .btn-primary");
});
return promise;
});
await exec("is logged in", () => {
return page.waitForSelector(".current-user", { visible: true });
});
};
await exec("go to site", () => {
return page.goto(url);
});
await exec("expect a log in button in the header", () => {
return page.waitForSelector("header .login-button", { visible: true });
});
if (process.env.LOGIN_AT_BEGINNING) {
await login();
}
await exec("go to latest page", () => {
return page.goto(path.join(url, "latest"));
});
await exec("at least one topic shows up", () => {
return page.waitForSelector(".topic-list tbody tr", { visible: true });
});
await exec("go to categories page", () => {
return page.goto(path.join(url, "categories"));
});
await exec("can see categories on the page", () => {
return page.waitForSelector(".category-list", { visible: true });
});
await exec("navigate to 1st topic", () => {
return page.click(".main-link a.title:first-of-type");
});
await exec("at least one post body", () => {
return page.waitForSelector(".topic-post", { visible: true });
});
await exec("click on the 1st user", () => {
return page.click(".topic-meta-data a:first-of-type");
});
await exec("user has details", () => {
return page.waitForSelector(".user-card .names", { visible: true });
});
if (!process.env.READONLY_TESTS) {
if (!process.env.LOGIN_AT_BEGINNING) {
await login();
}
await exec("go home", () => {
let promise = page.waitForSelector("#site-logo, #site-text-logo", {
visible: true,
});
promise = promise.then(() => {
return page.click("#site-logo, #site-text-logo");
});
return promise;
});
await exec("it shows a topic list", () => {
return page.waitForSelector(".topic-list", { visible: true });
});
await exec("we have a create topic button", () => {
return page.waitForSelector("#create-topic", { visible: true });
});
await exec("open composer", () => {
return page.click("#create-topic");
});
await exec("the editor is visible", () => {
return page.waitForFunction(
"document.activeElement === document.getElementById('reply-title')"
);
});
await page.evaluate(() => {
document.getElementById("reply-title").value = "";
});
await exec("compose new topic", () => {
const date = `(${+new Date()})`;
const title = `This is a new topic ${date}`;
const post = `I can write a new topic inside the smoke test! ${date} \n\n`;
let promise = page.type("#reply-title", title);
promise = promise.then(() => {
return page.type("#reply-control .d-editor-input", post);
});
return promise;
});
await exec("updates preview", () => {
return page.waitForSelector(".d-editor-preview p", { visible: true });
});
await exec("submit the topic", () => {
return page.click(".submit-panel .create");
});
await exec("topic is created", () => {
return page.waitForSelector(".fancy-title", { visible: true });
});
await exec("open the composer", () => {
return page.click(".post-controls:first-of-type .create");
});
await exec("composer is open", () => {
return page.waitForSelector("#reply-control .d-editor-input", {
visible: true,
});
});
await exec("compose reply", () => {
const post = `I can even write a reply inside the smoke test ;) (${+new Date()})`;
return page.type("#reply-control .d-editor-input", post);
});
await exec("waiting for the preview", () => {
return page.waitForXPath(
"//div[contains(@class, 'd-editor-preview') and contains(.//p, 'I can even write a reply')]",
{ visible: true }
);
});
await exec("wait a little bit", () => {
return page.waitForTimeout(5000);
});
await exec("submit the reply", () => {
let promise = page.click("#reply-control .create");
promise = promise.then(() => {
return page.waitForSelector("#reply-control.closed", {
visible: false,
});
});
return promise;
});
await assert("reply is created", () => {
let promise = page.waitForSelector(
".topic-post:not(.staged) #post_2 .cooked",
{
visible: true,
}
);
promise = promise.then(() => {
return page.waitForFunction(
"document.querySelector('#post_2 .cooked').innerText.includes('I can even write a reply')"
);
});
return promise;
});
await exec("wait a little bit", () => {
return page.waitForTimeout(5000);
});
await exec("open composer to edit first post", () => {
let promise = page.evaluate(() => {
window.scrollTo(0, 0);
});
promise = promise.then(() => {
return page.click("#post_1 .post-controls .edit");
});
promise = promise.then(() => {
return page.waitForSelector("#reply-control .d-editor-input", {
visible: true,
});
});
return promise;
});
await exec("update post raw in composer", () => {
let promise = page.waitForTimeout(5000);
promise = promise.then(() => {
return page.type(
"#reply-control .d-editor-input",
"\n\nI edited this post"
);
});
return promise;
});
await exec("submit the edit", () => {
let promise = page.click("#reply-control .create");
promise = promise.then(() => {
return page.waitForSelector("#reply-control.closed", {
visible: false,
});
});
return promise;
});
await assert("edit is successful", () => {
let promise = page.waitForSelector(
".topic-post:not(.staged) #post_1 .cooked",
{
visible: true,
}
);
promise = promise.then(() => {
return page.waitForFunction(
"document.querySelector('#post_1 .cooked').innerText.includes('I edited this post')"
);
});
return promise;
});
}
await exec("close browser", () => {
return browser.close();
});
console.log("ALL PASSED");
})();