forked from eclipse-thingweb/playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
323 lines (272 loc) · 11.4 KB
/
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
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the W3C Software Notice and
* Document License (2015-05-13) which is available at
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document.
*
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
*/
/**
* @file Visual testing for this package. Uses `playwright` to create screenshots of the locally served package
* and download an instance of the assertion report and OpenAPI convertion result (json & yaml).
* The screenshots are generated using chromium and firefox (webkit fails ATM) and different
* viewports and actions (click on forms etc.). The test results are written to "./test_results".
*/
const playwright = require("playwright");
const fs = require("fs");
const handler = require("serve-handler");
const http = require("http");
const path = require("path");
const assert = require("node:assert");
const arg = process.argv[2];
const options = arg === "-d" || arg === "--debug" ? { headless: false, slowMo: 200 } : {};
const port = 3000;
const host = "http://localhost";
const fullHost = host + ":" + port;
const testDir = "./test_results";
const server = http.createServer((request, response) => {
// You pass two more arguments for config and middleware
// More details here: https://github.com/vercel/serve-handler#options
return handler(request, response);
});
/* ################### */
/* MAIN */
server.listen(port, () => {
console.log("Running siteTest at " + fullHost);
});
testVisual();
/* ################### */
/**
* main function to execute visual test using playwright
*/
async function testVisual() {
if (!fs.existsSync(testDir)) {
fs.mkdirSync(testDir);
}
const BrowserList = [];
BrowserList.push("chromium");
BrowserList.push("firefox");
// BrowserList.push("webkit")
for (const browserType of BrowserList) {
const browser = await playwright[browserType].launch(options);
const context = await browser.newContext({ acceptDownloads: true });
const page = await context.newPage();
if (browserType === "chromium") {
await testVisualChromium(page);
await testViewport(browser, 1920, 1080);
await testViewport(browser, 2560, 1440);
await testViewport(browser, 3840, 2160);
await testViewport(browser, 4096, 2160);
} else if (browserType === "firefox") {
await testVisualFirefox(page);
} else if (browserType === "webkit") {
await testVisualWebkit(page);
}
await browser.close();
console.log("Finished testing with: " + browserType);
}
server.close();
}
/**
* Render the testing target with given viewport dimensions and take screenshots
* @param {object} browser a playwright browser
* @param {*} width viewport width in px
* @param {*} height viewport height in px
*/
async function testViewport(browser, width, height) {
const customPage = await browser.newPage({
viewport: { width, height },
});
await customPage.goto(fullHost);
await customPage.screenshot({ path: path.join(testDir, "viewport_" + width + "_x_" + height + ".png") });
await customPage.close();
}
/**
* execute tests and take screenshots with chromium
* works better than other browser engines
* @param {object} page a playwright page
*/
async function testVisualChromium(page) {
/** a shortcut function for taking screenshots of chromium pages */
const customShot = async (postfix) => {
await page.screenshot({ path: path.join(testDir, "chromium_" + postfix + ".png") });
};
await page.goto(fullHost);
await customShot("index");
await page.evaluate(() => window.scrollTo(0, document.body.scrollHeight));
await page.click("#load_example", { waitUntil: "networkidle" });
await customShot("dropdown-examples");
await loadTestToEditor("TypoCheckWithoutTypos", page);
await customShot("typo_check_without_typos");
await page.screenshot({ path: `./test_results/chromium_typo_check_without_typos.png`, fullPage: true });
assert(
await page.evaluate(() => {
const markerCount = monaco.editor.getModelMarkers({ owner: "typo" }).length;
const typoCount = JSON.parse(window.tdEditor.getModel(monaco.Uri.parse("")).getValue()).typoCount;
return markerCount === typoCount;
}),
"Typos markers are not equal to typo count!"
);
await loadTestToEditor("TypoCheckWithTypos", page);
await customShot("typo_check_with_typos");
await page.screenshot({ path: `./test_results/chromium_typo_check_with_typos.png`, fullPage: true });
assert(
await page.evaluate(() => {
const markerCount = monaco.editor.getModelMarkers({ owner: "typo" }).length;
const typoCount = JSON.parse(window.tdEditor.getModel(monaco.Uri.parse("")).getValue()).typoCount;
return markerCount === typoCount;
}),
"Typos markers are not equal to typo count!"
);
await page.evaluate(() => window.tdEditor.getModel(monaco.Uri.parse("")).setValue('{ "context": "Test" }'));
await myWait(1000);
await customShot("typo_check_handwritten_typo_exists");
await page.screenshot({ path: `./test_results/chromium_typo_check_handwritten_typo_exists.png`, fullPage: true });
await page.evaluate(() =>
window.tdEditor.getModel(monaco.Uri.parse("")).setValue('{ "@context": "https://www.w3.org/2022/wot/td/v1.1" }')
);
await myWait(1000);
await customShot("typo_check_handwritten_typo_fixed");
await page.screenshot({ path: `./test_results/chromium_typo_check_handwritten_typo_fixed.png`, fullPage: true });
await page.evaluate(() =>
window.tdEditor
.getModel(monaco.Uri.parse(""))
.setValue('{ "@context": "https://www.w3.org/2022/wot/td/v1.1", "@type": [] }')
);
await myWait(1000);
await customShot("unformatted_document");
await page.screenshot({ path: `./test_results/chromium_unformatted_document.png`, fullPage: true });
await page.click("#btn_formatDocument");
await myWait(1000);
await customShot("formatted_document");
await page.screenshot({ path: `./test_results/chromium_formatted_document.png`, fullPage: true });
await page.selectOption("#load_example", "SimpleTD");
await customShot("td");
await page.screenshot({ path: `./test_results/chromium_td.png` });
await page.click("#btn_validate");
await myWait(1000);
await page.screenshot({ path: path.join(testDir, "chromium_validation.png"), fullPage: true });
await page.click("#validation_table_head");
await page.screenshot({ path: `./test_results/chromium_lights.png` });
await customShot("lights");
await page.click("#btn_assertion_popup");
await customShot("assertion-popup");
const [assertionDownload] = await Promise.all([page.waitForEvent("download"), page.click("#btn_assertion")]);
assertionDownload.saveAs("./test_results/assertions.csv");
await page.click("#close_assertion_test_popup");
await loadTestToEditor("HttpAndMqtt", page);
await customShot("http_and_mqtt_td_tab");
await page.click("#open-api-tab");
await customShot("http_and_mqtt_open_api_tab");
const [openapiJsonDownload] = await Promise.all([page.waitForEvent("download"), page.click("#editor-print-btn")]);
openapiJsonDownload.saveAs("./test_results/openapi.json");
await page.click("#async-api-tab");
await customShot("http_and_mqtt_async_api_tab");
await page.evaluate(() => {
document.getElementById("yaml-format-btn").click();
});
await customShot("http_and_mqtt_async_api_tab_yaml");
const [asyncapiYamlDownload] = await Promise.all([page.waitForEvent("download"), page.click("#editor-print-btn")]);
asyncapiYamlDownload.saveAs("./test_results/asyncapi.yaml");
await loadTestToEditor("NoProtocol", page);
await customShot("NoProtocol");
await loadTestToEditor("OnlyHttp", page);
await customShot("OnlyHttp");
await loadTestToEditor("OnlyMqtt", page);
await customShot("OnlyMqtt");
await page.click("#td-tab");
await customShot("td_tab_yaml");
await page.evaluate(() => {
document.getElementById("json-format-btn").click();
});
await page.click("#btn_clearLog");
await myWait(300);
await customShot("cleared-log");
await page.click("#editor_theme");
await customShot("dropdown-editor-color");
await page.selectOption("#editor_theme", "vs-dark");
await customShot("dark-editor");
}
/**
* execute tests and take screenshots with firefox
* the page renders correctly but e.g. dropdowns are not shown in screenshots
* -> use chrome for advanced testing
* @param {object} page a playwright page
*/
async function testVisualFirefox(page) {
const browserType = "firefox";
await page.goto(fullHost, { waitUntil: "networkidle" });
await myWait(1000);
await page.screenshot({ path: path.join(testDir, "firefox_index.png") });
}
/**
* execute tests and take screenshots with webkit
* TODO: not working
* @param {object} page a playwright page
*/
async function testVisualWebkit(page) {
page.on("console", (msg) => console.log(msg.text())); // print page console output to node.js console
await page.goto(fullHost, { waitUntil: "networkidle" });
await myWait(9000);
await page.screenshot({ path: path.join(testDir, "webkit_index.png") });
}
/**
* helper function that resolves after given time
* @param {number} milliseconds time to wait in milliseconds
*/
function myWait(milliseconds) {
return new Promise((res, rej) => {
setTimeout(() => {
res();
}, milliseconds);
});
}
const testList = {
TypoCheckWithoutTypos: {
addr: "./node_modules/@thing-description-playground/core/tests/typo/typoCheckWithoutTypos.json",
},
TypoCheckWithTypos: {
addr: "./node_modules/@thing-description-playground/core/tests/typo/typoCheckWithTypos.json",
},
HttpAndMqtt: {
addr: "./node_modules/@thing-description-playground/core/tests/protocol-detection/httpAndMqtt.json",
},
NoProtocol: {
addr: "./node_modules/@thing-description-playground/core/tests/protocol-detection/noProtocol.json",
},
OnlyHttp: {
addr: "./node_modules/@thing-description-playground/core/tests/protocol-detection/onlyHttp.json",
},
OnlyMqtt: {
addr: "./node_modules/@thing-description-playground/core/tests/protocol-detection/onlyMqtt.json",
},
};
/**
*
* @param {string} testName name of test file
* @param {object} page a playwright page
*/
async function loadTestToEditor(testName, page) {
const testAddr = testList[testName].addr;
await page.evaluate((data) => {
window.editor.setValue(JSON.stringify(data, null, "\t"));
}, getTestData(testAddr));
}
/**
* Fetch the test from the given address and return the JSON object
* @param {string} urlAddr url of the test to fetch
*/
function getTestData(urlAddr) {
try {
const data = fs.readFileSync(urlAddr);
return JSON.parse(data);
} catch (err) {
console.log(`Error while reading the file on address ${urlAddr}: ${err}`);
}
}