playwright.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/node
  2. "use strict";
  3. const yargs = require('yargs');
  4. const uuid = require('uuid');
  5. const express = require('express');
  6. const { chromium, firefox, webkit, devices } = require('playwright');
  7. const fs = require('fs');
  8. //TODO use this to define the interface for /command
  9. let rawdata = fs.readFileSync('api.json');
  10. let spec = JSON.parse(rawdata);
  11. //console.log(spec);
  12. //TODO support device commands
  13. const argv = yargs
  14. .command('firefox', 'Starts a playwright instance of firefox', {
  15. firefox: {
  16. description: 'Start a firefox instance',
  17. alias: 'f',
  18. type: 'boolean',
  19. }
  20. })
  21. .command('chrome', 'Starts a playwright instance of chrome', {
  22. chrome: {
  23. description: 'Start a chrome instance',
  24. alias: 'c',
  25. type: 'boolean',
  26. }
  27. })
  28. .command('webkit', 'Starts a playwright instance of webkit', {
  29. webkit: {
  30. description: 'Start a webkit instance',
  31. alias: 'w',
  32. type: 'boolean',
  33. }
  34. })
  35. .option('port', {
  36. alias: 'p',
  37. description: 'Run on specified port',
  38. type: 'number',
  39. })
  40. .option('visible', {
  41. alias: 'v',
  42. description: 'Run with headless mode off',
  43. type: 'boolean',
  44. })
  45. .help()
  46. .alias('help', 'h')
  47. .argv;
  48. const app = express();
  49. const port = argv.port || 6969;
  50. var browser;
  51. var pages = {};
  52. //XXX this is probably a race but I don't care yet
  53. (async () => {
  54. if (argv._.includes('firefox')) {
  55. browser = await firefox.launch( { "headless" : !argv.visible } );
  56. }
  57. if (argv._.includes('chrome')) {
  58. browser = await chrome.launch( { "headless" : !argv.visible } );
  59. }
  60. if (argv._.includes('webkit')) {
  61. browser = await webkit.launch( { "headless" : !argv.visible } );
  62. }
  63. if (!browser) {
  64. console.log(argv);
  65. console.log('no browser selected, begone');
  66. process.exit(1);
  67. }
  68. pages.default = await browser.newPage();
  69. pages.default.goto('http://google.com');
  70. console.log('Browser Ready for use');
  71. })();
  72. var results = {};
  73. app.use(express.json())
  74. app.get('/command', async (req, res) => {
  75. var payload = req.query;
  76. var page = payload.page || 'default';
  77. var command = payload.command;
  78. var args = payload.args;
  79. console.log(...args);
  80. var result = {};
  81. if (pages[page]) {
  82. const res = await pages[page][command](...args);
  83. result = { error : false, message : res };
  84. } else {
  85. result = { error : true, message : "No such page, or " + command + " is not a globally recognized command for puppeteer" };
  86. }
  87. res.json(result);
  88. });
  89. app.get('/shutdown', async (req, res) => {
  90. console.log('shutting down...');
  91. await browser.close();
  92. console.log('done');
  93. res.send("Sent kill signal to browser");
  94. process.exit(0);
  95. });
  96. //Modulino
  97. if (require.main === module) {
  98. app.listen( port, () => {
  99. console.log(`Listening on port ${port}`);
  100. });
  101. }