blob: 8e33ebdb69c3a5a75963aaed8e74824b5dcc482a [file] [log] [blame]
Camillo Bruni5cd8bb12024-11-26 11:11:271#! /usr/bin/env node
2/* eslint-disable-next-line no-unused-vars */
3import serve from "./server.mjs";
4import { Builder, Capabilities } from "selenium-webdriver";
5import commandLineArgs from "command-line-args";
6import commandLineUsage from "command-line-usage";
7import assert from "assert";
8
9const optionDefinitions = [
10 { name: "browser", type: String, description: "Set the browser to test, choices are [safari, firefox, chrome]. By default the $BROWSER env variable is used." },
11 { name: "port", type: Number, defaultValue: 8010, description: "Set the test-server port, The default value is 8010." },
12 { name: "help", alias: "h", description: "Print this help text." },
13];
14
15function printHelp(message = "") {
16 const usage = commandLineUsage([
17 {
18 header: "Run all tests",
19 },
20 {
21 header: "Options",
22 optionList: optionDefinitions,
23 },
24 ]);
25 if (!message) {
26 console.log(usage);
27 process.exit(0);
28 } else {
29 console.error(message);
30 console.error();
31 console.error(usage);
32 process.exit(1);
33 }
34}
35
36const options = commandLineArgs(optionDefinitions);
37
38if ("help" in options)
39 printHelp();
40
41const BROWSER = options?.browser;
42if (!BROWSER)
43 printHelp("No browser specified, use $BROWSER or --browser");
44
45let capabilities;
46switch (BROWSER) {
47 case "safari":
48 capabilities = Capabilities.safari();
49 break;
50
51 case "firefox": {
52 capabilities = Capabilities.firefox();
53 break;
54 }
55 case "chrome": {
56 capabilities = Capabilities.chrome();
57 break;
58 }
59 case "edge": {
60 capabilities = Capabilities.edge();
61 break;
62 }
63 default: {
64 printHelp(`Invalid browser "${BROWSER}", choices are: "safari", "firefox", "chrome", "edge"`);
65 }
66}
67
68process.on("unhandledRejection", (err) => {
69 console.error(err);
70 process.exit(1);
71});
72process.once("uncaughtException", (err) => {
73 console.error(err);
74 process.exit(1);
75});
76
77const PORT = options.port;
Camillo Bruni6918b992025-01-23 14:00:5178const server = await serve(PORT);
Camillo Bruni5cd8bb12024-11-26 11:11:2779
80async function testEnd2End() {
81 const driver = await new Builder().withCapabilities(capabilities).build();
82 let results;
83 try {
84 await driver.get(`http://localhost:${PORT}/index.html?worstCaseCount=2&iterationCount=3`);
85 await driver.executeAsyncScript((callback) => {
Camillo Bruni238bd9b2025-01-28 15:38:0586 // callback() is explicitly called without the default event
87 // as argument to avoid serialization issues with chromedriver.
Camillo Brunib08e2c42025-01-28 13:17:2088 globalThis.addEventListener("JetStreamReady", () => callback());
Camillo Bruni5cd8bb12024-11-26 11:11:2789 // We might not get a chance to install the on-ready listener, thus
90 // we also check if the runner is ready synchronously.
91 if (globalThis?.JetStream?.isReady)
92 callback()
93 });
Camillo Bruni0a0dcc92025-01-28 13:26:2394 await driver.manage().setTimeouts({ script: 3 * 60_000 });
Camillo Bruni5cd8bb12024-11-26 11:11:2795 results = await driver.executeAsyncScript((callback) => {
96 globalThis.addEventListener("JetStreamDone", event => callback(event.detail));
97 JetStream.start();
98 });
Camillo Bruni0a0dcc92025-01-28 13:26:2399 console.log("\n✅ Tests completed!");
100 console.log("RESULTS:")
Camillo Bruni5cd8bb12024-11-26 11:11:27101 console.log(results)
Camillo Brunidcf3f682025-01-28 13:30:58102 } catch(e) {
103 console.error("\n❌ Tests failed!");
104 console.error(e);
105 throw e;
Camillo Bruni0a0dcc92025-01-28 13:26:23106 } finally {
Camillo Bruni5cd8bb12024-11-26 11:11:27107 driver.quit();
108 server.close();
109 }
110}
111
112setImmediate(testEnd2End);