๐Ÿ“ฆ microsoft / playwright

๐Ÿ“„ remote-server-impl.js ยท 63 lines
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
63const fs = require('fs');
const cluster = require('cluster');

async function start() {
  const { browserTypeName, launchOptions, stallOnClose, disconnectOnSIGHUP, exitOnFile, exitOnWarning, startStopAndRunHttp } = JSON.parse(process.argv[2]);
  if (stallOnClose) {
    launchOptions.__testHookGracefullyClose = () => {
      console.log(`(stalled=>true)`);
      return new Promise(() => { });
    };
  }
  if (exitOnWarning)
    process.on('warning', () => process.exit(43));
  if (disconnectOnSIGHUP)
    launchOptions.handleSIGHUP = false;

  const playwright = require('playwright-core');

  if (startStopAndRunHttp) {
    const browser = await playwright[browserTypeName].launch(launchOptions);
    await browser.close();
    console.log(`(wsEndpoint=>none)`);
    console.log(`(closed=>success)`);
    require('http').createServer(() => {}).listen();
    return;
  }

  const browserServer = await playwright[browserTypeName].launchServer(launchOptions);
  if (disconnectOnSIGHUP)
    process.on('SIGHUP', () => browserServer._disconnectForTest());

  if (exitOnFile) {
    (async function waitForFileAndExit() {
      while (true) {
        if (fs.existsSync(exitOnFile))
          break;
        await new Promise(f => setTimeout(f, 100));
      }
      process.exit(42);
    })();
  }

  browserServer.on('close', (exitCode, signal) => {
    console.log(`(exitCode=>${exitCode})`);
    console.log(`(signal=>${signal})`);
  });
  console.log(`(tempDir=>${browserServer._userDataDirForTest})`);
  console.log(`(pid=>${browserServer.process().pid})`);
  console.log(`(wsEndpoint=>${browserServer.wsEndpoint()})`);
}

process.on('uncaughtException', error => console.log(error));
process.on('unhandledRejection', reason => console.log(reason));

if (cluster.isWorker || !JSON.parse(process.argv[2]).inCluster) {
  start();
} else {
  cluster.fork();
  cluster.on('exit', (worker, code, signal) => {
    process.exit(0);
  });
}