code: function startPhantom(requestListener, callback, wait, startCfg) {
      startCfg = startCfg || {};
      // If wait is specified, it calls the callback 'wait' ms after server.listening
      // Otherwise the callback is called on phantom.exit
      var server = http.createServer(requestListener).listen(0);
      server.on("listening", function () {
          var instanceId = Math.round(Math.random() * 1000) % 1000;
          var state = {
              retries: [],
              erroredPhantomInstances: 0
          };
  
          var stdout = ""; // stores stdout for later inspection in test cases
          var cfg = {
              maxRetries: startCfg.maxRetries || 0,
              phantomPath: "phantomjs",
              slaveURL: "http://localhost:" + server.address().port,
              pipeStdOut: false,
              phantomInstances: 1,
              args: {
                  autoExitPolling: startCfg.autoExitPolling || 200
              },
              onData: function (data) {
                  stdout += data.toString();
              },
              onAllPhantomsDied: startCfg.onAllPhantomsDied
          };
          cfg.onExit = wait ? (function () {}) : function (code) {
              if (cfg.maxRetries > 0) {
                  // add the original onexit callback only if explicitly asked for
                  var originalOnExit = phantomLauncher.createPhantomExitCb(cfg, state, instanceId).bind(phantomLauncher);
                  originalOnExit(code);
              }
              callback(code, stdOutToMessages(stdout));
          };
  
          var phantomProcess = phantomLauncher.bootPhantom(cfg, state, instanceId);
          if (wait) {
              setTimeout(function () {
                  phantomProcess.kill();
                  callback(0, stdOutToMessages(stdout));
              }, wait);
          }
      });
  }
docs: Test the main script that drives phantom.
  Its responsibilities are:
  - open a URL
  - communicate through callbacks (like sendEvent and screenshot)
  - handle error messages
  - auto-exit if the page is not loaded correctly