forked from louislam/uptime-kuma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe2e.spec.js
312 lines (253 loc) · 10.3 KB
/
e2e.spec.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
// eslint-disable-next-line no-unused-vars
const { Page, Browser } = require("puppeteer");
const { sleep } = require("../src/util");
const axios = require("axios");
/**
* Set back the correct data type for page object
* @type {Page}
*/
page;
/**
* @type {Browser}
*/
browser;
beforeAll(async () => {
await page.setViewport({
width: 1280,
height: 720,
deviceScaleFactor: 1,
});
});
afterAll(() => {
});
const baseURL = "http://127.0.0.1:3002";
describe("Init", () => {
const title = "Uptime Kuma";
beforeAll(async () => {
await page.goto(baseURL);
});
it(`should be titled "${title}"`, async () => {
await expect(page.title()).resolves.toEqual(title);
});
// Setup Page
it("Setup", async () => {
// Create an Admin
await page.waitForSelector("#floatingInput");
await page.waitForSelector("#repeat");
await page.click("#floatingInput");
await page.type("#floatingInput", "admin");
await page.type("#floatingPassword", "admin123");
await page.type("#repeat", "admin123");
await page.click(".btn-primary[type=submit]");
await sleep(3000);
// Go to /setup again
await page.goto(baseURL + "/setup");
await sleep(3000);
let pathname = await page.evaluate(() => location.pathname);
expect(pathname).toEqual("/dashboard");
// Go to /
await page.goto(baseURL);
await page.waitForSelector("h1.mb-3");
pathname = await page.evaluate(() => location.pathname);
expect(pathname).toEqual("/dashboard");
});
it("should create monitor", async () => {
// Create monitor
await page.goto(baseURL + "/add");
await page.waitForSelector("#name");
await page.type("#name", "Myself");
await page.waitForSelector("#url");
await page.click("#url", { clickCount: 3 });
await page.keyboard.type(baseURL);
await page.keyboard.press("Enter");
await page.waitForFunction(() => {
const badge = document.querySelector("span.badge");
return badge && badge.innerText == "100%";
}, { timeout: 5000 });
});
// Settings Page
/*
describe("Settings", () => {
beforeEach(async () => {
await page.goto(baseURL + "/settings");
});
it("Change Language", async () => {
await page.goto(baseURL + "/settings/appearance");
await page.waitForSelector("#language");
await page.select("#language", "zh-HK");
let languageTitle = await page.evaluate(() => document.querySelector("[for=language]").innerText);
expect(languageTitle).toEqual("語言");
await page.select("#language", "en");
languageTitle = await page.evaluate(() => document.querySelector("[for=language]").innerText);
expect(languageTitle).toEqual("Language");
});
it("Change Theme", async () => {
await page.goto(baseURL + "/settings/appearance");
// Dark
await click(page, ".btn[for=btncheck2]");
await page.waitForSelector("div.dark");
await page.waitForSelector(".btn[for=btncheck1]");
// Light
await click(page, ".btn[for=btncheck1]");
await page.waitForSelector("div.light");
});
it("Change Heartbeat Bar Style", async () => {
await page.goto(baseURL + "/settings/appearance");
// Bottom
await click(page, ".btn[for=btncheck5]");
await page.waitForSelector("div.hp-bar-big");
// None
await click(page, ".btn[for=btncheck6]");
await page.waitForSelector("div.hp-bar-big", {
hidden: true,
timeout: 1000
});
});
// TODO: Timezone
it("Search Engine Visibility", async () => {
// Default
let res = await axios.get(baseURL + "/robots.txt");
expect(res.data).toContain("Disallow: /");
// Yes
await click(page, "#searchEngineIndexYes");
await click(page, "form > div > .btn[type=submit]");
await sleep(1000);
res = await axios.get(baseURL + "/robots.txt");
expect(res.data).not.toContain("Disallow: /");
// No
await click(page, "#searchEngineIndexNo");
await click(page, "form > div > .btn[type=submit]");
await sleep(1000);
res = await axios.get(baseURL + "/robots.txt");
expect(res.data).toContain("Disallow: /");
});
it("Entry Page", async () => {
const newPage = await browser.newPage();
// Default
await newPage.goto(baseURL);
await newPage.waitForSelector("h1.mb-3", { timeout: 3000 });
let pathname = await newPage.evaluate(() => location.pathname);
expect(pathname).toEqual("/dashboard");
// Status Page
await click(page, "#entryPageNo");
await click(page, "form > div > .btn[type=submit]");
await sleep(1000);
await newPage.goto(baseURL);
await newPage.waitForSelector("img.logo", { timeout: 3000 });
pathname = await newPage.evaluate(() => location.pathname);
expect(pathname).toEqual("/status");
// Back to Dashboard
await click(page, "#entryPageYes");
await click(page, "form > div > .btn[type=submit]");
await sleep(1000);
await newPage.goto(baseURL);
await newPage.waitForSelector("h1.mb-3", { timeout: 3000 });
pathname = await newPage.evaluate(() => location.pathname);
expect(pathname).toEqual("/dashboard");
await newPage.close();
});
it("Change Password (wrong current password)", async () => {
await page.goto(baseURL + "/settings/security");
await page.waitForSelector("#current-password");
await page.type("#current-password", "wrong_passw$$d");
await page.type("#new-password", "new_password123");
await page.type("#repeat-new-password", "new_password123");
// Save
await click(page, "form > div > .btn[type=submit]", 0);
await sleep(1000);
await click(page, "#logout-btn");
await login("admin", "new_password123");
let elementCount = await page.evaluate(() => document.querySelectorAll("#floatingPassword").length);
expect(elementCount).toEqual(1);
await login("admin", "admin123");
});
it("Change Password (wrong repeat)", async () => {
await page.goto(baseURL + "/settings/security");
await page.waitForSelector("#current-password");
await page.type("#current-password", "admin123");
await page.type("#new-password", "new_password123");
await page.type("#repeat-new-password", "new_password1234567898797898");
await click(page, "form > div > .btn[type=submit]", 0);
await sleep(1000);
await click(page, "#logout-btn");
await login("admin", "new_password123");
let elementCount = await page.evaluate(() => document.querySelectorAll("#floatingPassword").length);
expect(elementCount).toEqual(1);
await login("admin", "admin123");
await page.waitForSelector("#current-password");
let pathname = await page.evaluate(() => location.pathname);
expect(pathname).toEqual("/settings/security");
});
// TODO: 2FA
// TODO: Export Backup
// TODO: Import Backup
it("Should disable & enable auth", async () => {
await page.goto(baseURL + "/settings/security");
await click(page, "#disableAuth-btn");
await click(page, ".btn.btn-danger[data-bs-dismiss='modal']", 2); // Not a good way to do it
await page.waitForSelector("#enableAuth-btn", { timeout: 3000 });
await page.waitForSelector("#logout-btn", {
hidden: true,
timeout: 3000
});
const newPage = await browser.newPage();
await newPage.goto(baseURL);
await newPage.waitForSelector("span.badge", { timeout: 3000 });
newPage.close();
await click(page, "#enableAuth-btn");
await login("admin", "admin123");
await page.waitForSelector("#disableAuth-btn", { timeout: 3000 });
});
// it("Should clear all statistics", async () => {
// await page.goto(baseURL + "/settings/monitor-history");
// await click(page, "#clearAllStats-btn");
// await click(page, ".btn.btn-danger");
// await page.waitForFunction(() => {
// const badge = document.querySelector("span.badge");
// return badge && badge.innerText == "0%";
// }, { timeout: 3000 });
// });
});
*/
/*
* TODO
* Create Monitor - All type
* Edit Monitor
* Delete Monitor
*
* Create Notification (token problem, maybe hard to test)
*
*/
describe("Status Page", () => {
const title = "Uptime Kuma";
beforeAll(async () => {
await page.goto(baseURL + "/status");
});
it(`should be titled "${title}"`, async () => {
await expect(page.title()).resolves.toEqual(title);
});
});
});
async function login(username, password) {
await input(page, "#floatingInput", username);
await input(page, "#floatingPassword", password);
await page.click(".btn-primary[type=submit]");
await sleep(5000);
}
async function click(page, selector, elementIndex = 0) {
await page.waitForSelector(selector, {
timeout: 5000,
});
return await page.evaluate((s, i) => {
return document.querySelectorAll(s)[i].click();
}, selector, elementIndex);
}
async function input(page, selector, text) {
await page.waitForSelector(selector, {
timeout: 5000,
});
const element = await page.$(selector);
await element.click({ clickCount: 3 });
await page.keyboard.press("Backspace");
await page.type(selector, text);
}