Camillo Bruni | 5cd8bb1 | 2024-11-26 11:11:27 | [diff] [blame] | 1 | #! /usr/bin/env node |
| 2 | /* eslint-disable-next-line no-unused-vars */ |
| 3 | import serve from "./server.mjs"; |
| 4 | import { Builder, Capabilities } from "selenium-webdriver"; |
| 5 | import commandLineArgs from "command-line-args"; |
| 6 | import commandLineUsage from "command-line-usage"; |
| 7 | import assert from "assert"; |
| 8 | |
| 9 | const 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 | |
| 15 | function 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 | |
| 36 | const options = commandLineArgs(optionDefinitions); |
| 37 | |
| 38 | if ("help" in options) |
| 39 | printHelp(); |
| 40 | |
| 41 | const BROWSER = options?.browser; |
| 42 | if (!BROWSER) |
| 43 | printHelp("No browser specified, use $BROWSER or --browser"); |
| 44 | |
| 45 | let capabilities; |
| 46 | switch (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 | |
| 68 | process.on("unhandledRejection", (err) => { |
| 69 | console.error(err); |
| 70 | process.exit(1); |
| 71 | }); |
| 72 | process.once("uncaughtException", (err) => { |
| 73 | console.error(err); |
| 74 | process.exit(1); |
| 75 | }); |
| 76 | |
| 77 | const PORT = options.port; |
Camillo Bruni | 6918b99 | 2025-01-23 14:00:51 | [diff] [blame] | 78 | const server = await serve(PORT); |
Camillo Bruni | 5cd8bb1 | 2024-11-26 11:11:27 | [diff] [blame] | 79 | |
| 80 | async 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 Bruni | 238bd9b | 2025-01-28 15:38:05 | [diff] [blame^] | 86 | // callback() is explicitly called without the default event |
| 87 | // as argument to avoid serialization issues with chromedriver. |
Camillo Bruni | b08e2c4 | 2025-01-28 13:17:20 | [diff] [blame] | 88 | globalThis.addEventListener("JetStreamReady", () => callback()); |
Camillo Bruni | 5cd8bb1 | 2024-11-26 11:11:27 | [diff] [blame] | 89 | // 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 Bruni | 0a0dcc9 | 2025-01-28 13:26:23 | [diff] [blame] | 94 | await driver.manage().setTimeouts({ script: 3 * 60_000 }); |
Camillo Bruni | 5cd8bb1 | 2024-11-26 11:11:27 | [diff] [blame] | 95 | results = await driver.executeAsyncScript((callback) => { |
| 96 | globalThis.addEventListener("JetStreamDone", event => callback(event.detail)); |
| 97 | JetStream.start(); |
| 98 | }); |
Camillo Bruni | 0a0dcc9 | 2025-01-28 13:26:23 | [diff] [blame] | 99 | console.log("\n✅ Tests completed!"); |
| 100 | console.log("RESULTS:") |
Camillo Bruni | 5cd8bb1 | 2024-11-26 11:11:27 | [diff] [blame] | 101 | console.log(results) |
Camillo Bruni | dcf3f68 | 2025-01-28 13:30:58 | [diff] [blame] | 102 | } catch(e) { |
| 103 | console.error("\n❌ Tests failed!"); |
| 104 | console.error(e); |
| 105 | throw e; |
Camillo Bruni | 0a0dcc9 | 2025-01-28 13:26:23 | [diff] [blame] | 106 | } finally { |
Camillo Bruni | 5cd8bb1 | 2024-11-26 11:11:27 | [diff] [blame] | 107 | driver.quit(); |
| 108 | server.close(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | setImmediate(testEnd2End); |