testServer.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __export = (target, all) => {
  9. for (var name in all)
  10. __defProp(target, name, { get: all[name], enumerable: true });
  11. };
  12. var __copyProps = (to, from, except, desc) => {
  13. if (from && typeof from === "object" || typeof from === "function") {
  14. for (let key of __getOwnPropNames(from))
  15. if (!__hasOwnProp.call(to, key) && key !== except)
  16. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17. }
  18. return to;
  19. };
  20. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  21. // If the importer is in node compatibility mode or this is not an ESM
  22. // file that has been converted to a CommonJS file using a Babel-
  23. // compatible transform (i.e. "__esModule" has not been set), then set
  24. // "default" to the CommonJS "module.exports" for node compatibility.
  25. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  26. mod
  27. ));
  28. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  29. var testServer_exports = {};
  30. __export(testServer_exports, {
  31. TestRunnerEvent: () => TestRunnerEvent,
  32. TestServerDispatcher: () => TestServerDispatcher,
  33. runTestServer: () => runTestServer,
  34. runUIMode: () => runUIMode
  35. });
  36. module.exports = __toCommonJS(testServer_exports);
  37. var import_util = __toESM(require("util"));
  38. var import_server = require("playwright-core/lib/server");
  39. var import_utils = require("playwright-core/lib/utils");
  40. var import_utilsBundle = require("playwright-core/lib/utilsBundle");
  41. var import_configLoader = require("../common/configLoader");
  42. var import_list = __toESM(require("../reporters/list"));
  43. var import_reporters = require("./reporters");
  44. var import_sigIntWatcher = require("./sigIntWatcher");
  45. var import_testRunner = require("./testRunner");
  46. const originalDebugLog = import_utilsBundle.debug.log;
  47. const originalStdoutWrite = process.stdout.write;
  48. const originalStderrWrite = process.stderr.write;
  49. class TestServer {
  50. constructor(configLocation, configCLIOverrides) {
  51. this._configLocation = configLocation;
  52. this._configCLIOverrides = configCLIOverrides;
  53. }
  54. async start(options) {
  55. this._dispatcher = new TestServerDispatcher(this._configLocation, this._configCLIOverrides);
  56. return await (0, import_server.startTraceViewerServer)({ ...options, transport: this._dispatcher.transport });
  57. }
  58. async stop() {
  59. await this._dispatcher?._setInterceptStdio(false);
  60. await this._dispatcher?.runGlobalTeardown();
  61. }
  62. }
  63. const TestRunnerEvent = {
  64. TestFilesChanged: "testFilesChanged",
  65. RecoverFromStepError: "recoverFromStepError"
  66. };
  67. class TestServerDispatcher {
  68. constructor(configLocation, configCLIOverrides) {
  69. this._serializer = require.resolve("./uiModeReporter");
  70. this._closeOnDisconnect = false;
  71. this._testRunner = new import_testRunner.TestRunner(configLocation, configCLIOverrides);
  72. this.transport = {
  73. onconnect: () => {
  74. },
  75. dispatch: (method, params) => this[method](params),
  76. onclose: () => {
  77. if (this._closeOnDisconnect)
  78. (0, import_utils.gracefullyProcessExitDoNotHang)(0);
  79. }
  80. };
  81. this._dispatchEvent = (method, params) => this.transport.sendEvent?.(method, params);
  82. this._testRunner.on(TestRunnerEvent.TestFilesChanged, (testFiles) => this._dispatchEvent("testFilesChanged", { testFiles }));
  83. this._testRunner.on(TestRunnerEvent.RecoverFromStepError, (stepId, message, location) => this._dispatchEvent("recoverFromStepError", { stepId, message, location }));
  84. }
  85. async _wireReporter(messageSink) {
  86. return await (0, import_reporters.createReporterForTestServer)(this._serializer, messageSink);
  87. }
  88. async _collectingReporter() {
  89. const report = [];
  90. return {
  91. reporter: await (0, import_reporters.createReporterForTestServer)(this._serializer, (e) => report.push(e)),
  92. report
  93. };
  94. }
  95. async initialize(params) {
  96. this._serializer = params.serializer || require.resolve("./uiModeReporter");
  97. this._closeOnDisconnect = !!params.closeOnDisconnect;
  98. await this._setInterceptStdio(!!params.interceptStdio);
  99. await this._testRunner.initialize({
  100. watchTestDirs: !!params.watchTestDirs,
  101. populateDependenciesOnList: !!params.populateDependenciesOnList,
  102. recoverFromStepErrors: !!params.recoverFromStepErrors
  103. });
  104. }
  105. async ping() {
  106. }
  107. async open(params) {
  108. if ((0, import_utils.isUnderTest)())
  109. return;
  110. (0, import_utilsBundle.open)("vscode://file/" + params.location.file + ":" + params.location.line).catch((e) => console.error(e));
  111. }
  112. async resizeTerminal(params) {
  113. this._testRunner.resizeTerminal(params);
  114. }
  115. async checkBrowsers() {
  116. return { hasBrowsers: this._testRunner.hasSomeBrowsers() };
  117. }
  118. async installBrowsers() {
  119. await this._testRunner.installBrowsers();
  120. }
  121. async runGlobalSetup(params) {
  122. await this.runGlobalTeardown();
  123. const { reporter, report } = await this._collectingReporter();
  124. this._globalSetupReport = report;
  125. const { status } = await this._testRunner.runGlobalSetup([reporter, new import_list.default()]);
  126. return { report, status };
  127. }
  128. async runGlobalTeardown() {
  129. const { status } = await this._testRunner.runGlobalTeardown();
  130. const report = this._globalSetupReport || [];
  131. this._globalSetupReport = void 0;
  132. return { status, report };
  133. }
  134. async startDevServer(params) {
  135. await this.stopDevServer({});
  136. const { reporter, report } = await this._collectingReporter();
  137. const { status } = await this._testRunner.startDevServer(reporter, "out-of-process");
  138. return { report, status };
  139. }
  140. async stopDevServer(params) {
  141. const { status } = await this._testRunner.stopDevServer();
  142. const report = this._devServerReport || [];
  143. this._devServerReport = void 0;
  144. return { status, report };
  145. }
  146. async clearCache(params) {
  147. await this._testRunner.clearCache();
  148. }
  149. async listFiles(params) {
  150. const { reporter, report } = await this._collectingReporter();
  151. const { status } = await this._testRunner.listFiles(reporter, params.projects);
  152. return { report, status };
  153. }
  154. async listTests(params) {
  155. const { reporter, report } = await this._collectingReporter();
  156. const { status } = await this._testRunner.listTests(reporter, params);
  157. return { report, status };
  158. }
  159. async runTests(params) {
  160. const wireReporter = await this._wireReporter((e) => this._dispatchEvent("report", e));
  161. const { status } = await this._testRunner.runTests(wireReporter, params);
  162. return { status };
  163. }
  164. async resumeAfterStepError(params) {
  165. await this._testRunner.resumeAfterStepError(params);
  166. }
  167. async watch(params) {
  168. await this._testRunner.watch(params.fileNames);
  169. }
  170. async findRelatedTestFiles(params) {
  171. return this._testRunner.findRelatedTestFiles(params.files);
  172. }
  173. async stopTests() {
  174. await this._testRunner.stopTests();
  175. }
  176. async _setInterceptStdio(intercept) {
  177. if (process.env.PWTEST_DEBUG)
  178. return;
  179. if (intercept) {
  180. if (import_utilsBundle.debug.log === originalDebugLog) {
  181. import_utilsBundle.debug.log = (...args) => {
  182. const string = import_util.default.format(...args) + "\n";
  183. return originalStderrWrite.apply(process.stderr, [string]);
  184. };
  185. }
  186. const stdoutWrite = (chunk) => {
  187. this._dispatchEvent("stdio", chunkToPayload("stdout", chunk));
  188. return true;
  189. };
  190. const stderrWrite = (chunk) => {
  191. this._dispatchEvent("stdio", chunkToPayload("stderr", chunk));
  192. return true;
  193. };
  194. process.stdout.write = stdoutWrite;
  195. process.stderr.write = stderrWrite;
  196. } else {
  197. import_utilsBundle.debug.log = originalDebugLog;
  198. process.stdout.write = originalStdoutWrite;
  199. process.stderr.write = originalStderrWrite;
  200. }
  201. }
  202. async closeGracefully() {
  203. await this._testRunner.closeGracefully();
  204. }
  205. }
  206. async function runUIMode(configFile, configCLIOverrides, options) {
  207. const configLocation = (0, import_configLoader.resolveConfigLocation)(configFile);
  208. return await innerRunTestServer(configLocation, configCLIOverrides, options, async (server, cancelPromise) => {
  209. await (0, import_server.installRootRedirect)(server, [], { ...options, webApp: "uiMode.html" });
  210. if (options.host !== void 0 || options.port !== void 0) {
  211. await (0, import_server.openTraceInBrowser)(server.urlPrefix("human-readable"));
  212. } else {
  213. const channel = await installedChromiumChannelForUI(configLocation, configCLIOverrides);
  214. const page = await (0, import_server.openTraceViewerApp)(server.urlPrefix("precise"), "chromium", {
  215. headless: (0, import_utils.isUnderTest)() && process.env.PWTEST_HEADED_FOR_TEST !== "1",
  216. persistentContextOptions: {
  217. handleSIGINT: false,
  218. channel
  219. }
  220. });
  221. page.on("close", () => cancelPromise.resolve());
  222. }
  223. });
  224. }
  225. async function installedChromiumChannelForUI(configLocation, configCLIOverrides) {
  226. const config = await (0, import_configLoader.loadConfig)(configLocation, configCLIOverrides).catch((e) => null);
  227. if (!config)
  228. return void 0;
  229. if (config.projects.some((p) => (!p.project.use.browserName || p.project.use.browserName === "chromium") && !p.project.use.channel))
  230. return void 0;
  231. for (const channel of ["chromium", "chrome", "msedge"]) {
  232. if (config.projects.some((p) => p.project.use.channel === channel))
  233. return channel;
  234. }
  235. return void 0;
  236. }
  237. async function runTestServer(configFile, configCLIOverrides, options) {
  238. const configLocation = (0, import_configLoader.resolveConfigLocation)(configFile);
  239. return await innerRunTestServer(configLocation, configCLIOverrides, options, async (server) => {
  240. console.log("Listening on " + server.urlPrefix("precise").replace("http:", "ws:") + "/" + server.wsGuid());
  241. });
  242. }
  243. async function innerRunTestServer(configLocation, configCLIOverrides, options, openUI) {
  244. const testServer = new TestServer(configLocation, configCLIOverrides);
  245. const cancelPromise = new import_utils.ManualPromise();
  246. const sigintWatcher = new import_sigIntWatcher.SigIntWatcher();
  247. process.stdin.on("close", () => (0, import_utils.gracefullyProcessExitDoNotHang)(0));
  248. void sigintWatcher.promise().then(() => cancelPromise.resolve());
  249. try {
  250. const server = await testServer.start(options);
  251. await openUI(server, cancelPromise);
  252. await cancelPromise;
  253. } finally {
  254. await testServer.stop();
  255. sigintWatcher.disarm();
  256. }
  257. return sigintWatcher.hadSignal() ? "interrupted" : "passed";
  258. }
  259. function chunkToPayload(type, chunk) {
  260. if (chunk instanceof Uint8Array)
  261. return { type, buffer: chunk.toString("base64") };
  262. return { type, text: chunk };
  263. }
  264. // Annotate the CommonJS export names for ESM import in node:
  265. 0 && (module.exports = {
  266. TestRunnerEvent,
  267. TestServerDispatcher,
  268. runTestServer,
  269. runUIMode
  270. });